generated from pot-app/pot-app-translate-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
37 lines (36 loc) · 1.09 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
async function translate(text, from, to, options) {
const { utils } = options;
const { tauriFetch: fetch } = utils;
const res = await fetch(`https://tatoeba.org/eng/api_v0/search`, {
method: 'GET',
query:
{
"query": text,
"from": from,
"to": to,
"has_audio": "no",
"sort": "relevance"
}
});
if (res.ok) {
let result = res.data;
let final = { "sentence": [] };
const { results } = result;
for (let i of results) {
let source = i.text;
let target = "";
let translations = o.translations;
for (let j in translations) {
for (let k in j) {
target.push(k.text);
target.push("\n");
}
}
let sentence = { "source": source, "target": target };
final.sentence.push(sentence);
}
return final;
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}