-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (56 loc) · 1.93 KB
/
script.js
File metadata and controls
69 lines (56 loc) · 1.93 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
async function translateText() {
const text = document.getElementById("inputText").value;
const key = document.getElementById("apiKey").value;
const region = document.getElementById("region").value;
const target = document.getElementById("targetLang").value;
if (!text || !key || !region) {
alert("Fill all fields");
return;
}
const endpoint = `https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${target}`;
document.getElementById("outputText").value = "Translating...";
try {
const res = await fetch(endpoint, {
method: "POST",
headers: {
"Ocp-Apim-Subscription-Key": key,
"Ocp-Apim-Subscription-Region": region,
"Content-Type": "application/json"
},
body: JSON.stringify([{ Text: text }])
});
const data = await res.json();
const translated = data[0].translations[0].text;
const detected = data[0].detectedLanguage.language;
document.getElementById("outputText").value = translated;
document.getElementById("detectedLang").innerText =
"From: " + detected.toUpperCase();
} catch (e) {
document.getElementById("outputText").value = "Error";
}
}
/* copy */
function copyText(id) {
const text = document.getElementById(id);
text.select();
document.execCommand("copy");
}
/* clear */
function clearText(id) {
document.getElementById(id).value = "";
}
/* speech */
function speak(id) {
const text = document.getElementById(id).value;
const speech = new SpeechSynthesisUtterance(text);
speech.lang = "en-US";
speechSynthesis.speak(speech);
}
/* swap */
function swapLanguages() {
let input = document.getElementById("inputText");
let output = document.getElementById("outputText");
let temp = input.value;
input.value = output.value;
output.value = temp;
}