-
-
Notifications
You must be signed in to change notification settings - Fork 406
/
lingva.js
67 lines (60 loc) · 1.91 KB
/
lingva.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
import { translateID } from '../windows/Translator/components/TargetArea';
import { fetch } from '@tauri-apps/api/http';
import { get } from '../windows/main';
export const info = {
name: 'Lingva',
supportLanguage: {
auto: 'auto',
'zh-cn': 'zh',
'zh-tw': 'zh_HANT',
en: 'en',
ja: 'ja',
ko: 'ko',
fr: 'fr',
es: 'es',
ru: 'ru',
de: 'de',
},
needs: [
{
config_key: 'lingva_domain',
place_hold: 'lingva.ml',
display_name: '自定义域名',
},
],
};
export async function translate(text, from, to, setText, id) {
const { supportLanguage } = info;
if (!(to in supportLanguage) || !(from in supportLanguage)) {
return '该接口不支持该语言';
}
let domain = get('lingva_domain') ?? '';
if (domain == '') {
domain = 'lingva.ml';
}
let plainText = text.replaceAll('/', '@@');
let res = await fetch(`https://${domain}/api/v1/${supportLanguage[from]}/${supportLanguage[to]}/${encodeURIComponent(plainText)}`, {
method: 'GET'
})
if (res.ok) {
let result = res.data;
if (result.translation) {
if (result.info && result.info.detectedSource) {
if (result.info.detectedSource == supportLanguage[to]) {
let secondLanguage = get('second_language') ?? 'en';
if (secondLanguage != to) {
await translate(text, from, secondLanguage, setText, id);
return;
}
}
}
if (translateID.includes(id)) {
setText(result.translation.replaceAll('@@', '/'));
}
} else {
throw JSON.stringify(result);
}
} else {
throw `Http请求错误\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}