Skip to content

Commit

Permalink
feat: Add NiuTrans
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Sep 5, 2023
1 parent 9ed35b9 commit a58377a
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 5 deletions.
File renamed without changes
4 changes: 4 additions & 0 deletions src/i18n/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@
"title": "VolcEngine",
"appid": "AppID",
"secret": "Secret"
},
"niutrans": {
"title": "NiuTrans",
"apikey": "API Key"
}
},
"recognize": {
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/locales/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@
"title": "火山翻译",
"appid": "App ID",
"secret": "Secret"
},
"niutrans": {
"title": "小牛翻译",
"apikey": "API Key"
}
},
"recognize": {
Expand Down
2 changes: 1 addition & 1 deletion src/services/recognize/baidu/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export enum Language {
ru = 'RUS',
de = 'GER',
it = 'ITA',
pt = 'POR',
pt_pt = 'POR',
pt_br = 'POR',
}
2 changes: 1 addition & 1 deletion src/services/recognize/baidu_accurate/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum Language {
de = 'GER',
it = 'ITA',
tr = 'TUR',
pt = 'POR',
pt_pt = 'POR',
pt_br = 'POR',
vi = 'VIE',
id = 'IND',
Expand Down
2 changes: 1 addition & 1 deletion src/services/recognize/baidu_img/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum Language {
de = 'de',
it = 'it',
tr = 'tr',
pt = 'pt',
pt_pt = 'pt',
pt_br = 'pot',
vi = 'vie',
id = 'id',
Expand Down
2 changes: 1 addition & 1 deletion src/services/recognize/tencent/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export enum Language {
ru = 'rus',
de = 'ger',
it = 'ita',
pt = 'por',
pt_pt = 'por',
pt_br = 'por',
vi = 'vie',
th = 'tha',
Expand Down
2 changes: 1 addition & 1 deletion src/services/recognize/tencent_img/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export enum Language {
ru = 'ru',
de = 'de',
it = 'it',
pt = 'pt',
pt_pt = 'pt',
pt_br = 'pt',
vi = 'vi',
th = 'th',
Expand Down
1 change: 1 addition & 0 deletions src/services/translate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * as baidu from './baidu';
export * as baidu_field from './baidu_field';
export * as tencent from './tencent';
export * as volcengine from './volcengine';
export * as niutrans from './niutrans';
81 changes: 81 additions & 0 deletions src/services/translate/niutrans/Config.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Input, Button } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import { open } from '@tauri-apps/api/shell';
import React, { useState } from 'react';

import { useConfig } from '../../../hooks/useConfig';
import { useToastStyle } from '../../../hooks';
import { translate } from './index';
import { Language } from './index';

export function Config(props) {
const { updateServiceList, onClose } = props;
const [config, setConfig] = useConfig(
'niutrans',
{
apikey: '',
},
{ sync: false }
);
const [isLoading, setIsLoading] = useState(false);

const { t } = useTranslation();
const toastStyle = useToastStyle();

return (
config !== null && (
<>
<Toaster />
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.help')}</h3>
<Button
onPress={() => {
open('https://pot-app.com/docs/tutorial/api/translate');
}}
>
{t('services.help')}
</Button>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.translate.niutrans.apikey')}</h3>
<Input
value={config['apikey']}
variant='bordered'
className='max-w-[50%]'
onValueChange={(value) => {
setConfig({
...config,
apikey: value,
});
}}
/>
</div>
<div>
<Button
isLoading={isLoading}
color='primary'
fullWidth
onPress={() => {
setIsLoading(true);
translate('hello', Language.auto, Language.zh_cn, { config }).then(
() => {
setIsLoading(false);
setConfig(config, true);
updateServiceList('niutrans');
onClose();
},
(e) => {
setIsLoading(false);
toast.error(t('config.service.test_failed') + e.toString(), { style: toastStyle });
}
);
}}
>
{t('common.save')}
</Button>
</div>
</>
)
);
}
43 changes: 43 additions & 0 deletions src/services/translate/niutrans/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { fetch, Body } from '@tauri-apps/api/http';
import { store } from '../../../utils/store';

export async function translate(text, from, to, options = {}) {
const { config } = options;

let translateConfig = (await store.get('niutrans')) ?? {};
if (config !== undefined) {
translateConfig = config;
}

const { apikey } = translateConfig;

const url = `https://api.niutrans.com/NiuTransServer/translation`;

let res = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: Body.json({
from: from,
to: to,
apikey: apikey,
src_text: text,
}),
});

// 返回翻译结果
if (res.ok) {
let result = res.data;
if (result && result['tgt_text']) {
return result['tgt_text'].trim();
} else {
throw JSON.stringify(result);
}
} else {
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`;
}
}

export * from './Config';
export * from './info';
31 changes: 31 additions & 0 deletions src/services/translate/niutrans/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const info = {
name: 'niutrans',
icon: 'logo/niutrans.svg',
};

export enum Language {
auto = 'auto',
zh_cn = 'zh',
zh_tw = 'cht',
yue = 'yue',
en = 'en',
ja = 'ja',
ko = 'ko',
fr = 'fr',
es = 'es',
ru = 'ru',
de = 'de',
it = 'it',
tr = 'tr',
pt_pt = 'pt',
pt_br = 'pt',
vi = 'vi',
id = 'id',
th = 'th',
ms = 'ms',
ar = 'ar',
hi = 'hi',
mn_cy = 'mn',
mn_mo = 'mo',
km = 'km',
}

0 comments on commit a58377a

Please sign in to comment.