Skip to content

Commit

Permalink
feat: support multiple instance for translate service
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Jun 23, 2024
1 parent dfad857 commit 4e78b50
Show file tree
Hide file tree
Showing 31 changed files with 427 additions and 192 deletions.
7 changes: 4 additions & 3 deletions src/hooks/useConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const useConfig = (key, defaultValue, options = {}) => {
debounce((v) => {
store.set(key, v);
store.save();
emit(`${key}_changed`, v);
let eventKey = key.replaceAll('.', '_').replaceAll('@', ':');
emit(`${eventKey}_changed`, v);
}),
[]
);
Expand Down Expand Up @@ -44,8 +45,8 @@ export const useConfig = (key, defaultValue, options = {}) => {
// 初始化
useEffect(() => {
syncToState(null);
if (key.includes('[')) return;
const unlisten = listen(`${key}_changed`, (e) => {
const eventKey = key.replaceAll('.', '_').replaceAll('@', ':');
const unlisten = listen(`${eventKey}_changed`, (e) => {
syncToState(e.payload);
});
return () => {
Expand Down
29 changes: 25 additions & 4 deletions src/services/translate/alibaba/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance';
import { Input, Button } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
Expand All @@ -10,18 +11,19 @@ import { translate } from './index';
import { Language } from './index';

export function Config(props) {
const { updateServiceList, onClose } = props;
const { instanceKey, updateServiceList, onClose } = props;
const { t } = useTranslation();
const [config, setConfig] = useConfig(
'alibaba',
instanceKey,
{
[INSTANCE_NAME_CONFIG_KEY]: t('services.translate.alibaba.title'),
accesskey_id: '',
accesskey_secret: '',
},
{ sync: false }
);
const [isLoading, setIsLoading] = useState(false);

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

return (
Expand All @@ -34,7 +36,7 @@ export function Config(props) {
() => {
setIsLoading(false);
setConfig(config, true);
updateServiceList('alibaba');
updateServiceList(instanceKey);
onClose();
},
(e) => {
Expand All @@ -45,6 +47,25 @@ export function Config(props) {
}}
>
<Toaster />
<div className='config-item'>
<Input
label={t('services.instance_name')}
labelPlacement='outside-left'
value={config[INSTANCE_NAME_CONFIG_KEY]}
variant='bordered'
classNames={{
base: 'justify-between',
label: 'text-[length:--nextui-font-size-medium]',
mainWrapper: 'max-w-[50%]',
}}
onValueChange={(value) => {
setConfig({
...config,
[INSTANCE_NAME_CONFIG_KEY]: value,
});
}}
/>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.help')}</h3>
<Button
Expand Down
8 changes: 1 addition & 7 deletions src/services/translate/alibaba/index.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { fetch } from '@tauri-apps/api/http';
import { store } from '../../../utils/store';
import HmacSHA1 from 'crypto-js/hmac-sha1';
import base64 from 'crypto-js/enc-base64';

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

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

const { accesskey_id, accesskey_secret } = translateConfig;
const { accesskey_id, accesskey_secret } = config;

function getRandomNumber() {
const rand = Math.floor(Math.random() * 99999) + 100000;
Expand Down
29 changes: 25 additions & 4 deletions src/services/translate/baidu/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance';
import { Input, Button } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
Expand All @@ -10,18 +11,19 @@ import { translate } from './index';
import { Language } from './index';

export function Config(props) {
const { updateServiceList, onClose } = props;
const { instanceKey, updateServiceList, onClose } = props;
const { t } = useTranslation();
const [config, setConfig] = useConfig(
'baidu',
instanceKey,
{
[INSTANCE_NAME_CONFIG_KEY]: t('services.translate.baidu.title'),
appid: '',
secret: '',
},
{ sync: false }
);
const [isLoading, setIsLoading] = useState(false);

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

return (
Expand All @@ -34,7 +36,7 @@ export function Config(props) {
() => {
setIsLoading(false);
setConfig(config, true);
updateServiceList('baidu');
updateServiceList(instanceKey);
onClose();
},
(e) => {
Expand All @@ -45,6 +47,25 @@ export function Config(props) {
}}
>
<Toaster />
<div className='config-item'>
<Input
label={t('services.instance_name')}
labelPlacement='outside-left'
value={config[INSTANCE_NAME_CONFIG_KEY]}
variant='bordered'
classNames={{
base: 'justify-between',
label: 'text-[length:--nextui-font-size-medium]',
mainWrapper: 'max-w-[50%]',
}}
onValueChange={(value) => {
setConfig({
...config,
[INSTANCE_NAME_CONFIG_KEY]: value,
});
}}
/>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.help')}</h3>
<Button
Expand Down
8 changes: 1 addition & 7 deletions src/services/translate/baidu/index.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { fetch } from '@tauri-apps/api/http';
import { store } from '../../../utils/store';
import { nanoid } from 'nanoid';
import md5 from 'md5';

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

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

const { appid, secret } = translateConfig;
const { appid, secret } = config;

const url = 'https://fanyi-api.baidu.com/api/trans/vip/translate';

Expand Down
30 changes: 26 additions & 4 deletions src/services/translate/baidu_field/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance';
import { DropdownTrigger } from '@nextui-org/react';
import { Input, Button } from '@nextui-org/react';
import { DropdownMenu } from '@nextui-org/react';
Expand All @@ -14,10 +15,12 @@ import { translate } from './index';
import { Language } from './index';

export function Config(props) {
const { updateServiceList, onClose } = props;
const { instanceKey, updateServiceList, onClose } = props;
const { t } = useTranslation();
const [config, setConfig] = useConfig(
'baidu_field',
instanceKey,
{
[INSTANCE_NAME_CONFIG_KEY]: t('services.translate.baidu_field.title'),
appid: '',
secret: '',
field: 'it',
Expand All @@ -38,7 +41,7 @@ export function Config(props) {
'law',
'contract',
];
const { t } = useTranslation();

const toastStyle = useToastStyle();

return (
Expand All @@ -51,7 +54,7 @@ export function Config(props) {
() => {
setIsLoading(false);
setConfig(config, true);
updateServiceList('baidu_field');
updateServiceList(instanceKey);
onClose();
},
(e) => {
Expand All @@ -62,6 +65,25 @@ export function Config(props) {
}}
>
<Toaster />
<div className='config-item'>
<Input
label={t('services.instance_name')}
labelPlacement='outside-left'
value={config[INSTANCE_NAME_CONFIG_KEY]}
variant='bordered'
classNames={{
base: 'justify-between',
label: 'text-[length:--nextui-font-size-medium]',
mainWrapper: 'max-w-[50%]',
}}
onValueChange={(value) => {
setConfig({
...config,
[INSTANCE_NAME_CONFIG_KEY]: value,
});
}}
/>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.help')}</h3>
<Button
Expand Down
8 changes: 1 addition & 7 deletions src/services/translate/baidu_field/index.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { fetch } from '@tauri-apps/api/http';
import { store } from '../../../utils/store';
import { nanoid } from 'nanoid';
import md5 from 'md5';

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

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

const { appid, secret, field } = translateConfig;
const { appid, secret, field } = config;

const url = 'https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate';

Expand Down
29 changes: 25 additions & 4 deletions src/services/translate/caiyun/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { INSTANCE_NAME_CONFIG_KEY } from '../../../utils/service_instance';
import { Input, Button } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
Expand All @@ -10,17 +11,18 @@ import { translate } from './index';
import { Language } from './index';

export function Config(props) {
const { updateServiceList, onClose } = props;
const { instanceKey, updateServiceList, onClose } = props;
const { t } = useTranslation();
const [config, setConfig] = useConfig(
'caiyun',
instanceKey,
{
[INSTANCE_NAME_CONFIG_KEY]: t('services.translate.caiyun.title'),
token: '',
},
{ sync: false }
);
const [isLoading, setIsLoading] = useState(false);

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

return (
Expand All @@ -33,7 +35,7 @@ export function Config(props) {
() => {
setIsLoading(false);
setConfig(config, true);
updateServiceList('caiyun');
updateServiceList(instanceKey);
onClose();
},
(e) => {
Expand All @@ -44,6 +46,25 @@ export function Config(props) {
}}
>
<Toaster />
<div className='config-item'>
<Input
label={t('services.instance_name')}
labelPlacement='outside-left'
value={config[INSTANCE_NAME_CONFIG_KEY]}
variant='bordered'
classNames={{
base: 'justify-between',
label: 'text-[length:--nextui-font-size-medium]',
mainWrapper: 'max-w-[50%]',
}}
onValueChange={(value) => {
setConfig({
...config,
[INSTANCE_NAME_CONFIG_KEY]: value,
});
}}
/>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.help')}</h3>
<Button
Expand Down
8 changes: 1 addition & 7 deletions src/services/translate/caiyun/index.jsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
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('caiyun')) ?? {};
if (config !== undefined) {
translateConfig = config;
}

const { token } = translateConfig;
const { token } = config;

const url = 'https://api.interpreter.caiyunai.com/v1/translator';

Expand Down
Loading

0 comments on commit 4e78b50

Please sign in to comment.