-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.html
502 lines (482 loc) · 19.1 KB
/
voice.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
490
491
492
493
494
495
496
497
498
499
500
501
502
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Translation of Heart</title>
<script src="https://code.iconify.design/2/2.2.1/iconify.min.js"></script>
</head>
<style>
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap");
* {
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
display: flex;
align-items: center;
justify-content: center;
padding: 0 10px;
min-height: 100vh;
background: #5372f0;
}
#container {
max-width: 690px;
width: 100%;
padding: 2rem;
background: #fff;
border-radius: 7px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.01);
}
#wrapper {
border-radius: 5px;
border: 1px solid #ccc;
}
#wrapper #Inputs {
display: flex;
border-bottom: 1px solid #ccc;
}
#wrapper #to_text {
border-radius: 0px;
border-left: 1px solid #ccc;
}
textarea {
height: 250px;
width: 100%;
border: none;
outline: none;
resize: none;
background: none;
font-size: 18px;
padding: 10px 15px;
border-radius: 5px;
}
#control,
li,
#icon {
display: flex;
align-items: center;
justify-content: space-between;
}
#control {
list-style: none;
padding: 12px 15px;
}
.row #icon {
width: 30%;
}
.row #icon svg {
width: 52px;
color: #adadad;
font-size: 14px;
cursor: pointer;
transition: transform 0.2s ease;
justify-content: center;
}
.row select {
color: #333;
border: none;
outline: none;
font-size: 15px;
background: none;
margin-right: 20px;
/* border: 1px solid red; */
}
.from select {
margin-right: -2rem;
/* border: 5px solid red; */
}
</style>
<body>
<div id="container">
<div id="wrapper">
<div id="Inputs">
<textarea
spellcheck="true"
id="from_text"
placeholder="Enter Text"
oninput="trasnlate()"
></textarea>
<textarea
spellcheck="true"
id="to_text"
placeholder="Translation..."
></textarea>
</div>
<ul id="control">
<li class="row from">
<div id="icon">
<svg id="talk" width="1em" height="1em" viewBox="0 0 48 48">
<path
fill="#81D4FA"
d="M28 7.1v2c7.3 1 13 7.3 13 14.9s-5.7 13.9-13 14.9v2c8.4-1 15-8.2 15-16.9S36.4 8.1 28 7.1z"
/>
<path
fill="#546E7A"
d="M14 32H7c-1.1 0-2-.9-2-2V18c0-1.1.9-2 2-2h7v16z"
/>
<path fill="#78909C" d="M26 42L14 32V16L26 6z" />
<path
fill="#03A9F4"
d="M28 17.3v2.1c1.8.8 3 2.5 3 4.6s-1.2 3.8-3 4.6v2.1c2.9-.9 5-3.5 5-6.7s-2.1-5.8-5-6.7z"
/>
<path
fill="#4FC3F7"
d="M28 12.2v2c4.6.9 8 5 8 9.8s-3.4 8.9-8 9.8v2c5.7-1 10-5.9 10-11.8s-4.3-10.9-10-11.8z"
/>
</svg>
<svg
id="voice_reconisation"
width="1.2rem"
height="1.2rem"
viewBox="0 0 24 24"
onclick="runSpeechRecognition()"
>
<path
fill="currentColor"
d="M12 15c1.66 0 2.99-1.34 2.99-3L15 6c0-1.66-1.34-3-3-3S9 4.34 9 6v6c0 1.66 1.34 3 3 3zm5.3-3c0 3-2.54 5.1-5.3 5.1S6.7 15 6.7 12H5c0 3.42 2.72 6.23 6 6.72V22h2v-3.28c3.28-.48 6-3.3 6-6.72h-1.7z"
/>
</svg>
<svg id="copy" class="copy" width="1.2rem" height="1.2rem" viewBox="0 0 20 20">
<path
fill="currentColor"
d="M8 2a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2H8ZM7 4a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V4ZM4 6a2 2 0 0 1 1-1.732V14.5A2.5 2.5 0 0 0 7.5 17h6.232A2 2 0 0 1 12 18H7.5A3.5 3.5 0 0 1 4 14.5V6Z"
/>
</svg>
</div>
<select id="inp_lang">
<option value="am">Amharic</option>
<option value="ar">Arabic</option>
<option value="be">Bielarus</option>
<option value="bem">Bemba</option>
<option value="bi">Bislama</option>
<option value="bjs">Bajan</option>
<option value="bn">Bengali</option>
<option value="bo">Tibetan</option>
<option value="br">Breton</option>
<option value="bs">Bosnian</option>
<option value="ca">Catalan</option>
<option value="cop">Coptic</option>
<option value="cs">Czech</option>
<option value="cy">Welsh</option>
<option value="da">Danish</option>
<option value="dz">Dzongkha</option>
<option value="de">German</option>
<option value="dv">Maldivian</option>
<option value="el">Greek</option>
<option selected="" value="en">English</option>
<option value="es">Spanish</option>
<option value="et">Estonian</option>
<option value="eu">Basque</option>
<option value="fa">Persian</option>
<option value="fi">Finnish</option>
<option value="fn">Fanagalo</option>
<option value="fo">Faroese</option>
<option value="fr">French</option>
<option value="gl">Galician</option>
<option value="gu">Gujarati</option>
<option value="ha">Hausa</option>
<option value="he">Hebrew</option>
<option value="hi">Hindi</option>
<option value="hr">Croatian</option>
<option value="hu">Hungarian</option>
<option value="id">Indonesian</option>
<option value="is">Icelandic</option>
<option value="it">Italian</option>
<option value="ja">Japanese</option>
<option value="kk">Kazakh</option>
<option value="km">Khmer</option>
<option value="kn">Kannada</option>
<option value="ko">Korean</option>
<option value="ku">Kurdish</option>
<option value="ky">Kyrgyz</option>
<option value="la">Latin</option>
<option value="lo">Lao</option>
<option value="lv">Latvian</option>
<option value="men">Mende</option>
<option value="mg">Malagasy</option>
<option value="mi">Maori</option>
<option value="ms">Malay</option>
<option value="mt">Maltese</option>
<option value="my">Burmese</option>
<option value="ne">Nepali</option>
<option value="niu">Niuean</option>
<option value="nl">Dutch</option>
<option value="no">Norwegian</option>
<option value="ny">Nyanja</option>
<option value="ur">Pakistani</option>
<option value="pau">Palauan</option>
<option value="pa">Panjabi</option>
<option value="ps">Pashto</option>
<option value="pis">Pijin</option>
<option value="pl">Polish</option>
<option value="pt">Portuguese</option>
<option value="rn">Kirundi</option>
<option value="ro">Romanian</option>
<option value="ru">Russian</option>
<option value="sg">Sango</option>
<option value="si">Sinhala</option>
<option value="sk">Slovak</option>
<option value="sm">Samoan</option>
<option value="sn">Shona</option>
<option value="so">Somali</option>
<option value="sq">Albanian</option>
<option value="sr">Serbian</option>
<option value="sv">Swedish</option>
<option value="sw">Swahili</option>
<option value="ta">Tamil</option>
<option value="te">Telugu</option>
<option value="tet-TL">Tetum</option>
<option value="tg">Tajik</option>
<option value="th">Thai</option>
<option value="ti">Tigrinya</option>
<option value="tk">Turkmen</option>
<option value="tl">Tagalog</option>
<option value="tn">Tswana</option>
<option value="to">Tongan</option>
<option value="tr">Turkish</option>
<option value="uk">Ukrainian</option>
<option value="uz">Uzbek</option>
<option value="vi">Vietnamese</option>
<option value="wo">Wolof</option>
<option value="xh">Xhosa</option>
<option value="yi">Yiddish</option>
<option value="zu">Zulu</option>
</select>
</li>
<li id="exchange">
<svg width="1.2rem" height="1.2rem" viewBox="0 0 32 32">
<path
fill="currentColor"
d="M22.188 2.281L20.78 3.72L25.063 8H4v2h21.063l-4.282 4.281l1.407 1.438L28.905 9zm-12.375 14L3.093 23l6.72 6.719l1.406-1.438L6.938 24H28v-2H6.937l4.282-4.281z"
/>
</svg>
</li>
<li class="row to">
<select id="out_lang">
<option value="am">Amharic</option>
<option value="ar">Arabic</option>
<option value="be">Bielarus</option>
<option value="bem">Bemba</option>
<option value="bi">Bislama</option>
<option value="bjs">Bajan</option>
<option value="bn">Bengali</option>
<option value="bo">Tibetan</option>
<option value="br">Breton</option>
<option value="bs">Bosnian</option>
<option value="ca">Catalan</option>
<option value="cop">Coptic</option>
<option value="cs">Czech</option>
<option value="cy">Welsh</option>
<option value="da">Danish</option>
<option value="dz">Dzongkha</option>
<option value="de">German</option>
<option value="dv">Maldivian</option>
<option value="el">Greek</option>
<option value="en">English</option>
<option value="es">Spanish</option>
<option value="et">Estonian</option>
<option value="eu">Basque</option>
<option value="fa">Persian</option>
<option value="fi">Finnish</option>
<option value="fn">Fanagalo</option>
<option value="fo">Faroese</option>
<option value="fr">French</option>
<option value="gl">Galician</option>
<option value="gu">Gujarati</option>
<option value="ha">Hausa</option>
<option value="he">Hebrew</option>
<option selected="" value="hi">Hindi</option>
<option value="hr">Croatian</option>
<option value="hu">Hungarian</option>
<option value="id">Indonesian</option>
<option value="is">Icelandic</option>
<option value="it">Italian</option>
<option value="ja">Japanese</option>
<option value="kk">Kazakh</option>
<option value="km">Khmer</option>
<option value="kn">Kannada</option>
<option value="ko">Korean</option>
<option value="ku">Kurdish</option>
<option value="ky">Kyrgyz</option>
<option value="la">Latin</option>
<option value="lo">Lao</option>
<option value="lv">Latvian</option>
<option value="men">Mende</option>
<option value="mg">Malagasy</option>
<option value="mi">Maori</option>
<option value="ms">Malay</option>
<option value="mt">Maltese</option>
<option value="my">Burmese</option>
<option value="ne">Nepali</option>
<option value="niu">Niuean</option>
<option value="nl">Dutch</option>
<option value="no">Norwegian</option>
<option value="ny">Nyanja</option>
<option value="ur">Pakistani</option>
<option value="pau">Palauan</option>
<option value="pa">Panjabi</option>
<option value="ps">Pashto</option>
<option value="pis">Pijin</option>
<option value="pl">Polish</option>
<option value="pt">Portuguese</option>
<option value="rn">Kirundi</option>
<option value="ro">Romanian</option>
<option value="ru">Russian</option>
<option value="sg">Sango</option>
<option value="si">Sinhala</option>
<option value="sk">Slovak</option>
<option value="sm">Samoan</option>
<option value="sn">Shona</option>
<option value="so">Somali</option>
<option value="sq">Albanian</option>
<option value="sr">Serbian</option>
<option value="sv">Swedish</option>
<option value="sw">Swahili</option>
<option value="ta">Tamil</option>
<option value="te">Telugu</option>
<option value="tet-TL">Tetum</option>
<option value="tg">Tajik</option>
<option value="th">Thai</option>
<option value="ti">Tigrinya</option>
<option value="tk">Turkmen</option>
<option value="tl">Tagalog</option>
<option value="tn">Tswana</option>
<option value="to">Tongan</option>
<option value="tr">Turkish</option>
<option value="uk">Ukrainian</option>
<option value="uz">Uzbek</option>
<option value="vi">Vietnamese</option>
<option value="wo">Wolof</option>
<option value="xh">Xhosa</option>
<option value="yi">Yiddish</option>
<option value="zu">Zulu</option>
</select>
<div id="icon">
<svg id="ottalk" class="talk" width="1em" height="1em" viewBox="0 0 48 48">
<path
fill="#81D4FA"
d="M28 7.1v2c7.3 1 13 7.3 13 14.9s-5.7 13.9-13 14.9v2c8.4-1 15-8.2 15-16.9S36.4 8.1 28 7.1z"
/>
<path
fill="#546E7A"
d="M14 32H7c-1.1 0-2-.9-2-2V18c0-1.1.9-2 2-2h7v16z"
/>
<path fill="#78909C" d="M26 42L14 32V16L26 6z" />
<path
fill="#03A9F4"
d="M28 17.3v2.1c1.8.8 3 2.5 3 4.6s-1.2 3.8-3 4.6v2.1c2.9-.9 5-3.5 5-6.7s-2.1-5.8-5-6.7z"
/>
<path
fill="#4FC3F7"
d="M28 12.2v2c4.6.9 8 5 8 9.8s-3.4 8.9-8 9.8v2c5.7-1 10-5.9 10-11.8s-4.3-10.9-10-11.8z"
/>
</svg>
<!-- <svg width="1.2rem" height="1.2rem" viewBox="0 0 24 24">
<path
fill="currentColor"
d="M12 15c1.66 0 2.99-1.34 2.99-3L15 6c0-1.66-1.34-3-3-3S9 4.34 9 6v6c0 1.66 1.34 3 3 3zm5.3-3c0 3-2.54 5.1-5.3 5.1S6.7 15 6.7 12H5c0 3.42 2.72 6.23 6 6.72V22h2v-3.28c3.28-.48 6-3.3 6-6.72h-1.7z"
/>
</svg> -->
<svg id="otcopy" class="copy" width="1.2rem" height="1.2rem" viewBox="0 0 20 20">
<path
fill="currentColor"
d="M8 2a2 2 0 0 0-2 2v10a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V4a2 2 0 0 0-2-2H8ZM7 4a1 1 0 0 1 1-1h6a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H8a1 1 0 0 1-1-1V4ZM4 6a2 2 0 0 1 1-1.732V14.5A2.5 2.5 0 0 0 7.5 17h6.232A2 2 0 0 1 12 18H7.5A3.5 3.5 0 0 1 4 14.5V6Z"
/>
</svg>
</div>
</li>
</ul>
</div>
</div>
</body>
</html>
<script>
let output = document.querySelector("#to_text");
let icons = document.querySelectorAll("#icon");
let fromText = document.querySelector("#from_text");
let toText = document.querySelector("#to_text");
let selectTag = document.querySelectorAll("select")
function read(id) {
return document.querySelector(id).value;
}
let trasnlate = async () => {
try {
let input = read("#from_text");
let input_lang = read("#inp_lang");
let output_lang = read("#out_lang");
console.log(input, input_lang, output_lang);
const res = await fetch(`https://libretranslate.de/translate`, {
method: "POST",
body: JSON.stringify({
q: input,
source: input_lang,
target: output_lang,
format: "text",
}),
headers: {
"Content-Type": "application/json",
},
});
const data = await res.json();
showTooutput(data);
console.log(data);
} catch (error) {
console.log("error", error);
output.setAttribute("placeholder", "Translation....")
}
};
output.setAttribute("placeholder", "Translation....")
//properties inside data
let showTooutput = ({ translatedText }) => {
output.innerHTML = translatedText;
};
//speech recognization
function runSpeechRecognition() {
let input = document.querySelector("#from_text");
let voice = document.querySelector("#voice_reconisation");
var SpeechRecognition = SpeechRecognition || webkitSpeechRecognition;
var recognition = new SpeechRecognition();
recognition.onstart = function () {
//voice.innerHTML = "<small>start</small>";
};
recognition.onspeechend = function () {
// voice.innerHTML = "<small>stopp</small>";
recognition.stop();
};
recognition.onresult = function (event) {
let transcript = event.results[0][0].transcript;
input.innerHTML = transcript;
console.log(transcript);
//showTooutput(transcript);
trasnlate();
};
recognition.start();
}
//copy and talk functionality
icons.forEach(icon => {
icon.addEventListener("click", ({target}) => {
if(!fromText.value || !toText.value) return;
if(target.classList.contains("copy")) {
if(target.id == "copy") {
navigator.clipboard.writeText(fromText.value);
console.log("selected 1",fromText.value);
} else {
navigator.clipboard.writeText(toText.value);
console.log("selected 2",toText.value);
}
} else {
let utterance;
if(target.id == "talk") {
utterance = new SpeechSynthesisUtterance(fromText.value);
utterance.lang = selectTag[0].value;
} else {
utterance = new SpeechSynthesisUtterance(toText.value);
utterance.lang = selectTag[1].value;
}
speechSynthesis.speak(utterance);
}
});
});
</script>