-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
193 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}`; | ||
} | ||
} |