-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
127 lines (97 loc) · 3.47 KB
/
script.js
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
let btn1 = document.querySelectorAll("btn1");
// for the history
let outputText = "";
const selectTag = document.querySelectorAll("select");
selectTag.forEach((tag,id) => {
for(const countryCode in countries ){
let selected;
if(id == 0 && countryCode == "en-GB"){
selected = "selected";
}else if(id == 1 && countryCode == "si-LK"){
selected = "selected";
}
let option = `<option value="${countryCode}" ${selected}>${countries[countryCode]}</option>`;
tag.insertAdjacentHTML("beforeend",option);
}
})
function setSelectLanguage(){
let selectLang1 = document.getElementById("select1").value;
let selectLang2 = document.getElementById("select2").value;
document.getElementById("setSelectedLang1").innerHTML = countries[selectLang1];
document.getElementById("setSelectedLang2").innerHTML = countries[selectLang2];
}
function setFlags(){
let selectLang1 = document.getElementById("select1").value;
let selectLang2 = document.getElementById("select2").value;
let flag1 = countries[selectLang1].substring(0,2)
let lowerCaseFlag1 = flag1.toLocaleLowerCase(flag1)
console.log(lowerCaseFlag1);
let flag2 = countries[selectLang2].substring(0,2)
let lowerCaseFlag2 = flag2.toLocaleLowerCase(flag2)
console.log(lowerCaseFlag2);
document.getElementById("setFlag1").innerHTML=`<img src="https://flagcdn.com/w40/${lowerCaseFlag1}.png" alt="English" class="flag-icon me-2">`
document.getElementById("setFlag2").innerHTML=`<img src="https://flagcdn.com/w40/${lowerCaseFlag2}.png" alt="English" class="flag-icon me-2">`
}
function convert(){
let inputText = document.getElementById("inputText").value;
let select1= document.getElementById("select1").value;
let select2 = document.getElementById("select2").value;
console.log(inputText);
setSelectLanguage();
setFlags();
const raw = "";
const requestOptions = {
method: "POST",
body: raw,
redirect: "follow"
};
fetch(`https://api.mymemory.translated.net/get?q=${inputText}&langpair=${select1}|${select2}`, requestOptions)
.then(res => res.json())
.then(data => {
let output = data.responseData.translatedText;
document.getElementById("setText").innerHTML=output;
outputText=output;
})
.catch((error) => console.error(error));
setTimeout(() => {
history();
}, 1000);
}
function speech(){
let inputText = document.getElementById("inputText").value;
try {
let utr = new SpeechSynthesisUtterance(inputText);
speechSynthesis.speak(utr);
} catch (e) {
console.error("Speech synthesis failed:", e);
}
}
let historyArr = [];
function history(){
let inputText = document.getElementById("inputText").value;
let inputHistory = `<ul>
<li>Input Text Is : ${inputText}</li>
<li>Translated Text Is : ${outputText}</li>
</ul>`
historyArr.push(inputHistory);
console.log(historyArr);
}
function historyBtn(){
Swal.fire({
title: historyArr,
showClass: {
popup: `
animate__animated
animate__fadeInUp
animate__faster
`
},
hideClass: {
popup: `
animate__animated
animate__fadeOutDown
animate__faster
`
}
});
}