-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
71 lines (62 loc) · 1.51 KB
/
main.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
/* test();
*/
async function getlangs() {
let res = await fetch(`https://libretranslate.de/languages`);
let data = await res.json();
appenddata(data);
}
getlangs();
function appenddata(d) {
let maindiv = document.getElementById(`Langs`);
d.forEach((el) => {
let opt = document.createElement(`option`);
opt.value = el.code;
opt.textContent = el.name;
maindiv.append(opt);
});
}
function getTheValu() {
let val = document.getElementById(`Langs`).value;
localStorage.setItem(`lang`, JSON.stringify(val));
return val;
}
async function detectLang() {
const res = await fetch("https://libretranslate.de/detect", {
method: "POST",
body: JSON.stringify({
q: "hola",
source: "en",
target: "es",
}),
headers: { "Content-Type": "application/json" },
});
console.log(await res.json());
}
//detectLang();
function getInput() {
let log = document.getElementById("Box");
return log.value;
}
let userinput = getInput();
async function translate() {
const res = await fetch("https://libretranslate.de/translate", {
method: "POST",
body: JSON.stringify({
q: getInput(),
source: "en",
target: getTheValu(),
}),
headers: { "Content-Type": "application/json" },
});
let data = await res.json();
let { translatedText } = data;
appendres(translatedText);
}
function getthevalu() {
let log = document.getElementById("Box");
translate();
}
function appendres(data) {
let trgt = document.getElementById(`Box2`);
trgt.value = data;
}