Skip to content

Commit

Permalink
feat: Add Custom URL for Google Translate
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Sep 6, 2023
1 parent cdbaea9 commit bc5b372
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/i18n/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
"api": "Auth Key",
"deeplx": "DeepLX"
},
"google": { "title": "Google" },
"google": { "title": "Google", "custom_url": "Custom URL" },
"bing": { "title": "Bing" },
"openai": {
"title": "OpenAI",
Expand Down
3 changes: 2 additions & 1 deletion src/i18n/locales/pt_BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@
"deeplx": "DeepLX"
},
"google": {
"title": "Google"
"title": "Google",
"custom_url": "URL personalizado"
},
"bing": {
"title": "Bing"
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/locales/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
"api": "Auth Key",
"deeplx": "DeepLX"
},
"google": { "title": "谷歌翻译" },
"google": { "title": "谷歌翻译", "custom_url": "自定义 URL" },
"bing": { "title": "必应翻译" },
"openai_summary": { "title": "OpenAI 总结" },
"openai_polish": { "title": "OpenAI 润色" },
Expand Down
78 changes: 61 additions & 17 deletions src/services/translate/google/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,70 @@
import { Input, Button } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import { Button } from '@nextui-org/react';
import React from 'react';
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(
'google',
{
custom_url: 'https://translate.google.com',
},
{ sync: false }
);
const [isLoading, setIsLoading] = useState(false);

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

return (
<>
<div>{t('services.no_need')}</div>
<div>
<Button
fullWidth
color='primary'
onPress={() => {
updateServiceList('google');
onClose();
}}
>
{t('common.save')}
</Button>
</div>
</>
config !== null && (
<>
<Toaster />
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.translate.google.custom_url')}</h3>
<Input
value={config['custom_url']}
variant='bordered'
className='max-w-[50%]'
onValueChange={(value) => {
setConfig({
...config,
custom_url: 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('google');
onClose();
},
(e) => {
setIsLoading(false);
toast.error(t('config.service.test_failed') + e.toString(), { style: toastStyle });
}
);
}}
>
{t('common.save')}
</Button>
</div>
</>
)
);
}
20 changes: 18 additions & 2 deletions src/services/translate/google/index.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import { fetch } from '@tauri-apps/api/http';

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

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

let { custom_url } = translateConfig;

if (custom_url === undefined || custom_url === '') {
custom_url = 'https://translate.google.com';
}
if (!custom_url.startsWith('http')) {
custom_url = 'https://' + custom_url;
}

let res = await fetch(
`https://translate.google.com/translate_a/single?dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t`,
`${custom_url}/translate_a/single?dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t`,
{
method: 'GET',
headers: { 'content-type': 'application/json' },
Expand Down

0 comments on commit bc5b372

Please sign in to comment.