Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加Lingva翻译接口 #103

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
}
}