Skip to content

Commit

Permalink
feat: 添加腾讯交互翻译接口 (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon authored May 29, 2023
1 parent 814af8a commit ab68b3f
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 2 deletions.
92 changes: 92 additions & 0 deletions public/transmart.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 @@ -13,6 +13,7 @@ import * as _bing_dict from './bing_dict';
import * as _bing from './bing';
import * as _lingva from './lingva';
import * as _xiaoniu from './xiaoniu';
import * as _transmart from './transmart';

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

export const info = {
// 接口中文名称
name: '腾讯交互翻译',
// 接口支持语言及映射
supportLanguage: {
auto: 'auto',
'zh-cn': 'zh',
'zh-tw': 'zh-TW',
en: 'en',
ja: 'ja',
ko: 'ko',
fr: 'fr',
es: 'es',
ru: 'ru',
de: 'de',
it: 'it',
tr: 'tr',
pt: 'pt',
vi: 'vi',
id: 'id',
th: 'th',
ms: 'ms',
ar: 'ar'
},
// 接口需要配置项
needs: [
{
config_key: 'transmart_user',
place_hold: '',
display_name: '用户名',
},
{
config_key: 'transmart_token',
place_hold: '',
display_name: 'Token',
},
],
};

//必须向外暴露translate
export async function translate(text, from, to, setText, id) {
// 获取语言映射
const { supportLanguage } = info;
// 获取设置项
const user = get('transmart_user') ?? '';
const token = get('transmart_token') ?? '';

if (user == '' || token == '') {
throw '请先配置用户名和Token';
}
if (!(from in supportLanguage) || !(to in supportLanguage)) {
throw '该接口不支持该语言';
}

const url = 'https://transmart.qq.com/api/imt';
const analysis_res = await fetch(url, {
method: 'POST',
body: {
type: 'Json',
payload: {
"header": {
"fn": "text_analysis",
"token": token,
"user": user
},
"type": "plain",
"text": text
}
}
})
if (analysis_res.ok) {
let analysis_result = analysis_res.data;
if (analysis_result['language']) {
if (analysis_result['language'] == supportLanguage[to]) {
let secondLanguage = get('second_language') ?? 'en';
to = secondLanguage;
}
if (analysis_result['sentence_list']) {
let text_list = analysis_result['sentence_list'].map(text => {
return text['str']
});
const res = await fetch(url, {
method: 'POST',
body: {
type: 'Json',
payload: {
"header": {
"fn": "auto_translation",
"token": token,
"user": user
},
"type": "plain",
"source": {
"lang": from == 'auto' ? analysis_result['language'] : supportLanguage[from],
"text_list": text_list
},
"target": {
"lang": supportLanguage[to]
}
}
}
})
if (res.ok) {
const result = res.data;
if (result['auto_translation']) {
let target = '';
for (let i of result['auto_translation']) {
if (translateID.includes(id)) {
target += i;
setText(target);
}
}
} else {
throw (JSON.stringify(result));
}
} else {
throw `Http请求错误\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
} else {
throw JSON.stringify(analysis_result);
}

} else {
throw JSON.stringify(analysis_result);
}
} else {
throw `Http请求错误\nHttp Status: ${analysis_res.status}\n${JSON.stringify(analysis_res.data)}`;
}

}
1 change: 0 additions & 1 deletion src/interfaces/xiaoniu.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export async function translate(text, from, to, setText, id) {
// 返回翻译结果
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';
Expand Down

0 comments on commit ab68b3f

Please sign in to comment.