Skip to content

Commit

Permalink
feat: Support Http for NiuTrans
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Sep 6, 2023
1 parent bc5b372 commit 6ecc04f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/i18n/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@
},
"niutrans": {
"title": "NiuTrans",
"https": "Use Https",
"apikey": "API Key"
},
"youdao": {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/pt_BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
},
"niutrans": {
"title": "NiuTrans",
"https": "Usando Https",
"apikey": "Chave de API"
},
"youdao": {
Expand Down
1 change: 1 addition & 0 deletions src/i18n/locales/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
},
"niutrans": {
"title": "小牛翻译",
"https": "使用 Https",
"apikey": "API Key"
},
"youdao": {
Expand Down
12 changes: 11 additions & 1 deletion src/services/translate/niutrans/Config.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Input, Button } from '@nextui-org/react';
import { Input, Button, Switch } from '@nextui-org/react';
import toast, { Toaster } from 'react-hot-toast';
import { useTranslation } from 'react-i18next';
import { open } from '@tauri-apps/api/shell';
Expand All @@ -14,6 +14,7 @@ export function Config(props) {
const [config, setConfig] = useConfig(
'niutrans',
{
https: true,
apikey: '',
},
{ sync: false }
Expand All @@ -37,6 +38,15 @@ export function Config(props) {
{t('services.help')}
</Button>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.translate.niutrans.https')}</h3>
<Switch
isSelected={config['https'] ?? true}
onValueChange={(v) => {
setConfig({ ...config, https: v });
}}
/>
</div>
<div className={'config-item'}>
<h3 className='my-auto'>{t('services.translate.niutrans.apikey')}</h3>
<Input
Expand Down
5 changes: 3 additions & 2 deletions src/services/translate/niutrans/index.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fetch, Body } from '@tauri-apps/api/http';
import { store } from '../../../utils/store';
import { http } from '@tauri-apps/api';

export async function translate(text, from, to, options = {}) {
const { config } = options;
Expand All @@ -9,9 +10,9 @@ export async function translate(text, from, to, options = {}) {
translateConfig = config;
}

const { apikey } = translateConfig;
const { https, apikey } = translateConfig;

const url = `https://api.niutrans.com/NiuTransServer/translation`;
const url = `${https ? 'https' : 'http'}://api.niutrans.com/NiuTransServer/translation`;

let res = await fetch(url, {
method: 'POST',
Expand Down

0 comments on commit 6ecc04f

Please sign in to comment.