-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.html
489 lines (400 loc) · 15.1 KB
/
random.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>50languages unofficial player - listen to random sentences</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<body class="container">
<h1 class="fs-4 mt-3 mb-3">50languages unofficial player - listen to random sentences</h1>
<p>Practice your listening skills. Choose one of 60 languages and listen to random sentences. Add translation if you want.</p>
<div class="card container mt-1 mb-3">
<div class="fs-6 row m-1">
<div class="col-2">
<label for="slc-lang">Select language to listen:</label>
<select id="slc-lang" class="form-select mt-2 mb-3 mr-2 fs-6"></select>
</div>
<button id="btn-play" type="button" class="btn btn-primary btn-lg mt-2 mb-3 fs-6 col-2" title="Play new audio">▶️ New <span id="btn-play-spinner" class="collapse spinner-border spinner-border-sm" role="status" aria-hidden="true"></span></button>
<button id="btn-repeat" type="button" class="btn btn-secondary btn-lg mt-2 mb-3 fs-6 col-2">🔁 Repeat <span id="btn-repeat-spinner" class="collapse spinner-border spinner-border-sm" role="status" aria-hidden="true"></span></button>
<button id="btn-repeat-slowly" type="button" class="btn btn-secondary btn-lg mt-2 mb-3 fs-6 col-2">🐢 Repeat slowly <span id="btn-repeat-slowly-spinner" class="collapse spinner-border spinner-border-sm" role="status" aria-hidden="true"></span></button>
<button id="btn-translate" type="button" class="btn btn-success btn-lg mt-2 mb-3 fs-6 col-2">🕮 Translate <span id="btn-translate-spinner" class="collapse spinner-border spinner-border-sm" role="status" aria-hidden="true"></span></button>
<div class="col-1">
<label for="slc-lang-translation">Select translation:</label>
<select id="slc-lang-translation" class="form-select mt-2 mb-3 mr-2 fs-6"></select>
</div>
</div>
<div>
<div class="form-check form-switch form-check-inline" title="Will play continuously. To stop switch off Auto-play.">
<input id="check-autoplay" class="form-check-input" type="checkbox" role="switch">
<label for="check-autoplay" class="form-check-label">Auto-play</label>
</div>
<div class="form-check form-switch form-check-inline" title="After original audio will play translation. You can select language for translation.">
<input id="check-play-with-translation" class="form-check-input" type="checkbox" role="switch">
<label for="check-play-with-translation" class="form-check-label">Play with translation</label>
</div>
<div class="form-check form-switch form-check-inline" title="Show details about playing audio.">
<input id="check-details" class="form-check-input" type="checkbox" role="switch" data-bs-toggle="collapse" data-bs-target="#details" aria-expanded="false" aria-controls="details">
<label for="check-details" class="form-check-label">Show details</label>
</div>
</div>
</div>
<div id="details" class="card collapse mt-1 mb-3 p-2">
<h5 class="fs-5 fw-bold">Details</h5>
<p>Last playing audio: language <span id="details-language">??</span>, lesson <span id="details-lesson-number">??</span>. <a id="details-text-url" target="_blank">Open lesson with text</a>. <a id="details-sound-url">Open sound file</a>.
</p>
</div>
<div class="mt-4 pt-3">
<h5 class="fs-6 fw-bold">Description:</h5>
<p>This web application is for free practice of listening to foreign languages.<br>
Audio is property of <a href="https://www.goethe-verlag.com/">Goethe Verlag GmbH</a> and <a href="https://www.50languages.com/">50Languages</a>.<br>
This is unofficial application, not related to Goethe Verlag GmbH.
</p>
<h5 class="fs-6 fw-bold">Credits:</h5>
<p><a href="https://www.linkedin.com/in/hassman/">Martin Hassman</a>, programming.<br>
Goethe Verlag GmbH, <a href="https://www.50languages.com/">50 Languages project</a> for audio files.
</p>
</div>
<footer class="text-center fst-italic pt-4">
<p><a href="https://github.com/met/50languages-unofficial-player">Source code</a> available (MIT License).</p>
</footer>
<script>
'use strict';
let appState = { lastAudio: null, lastSoundId: null };
let languages = [
['AD','адыгабзэ'],
['AF','Afrikaans'],
['AM','አማርኛ'],
['AR','العربية'],
['BE','беларуская'],
['BG','български'],
['BN','বাংলা'],
['BS','bosanski'],
['CA','català'],
['CS','čeština'],
['DA','dansk'],
['DE','Deutsch'],
['EL','ελληνικά'],
['EM','English US'],
['EN','English UK'],
['EO','esperanto'],
['ES','español'],
['ET','eesti'],
['FA','فارسی'],
['FI','suomi'],
['FR','français'],
['HE','עברית'],
['HI','हिन्दी'],
['HR','hrvatski'],
['HU','magyar'],
['HY','հայերեն'],
['ID','bahasa Indonesia'],
['IT','italiano'],
['JA','日本語'],
['KA','ქართული'],
['KK','қазақша'],
['KN','ಕನ್ನಡ'],
['KO','한국어'],
['KY','кыргызча'],
['LT','lietuvių'],
['LV','latviešu'],
['MK','македонски'],
['MR','मराठी'],
['NL','Nederlands'],
['NN','nynorsk'],
['NO','norsk'],
['PA','ਪੰਜਾਬੀ'],
['PL','polski'],
['PT','português PT'],
['PX','português BR'],
['RO','română'],
['RU','русский'],
['SK','slovenčina'],
['SL','slovenščina'],
['SQ','Shqip'],
['SR','српски'],
['SV','svenska'],
['TA','தமிழ்'],
['TE','తెలుగు'],
['TH','ภาษาไทย'],
['TI','ትግርኛ'],
['TL','Tagalog'],
['TR','Türkçe'],
['UK','українська'],
['UR','اردو'],
['VI','Tiếng Việt'],
['ZH','中文'],
];
function populateLanguageSelect(selectId, languages) {
let selectEl = document.getElementById(selectId);
for (let lang of languages) {
let langCode = lang[0];
let langText = lang[1];
let optEl = document.createElement('option');
optEl.textContent = `${langCode} - ${langText}`;
optEl.value = langCode;
selectEl.appendChild(optEl);
}
}
function setLanguageSelectHandler(selectId) {
let selectEl = document.getElementById(selectId);
selectEl.addEventListener('change', function(event) {
localStorage.setItem('selectedLanguage', event.target.value);
});
}
function setLanguageTranslateSelectHandler(selectId) {
let selectEl = document.getElementById(selectId);
selectEl.addEventListener('change', function(event) {
localStorage.setItem('selectedLanguageTranslate', event.target.value);
});
}
// Load last selected language. Otherwise set default
function setLastLanguage(selectId) {
const DEFAULT_LANGUAGE = 'EN';
let selectEl = document.getElementById(selectId);
let lastLanguage = localStorage.getItem('selectedLanguage');
if (lastLanguage) {
selectEl.value = lastLanguage;
}
else {
selectEl.value = DEFAULT_LANGUAGE;
}
}
// Load last selected language. Otherwise set default
function setLastTranslateLanguage(selectId) {
const DEFAULT_LANGUAGE = 'EN';
let selectEl = document.getElementById(selectId);
let lastLanguage = localStorage.getItem('selectedLanguageTranslate');
if (lastLanguage) {
selectEl.value = lastLanguage;
}
else {
selectEl.value = DEFAULT_LANGUAGE;
}
}
function makeUrlFromId(id, lang) {
let idString = id.toString();
if (idString.length < 4) {
// Add zeros to the beginning of the string
idString = '0'.repeat(4 - idString.length).concat(idString); // Want '0001' .. '2000' strings
}
let mp3Url = `https://www.50languages.com/front_assets/sounds/${lang}/SOUND/${idString}.mp3`;
return mp3Url;
}
// Play random sentence from selected language
function playNewRandom() {
let lang = document.getElementById('slc-lang').value;
let rndNum = Math.floor(Math.random()*2000)+1; // There are mp3 files from 0001 to 2000
let mp3Url = makeUrlFromId(rndNum, lang);
disableButtons();
showPlaySpinningWheel();
populateDetails(lang, rndNum, mp3Url);
let audio = playURL(mp3Url, function(event) {
// Cleaning GUI and check for autoplay
function afterPlayNewRandomCallback() {
const DELAY_BETWEEN_AUDIOS = 750;
if (isAutoPlaySet()) {
setTimeout(playNewRandom, DELAY_BETWEEN_AUDIOS); // play another random audio after small delay
}
else {
hidePlaySpinningWheel();
enableButtons();
}
}
// Now we either call afterPlayNewRandomCallback or we play translation audio first and from the translation callback we call afterPlayNewRandomCallback
let translationLang = document.getElementById('slc-lang-translation').value;
if (isPlayWithTranslationSet() && translationLang) {
let mp3TranslationUrl = makeUrlFromId(rndNum, translationLang);
playURL(mp3TranslationUrl, afterPlayNewRandomCallback);
} else {
afterPlayNewRandomCallback();
}
});
appState.lastAudio = audio;
appState.lastSoundId = rndNum;
}
function playRepeat() {
if (appState.lastAudio) {
let audio = appState.lastAudio;
disableButtons();
showRepeatSpinningWheel();
function refreshGuiAfterPlayingEnds(event) {
hideRepeatSpinningWheel();
enableButtons();
}
// This is Callback that unregister itself after being succesfully completed
let oneTimeCallback = function (event) {
refreshGuiAfterPlayingEnds(event);
audio.removeEventListener('ended', oneTimeCallback);
};
audio.addEventListener('ended', oneTimeCallback);
audio.play();
}
}
function playRepeatSlowly() {
if (appState.lastAudio) {
let audio = appState.lastAudio;
disableButtons();
showRepeatSlowlySpinningWheel();
audio.playbackRate = 0.7;
function refreshGuiAfterPlayingEnds(event) {
audio.playbackRate = audio.defaultPlaybackRate;
hideRepeatSlowlySpinningWheel();
enableButtons();
}
// This is Callback that unregister itself after being succesfully completed
let oneTimeCallback = function (event) {
refreshGuiAfterPlayingEnds(event);
audio.removeEventListener('ended', oneTimeCallback);
};
audio.addEventListener('ended', oneTimeCallback);
audio.play();
}
}
function playTranslate() {
if (appState.lastSoundId) {
let soundId = appState.lastSoundId;
let translateLang = document.getElementById('slc-lang-translation').value;
let mp3Url = makeUrlFromId(soundId, translateLang);
disableButtons();
showTranslateSpinningWheel();
let audio = playURL(mp3Url, function(event) {
hideTranslateSpinningWheel();
enableButtons();
});
}
}
// Play sound at given URL, call callbackEnded when sound ends
// Parameter callbackEnded is optional
// Return reference to audio element
// Eg. playURL('https://www.book2.nl/book2/VI/SOUND/0001.mp3')
function playURL(url, callbackEnded) {
if (url) {
let audio = new Audio(url);
if (callbackEnded) {
// This is Callback that unregister itself after being succesfully completed
let oneTimeCallback = function (event) {
callbackEnded(event);
audio.removeEventListener('ended', oneTimeCallback);
};
audio.addEventListener('ended', oneTimeCallback);
}
console.debug('Play', url);
audio.play();
return audio;
}
else {
console.error('url cannot be null');
}
}
function populateDetails(lang, soundId, soundUrl) {
document.getElementById('details-language').innerText = lang;
document.getElementById('details-sound-url').href = soundUrl;
let translationLanguage = document.getElementById('slc-lang-translation').value;
let lessonId = Math.ceil(soundId / 20); // every lesson contains 20 sounds. lesson 1: 1-20, lessong 2: 21-40 ...
let lineInLesson = soundId % 20;
if (lineInLesson === 0) { lineInLesson = 20; } // Without this last line will not work (need value >=2 value)
document.getElementById('details-lesson-number').innerText = lessonId;
let detailsTextUrlEl = document.getElementById('details-text-url');
detailsTextUrlEl.href = `https://www.50languages.com/${translationLanguage.toLowerCase()}/learn/phrasebook-lessons/${lessonId+161}/${lang.toLowerCase()}#sn_${lineInLesson-2}`; // line minus two, because first two sound are number and name of the lesson
}
function disableButtons() {
disablePlayBtn();
disableRepeatBtn();
disableRepeatSlowlyBtn();
disableTranslateBtn();
}
function enableButtons() {
enablePlayBtn();
enableRepeatBtn();
enableRepeatSlowlyBtn();
enableTranslateBtn();
}
function disablePlayBtn() {
document.getElementById('btn-play').disabled = true;
}
function enablePlayBtn() {
document.getElementById('btn-play').disabled = false;
}
function showPlaySpinningWheel() {
document.getElementById('btn-play-spinner').classList.remove('collapse');
}
function hidePlaySpinningWheel() {
document.getElementById('btn-play-spinner').classList.add('collapse');
}
function disableRepeatBtn() {
document.getElementById('btn-repeat').disabled = true;
}
function enableRepeatBtn() {
document.getElementById('btn-repeat').disabled = false;
}
function showRepeatSpinningWheel() {
document.getElementById('btn-repeat-spinner').classList.remove('collapse');
}
function hideRepeatSpinningWheel() {
document.getElementById('btn-repeat-spinner').classList.add('collapse');
}
function disableRepeatSlowlyBtn() {
document.getElementById('btn-repeat-slowly').disabled = true;
}
function enableRepeatSlowlyBtn() {
document.getElementById('btn-repeat-slowly').disabled = false;
}
function showRepeatSlowlySpinningWheel() {
document.getElementById('btn-repeat-slowly-spinner').classList.remove('collapse');
}
function hideRepeatSlowlySpinningWheel() {
document.getElementById('btn-repeat-slowly-spinner').classList.add('collapse');
}
function disableTranslateBtn() {
document.getElementById('btn-translate').disabled = true;
}
function enableTranslateBtn() {
document.getElementById('btn-translate').disabled = false;
}
function showTranslateSpinningWheel() {
document.getElementById('btn-translate-spinner').classList.remove('collapse');
}
function hideTranslateSpinningWheel() {
document.getElementById('btn-translate-spinner').classList.add('collapse');
}
function isAutoPlaySet() {
return document.getElementById("check-autoplay").checked;
}
function isPlayWithTranslationSet() {
return document.getElementById("check-play-with-translation").checked;
}
function initUI() {
function setPlayHandler(btnId) {
document.getElementById(btnId).addEventListener('click', playNewRandom);
}
function setRepeatHandler(btnId) {
document.getElementById(btnId).addEventListener('click', playRepeat);
}
function setRepeatSlowlyHandler(btnId) {
document.getElementById(btnId).addEventListener('click', playRepeatSlowly);
}
function setTranslateHandler(btnId) {
document.getElementById(btnId).addEventListener('click', playTranslate);
}
populateLanguageSelect('slc-lang', languages);
setLastLanguage('slc-lang');
setLanguageSelectHandler('slc-lang');
populateLanguageSelect('slc-lang-translation', languages);
setLastTranslateLanguage('slc-lang-translation');
setLanguageTranslateSelectHandler('slc-lang-translation');
setPlayHandler('btn-play');
setRepeatHandler('btn-repeat');
setRepeatSlowlyHandler('btn-repeat-slowly');
setTranslateHandler('btn-translate');
enablePlayBtn();
disableRepeatBtn();
disableRepeatSlowlyBtn();
disableTranslateBtn();
}
initUI();
</script>