Skip to content

Commit

Permalink
feat: 添加Lingva翻译接口 (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed May 18, 2023
1 parent 0c40eae commit 1f2682b
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 1 deletion.
188 changes: 188 additions & 0 deletions public/lingva.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/interfaces/index.js
Expand Up @@ -11,6 +11,7 @@ import * as _deepl from './deepl';
import * as _alibaba from './alibaba';
import * as _bing_dict from './bing_dict';
import * as _bing from './bing';
import * as _lingva from './lingva';

export const baidu = _baidu;
export const caiyun = _caiyun;
Expand All @@ -24,4 +25,5 @@ export const google = _google;
export const deepl = _deepl;
export const alibaba = _alibaba;
export const bing_dict = _bing_dict;
export const bing = _bing;
export const bing = _bing;
export const lingva = _lingva;
62 changes: 62 additions & 0 deletions src/interfaces/lingva.js
@@ -0,0 +1,62 @@
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) {

const { supportLanguage } = info;

if (!(to in supportLanguage) || !(from in supportLanguage)) {
return '该接口不支持该语言';
}

let domain = get('lingva_domain') ?? '';
if (domain == '') {
domain = 'lingva.ml';
}

let res = await fetch(`https://${domain}/api/v1/${supportLanguage[from]}/${supportLanguage[to]}/${encodeURIComponent(text)}`, {
method: 'GET'
})

if (res.ok) {
let result = res.data;
if (result.translation && 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);
return;
}
}
setText(result.translation);
} else {
throw JSON.stringify(result);
}
} else {
throw 'http请求出错\n' + JSON.stringify(res);
}
}

0 comments on commit 1f2682b

Please sign in to comment.