-
-
Notifications
You must be signed in to change notification settings - Fork 476
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
12 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
File renamed without changes
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
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 |
---|---|---|
|
@@ -16,6 +16,6 @@ export enum Language { | |
ru = 'RUS', | ||
de = 'GER', | ||
it = 'ITA', | ||
pt = 'POR', | ||
pt_pt = 'POR', | ||
pt_br = 'POR', | ||
} |
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
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
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,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> | ||
</> | ||
) | ||
); | ||
} |
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,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'; |
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,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', | ||
} |