-
-
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
11 changed files
with
214 additions
and
8 deletions.
There are no files selected for viewing
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,96 @@ | ||
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( | ||
'youdao', | ||
{ | ||
appkey: '', | ||
key: '', | ||
}, | ||
{ 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.youdao.appkey')}</h3> | ||
<Input | ||
value={config['appkey']} | ||
variant='bordered' | ||
className='max-w-[50%]' | ||
onValueChange={(value) => { | ||
setConfig({ | ||
...config, | ||
appkey: value, | ||
}); | ||
}} | ||
/> | ||
</div> | ||
<div className={'config-item'}> | ||
<h3 className='my-auto'>{t('services.translate.youdao.key')}</h3> | ||
<Input | ||
value={config['key']} | ||
variant='bordered' | ||
className='max-w-[50%]' | ||
onValueChange={(value) => { | ||
setConfig({ | ||
...config, | ||
key: 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('youdao'); | ||
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,72 @@ | ||
import { fetch } from '@tauri-apps/api/http'; | ||
import { store } from '../../../utils/store'; | ||
import CryptoJS from 'crypto-js'; | ||
import { nanoid } from 'nanoid'; | ||
|
||
export async function translate(text, from, to, options = {}) { | ||
const { config } = options; | ||
|
||
let translateConfig = (await store.get('youdao')) ?? {}; | ||
if (config !== undefined) { | ||
translateConfig = config; | ||
} | ||
|
||
const { appkey, key } = translateConfig; | ||
|
||
const url = 'https://openapi.youdao.com/api'; | ||
const curtime = String(Math.round(new Date().getTime() / 1000)); | ||
const salt = nanoid(); | ||
const str1 = appkey + truncate(text) + salt + curtime + key; | ||
const sign = CryptoJS.SHA256(str1).toString(CryptoJS.enc.Hex); | ||
|
||
let res = await fetch(url, { | ||
method: 'GET', | ||
query: { | ||
q: text, | ||
from: from, | ||
to: to, | ||
appKey: appkey, | ||
salt: salt, | ||
sign: sign, | ||
signType: 'v3', | ||
curtime: curtime, | ||
}, | ||
}); | ||
if (res.ok) { | ||
let result = res.data; | ||
|
||
if (result['isWord']) { | ||
// 后续在此处实现词典功能 | ||
if (result['translation']) { | ||
let target = ''; | ||
for (let i of result['translation']) { | ||
target += i + '\n'; | ||
} | ||
return target.trim(); | ||
} else { | ||
throw JSON.stringify(result); | ||
} | ||
} else { | ||
if (result['translation']) { | ||
let target = ''; | ||
for (let i of result['translation']) { | ||
target += i + '\n'; | ||
} | ||
return target.trim(); | ||
} else { | ||
throw JSON.stringify(result); | ||
} | ||
} | ||
} else { | ||
throw `Http Request Error\nHttp Status: ${res.status}\n${JSON.stringify(res.data)}`; | ||
} | ||
} | ||
|
||
function truncate(q) { | ||
var len = q.length; | ||
if (len <= 20) return q; | ||
return q.substring(0, 10) + len + q.substring(len - 10, len); | ||
} | ||
|
||
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,30 @@ | ||
export const info = { | ||
name: 'youdao', | ||
icon: 'logo/youdao.svg', | ||
}; | ||
|
||
export enum Language { | ||
auto = 'auto', | ||
zh_cn = 'zh-CHS', | ||
zh_tw = 'zh-CHT', | ||
yue = 'yue', | ||
en = 'en', | ||
ja = 'jp', | ||
ko = 'kor', | ||
fr = 'fra', | ||
es = 'spa', | ||
ru = 'ru', | ||
de = 'de', | ||
it = 'it', | ||
tr = 'tr', | ||
pt = 'pt', | ||
pt_br = 'pt', | ||
vi = 'vie', | ||
id = 'id', | ||
th = 'th', | ||
ms = 'may', | ||
ar = 'ar', | ||
hi = 'hi', | ||
mn_mo = 'mn', | ||
km = 'km', | ||
} |
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