generated from pot-app/pot-app-translate-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
35 lines (32 loc) · 874 Bytes
/
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
async function translate(text, from, to, options) {
const { utils } = options;
const { tauriFetch: fetch } = utils;
const URL = "https://translate.volcengine.com/crx/translate/v1";
const body = {
source_language: from,
target_language: to,
text
}
const headers = {
'content-type': 'application/json'
};
let res = await fetch(URL, {
method: 'POST',
headers: headers,
body: {
type: 'Json',
payload: body
},
});
if (res.ok) {
let result = res.data;
const { translation } = result;
if (translation) {
return translation;
} else {
throw JSON.stringify(result.trim());
}
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}