forked from lahiru-rajapakshe/language-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (65 loc) · 2.41 KB
/
script.js
File metadata and controls
82 lines (65 loc) · 2.41 KB
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
const formText=document.querySelector(".form-text");
const toText=document.querySelector(".to-text");
selectTag=document.querySelectorAll("select");
exchangeIcon=document.querySelector(".exchange");
translatorBtn=document.querySelector("button");
icons=document.querySelectorAll(".row i");
selectTag.forEach((tag,id)=>{
for (const country_code in countries) {
let selected;
if(id==0 && country_code == "en-GB"){
selected="selected";
}else if(id==1 && country_code == "hi-IN"){
selected="selected";
}
console.log(countries[country_code]);
let option=`<option value="${country_code}" ${selected}>${countries[country_code]}</option>`;
tag.insertAdjacentHTML("beforeend",option);
}
});
translatorBtn.addEventListener("click",()=>{
let text=formText.value,
translateFrom=selectTag[0].value,
translateTo=selectTag[1].value;
if(!text)return;
toText.setAttribute("placeholder","Translating..");
let api_url=`https://api.mymemory.translated.net/get?q=${text}&langpair=${translateFrom}|${translateTo}`;
fetch(api_url).then(res=>res.json()).then(data=>{
// console.log(data);
toText.value=data.responseData.translatedText;
toText.setAttribute("placeholder","Translation");
});
});
exchangeIcon.addEventListener("click",()=>{
let tempText=formText.value;
let tempLang=selectTag[0].value;
formText.value=toText.value;
selectTag[0].value=selectTag[1].value;
toText.value=tempText;
selectTag[1].value=tempLang;
});
icons.forEach(icon=>{
icon.addEventListener("click",({target})=>{
if(target.classList.contains("fa-copy")){
if(target.id=="from"){
navigator.clipboard.writeText(formText.value);
}else{
navigator.clipboard.writeText(toText.value);
// console.log(" to copy icon clicked");
}
}else{
let utterance;
if(target.id=="from"){
// navigator.clipboard.writeText(formText.value);
utterance=new SpeechSynthesisUtterance(formText.value);
utterance.lang=selectTag[0].value;
}else{
utterance=new SpeechSynthesisUtterance(toText.value);
utterance.lang=selectTag[1].value;
// navigator.clipboard.writeText(toText.value);
// console.log(" to copy icon clicked");
}
speechSynthesis.speak(utterance);
}
});
});