Skip to content

Commit

Permalink
feat:添加翻译接口:小牛翻译 (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliauk365 authored May 24, 2023
1 parent de44ea2 commit 8c64348
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
102 changes: 102 additions & 0 deletions public/xiaoniu.svg
Loading
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 @@ -12,6 +12,7 @@ import * as _alibaba from './alibaba';
import * as _bing_dict from './bing_dict';
import * as _bing from './bing';
import * as _lingva from './lingva';
import * as _xiaoniu from './xiaoniu';

export const baidu = _baidu;
export const caiyun = _caiyun;
Expand All @@ -26,4 +27,5 @@ export const deepl = _deepl;
export const alibaba = _alibaba;
export const bing_dict = _bing_dict;
export const bing = _bing;
export const lingva = _lingva;
export const lingva = _lingva;
export const xiaoniu = _xiaoniu;
88 changes: 88 additions & 0 deletions src/interfaces/xiaoniu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { fetch } from '@tauri-apps/api/http';
import { get } from '../windows/main';
import { translateID } from '../windows/Translator/components/TargetArea/index.jsx';

export const info = {
// 接口中文名称
// 翻译服务商:https://niutrans.com
// 翻译服务接口文档:https://niutrans.com/documents/overview?id=2
name: '小牛翻译',
// 接口支持语言及映射
supportLanguage: {
auto: 'auto',
'zh-cn': 'zh',
'zh-tw': 'cht',
yue: 'yue',
en: 'en',
ja: 'ja',
ko: 'ko',
fr: 'fr',
es: 'es',
ru: 'ru',
de: 'de',
},
// 接口需要配置项(会在设置中出现设置项来获取)
needs: [
{
config_key: 'xiaoniu_apikey',
place_hold: 'ApiKey',
display_name: 'API 密钥',
}
],
};

//必须向外暴露translate
export async function translate(text, from, to, setText, id) {
// 获取语言映射
const { supportLanguage } = info;
// 获取设置项
const apikey = get('xiaoniu_apikey') ?? '';
// 检查设置
if (apikey == '') {
return '请先配置API 密钥';
}
// 检查语言支持
if (!(to in supportLanguage) || !(from in supportLanguage)) {
return '该接口不支持该语言';
}
// 完成翻译过程
const url = `https://api.niutrans.com/NiuTransServer/translation`;

let res = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
type: 'Json',
payload: {
from: supportLanguage[from],
to: supportLanguage[to],
apikey: apikey,
src_text: text
}
}
});

// 返回翻译结果
if (res.ok) {
let result = res.data;
console.log(result);
if (result && result['tgt_text'] && result['from']) {
if (result['from'] == 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['tgt_text']);
}
} else {
throw JSON.stringify(result);
}
} else {
throw `Http请求错误\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}

0 comments on commit 8c64348

Please sign in to comment.