feat: add niutrans api
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
|||||||
OPT_TRANS_DEEPL,
|
OPT_TRANS_DEEPL,
|
||||||
OPT_TRANS_DEEPLFREE,
|
OPT_TRANS_DEEPLFREE,
|
||||||
OPT_TRANS_DEEPLX,
|
OPT_TRANS_DEEPLX,
|
||||||
|
OPT_TRANS_NIUTRANS,
|
||||||
OPT_TRANS_BAIDU,
|
OPT_TRANS_BAIDU,
|
||||||
OPT_TRANS_TENCENT,
|
OPT_TRANS_TENCENT,
|
||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
@@ -219,6 +220,14 @@ export const apiTranslate = async ({
|
|||||||
trText = res.data;
|
trText = res.data;
|
||||||
isSame = to === res.source_lang;
|
isSame = to === res.source_lang;
|
||||||
break;
|
break;
|
||||||
|
case OPT_TRANS_NIUTRANS:
|
||||||
|
const json = JSON.parse(res);
|
||||||
|
if (json.error_msg) {
|
||||||
|
throw new Error(json.error_msg);
|
||||||
|
}
|
||||||
|
trText = json.tgt_text;
|
||||||
|
isSame = to === json.from;
|
||||||
|
break;
|
||||||
case OPT_TRANS_BAIDU:
|
case OPT_TRANS_BAIDU:
|
||||||
// trText = res.trans_result?.data.map((item) => item.dst).join(" ");
|
// trText = res.trans_result?.data.map((item) => item.dst).join(" ");
|
||||||
// isSame = res.trans_result?.to === res.trans_result?.from;
|
// isSame = res.trans_result?.to === res.trans_result?.from;
|
||||||
|
|||||||
@@ -819,4 +819,8 @@ export const I18N = {
|
|||||||
zh: `网页修复选择器`,
|
zh: `网页修复选择器`,
|
||||||
en: `Fixer Selector`,
|
en: `Fixer Selector`,
|
||||||
},
|
},
|
||||||
|
reg_niutrans: {
|
||||||
|
zh: `获取小牛翻译密钥`,
|
||||||
|
en: `Get NiuTrans APIKey`,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -90,6 +90,8 @@ export const URL_BAIDU_TRANSAPI = "https://fanyi.baidu.com/transapi";
|
|||||||
export const URL_BAIDU_TRANSAPI_V2 = "https://fanyi.baidu.com/v2transapi";
|
export const URL_BAIDU_TRANSAPI_V2 = "https://fanyi.baidu.com/v2transapi";
|
||||||
export const URL_DEEPLFREE_TRAN = "https://www2.deepl.com/jsonrpc";
|
export const URL_DEEPLFREE_TRAN = "https://www2.deepl.com/jsonrpc";
|
||||||
export const URL_TENCENT_TRANSMART = "https://transmart.qq.com/api/imt";
|
export const URL_TENCENT_TRANSMART = "https://transmart.qq.com/api/imt";
|
||||||
|
export const URL_NIUTRANS_REG =
|
||||||
|
"https://niutrans.com/login?active=3&userSource=kiss-translator";
|
||||||
|
|
||||||
export const DEFAULT_USER_AGENT =
|
export const DEFAULT_USER_AGENT =
|
||||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
|
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
|
||||||
@@ -99,6 +101,7 @@ export const OPT_TRANS_MICROSOFT = "Microsoft";
|
|||||||
export const OPT_TRANS_DEEPL = "DeepL";
|
export const OPT_TRANS_DEEPL = "DeepL";
|
||||||
export const OPT_TRANS_DEEPLX = "DeepLX";
|
export const OPT_TRANS_DEEPLX = "DeepLX";
|
||||||
export const OPT_TRANS_DEEPLFREE = "DeepLFree";
|
export const OPT_TRANS_DEEPLFREE = "DeepLFree";
|
||||||
|
export const OPT_TRANS_NIUTRANS = "NiuTrans";
|
||||||
export const OPT_TRANS_BAIDU = "Baidu";
|
export const OPT_TRANS_BAIDU = "Baidu";
|
||||||
export const OPT_TRANS_TENCENT = "Tencent";
|
export const OPT_TRANS_TENCENT = "Tencent";
|
||||||
export const OPT_TRANS_OPENAI = "OpenAI";
|
export const OPT_TRANS_OPENAI = "OpenAI";
|
||||||
@@ -117,6 +120,7 @@ export const OPT_TRANS_ALL = [
|
|||||||
OPT_TRANS_DEEPL,
|
OPT_TRANS_DEEPL,
|
||||||
OPT_TRANS_DEEPLFREE,
|
OPT_TRANS_DEEPLFREE,
|
||||||
OPT_TRANS_DEEPLX,
|
OPT_TRANS_DEEPLX,
|
||||||
|
OPT_TRANS_NIUTRANS,
|
||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
OPT_TRANS_GEMINI,
|
OPT_TRANS_GEMINI,
|
||||||
OPT_TRANS_CLOUDFLAREAI,
|
OPT_TRANS_CLOUDFLAREAI,
|
||||||
@@ -193,6 +197,12 @@ export const OPT_LANGS_SPECIAL = {
|
|||||||
["zh-CN", "ZH"],
|
["zh-CN", "ZH"],
|
||||||
["zh-TW", "ZH"],
|
["zh-TW", "ZH"],
|
||||||
]),
|
]),
|
||||||
|
[OPT_TRANS_NIUTRANS]: new Map([
|
||||||
|
...OPT_LANGS_FROM.map(([key]) => [key, key]),
|
||||||
|
["auto", "auto"],
|
||||||
|
["zh-CN", "zh"],
|
||||||
|
["zh-TW", "cht"],
|
||||||
|
]),
|
||||||
[OPT_TRANS_BAIDU]: new Map([
|
[OPT_TRANS_BAIDU]: new Map([
|
||||||
...OPT_LANGS_FROM.map(([key]) => [key, key]),
|
...OPT_LANGS_FROM.map(([key]) => [key, key]),
|
||||||
["zh-CN", "zh"],
|
["zh-CN", "zh"],
|
||||||
@@ -469,6 +479,14 @@ export const DEFAULT_TRANS_APIS = {
|
|||||||
fetchLimit: 1,
|
fetchLimit: 1,
|
||||||
fetchInterval: 500,
|
fetchInterval: 500,
|
||||||
},
|
},
|
||||||
|
[OPT_TRANS_NIUTRANS]: {
|
||||||
|
url: "https://api.niutrans.com/NiuTransServer/translation",
|
||||||
|
key: "",
|
||||||
|
dictNo: "",
|
||||||
|
memoryNo: "",
|
||||||
|
fetchLimit: DEFAULT_FETCH_LIMIT,
|
||||||
|
fetchInterval: DEFAULT_FETCH_INTERVAL,
|
||||||
|
},
|
||||||
[OPT_TRANS_OPENAI]: {
|
[OPT_TRANS_OPENAI]: {
|
||||||
url: "https://api.openai.com/v1/chat/completions",
|
url: "https://api.openai.com/v1/chat/completions",
|
||||||
key: "",
|
key: "",
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ export function useApi(translator) {
|
|||||||
|
|
||||||
const updateApi = useCallback(
|
const updateApi = useCallback(
|
||||||
async (obj) => {
|
async (obj) => {
|
||||||
const api = transApis[translator] || {};
|
const api = {
|
||||||
|
...DEFAULT_TRANS_APIS[translator],
|
||||||
|
...(transApis[translator] || {}),
|
||||||
|
};
|
||||||
Object.assign(transApis, { [translator]: { ...api, ...obj } });
|
Object.assign(transApis, { [translator]: { ...api, ...obj } });
|
||||||
await updateSetting({ transApis });
|
await updateSetting({ transApis });
|
||||||
},
|
},
|
||||||
@@ -20,5 +23,12 @@ export function useApi(translator) {
|
|||||||
await updateSetting({ transApis });
|
await updateSetting({ transApis });
|
||||||
}, [translator, transApis, updateSetting]);
|
}, [translator, transApis, updateSetting]);
|
||||||
|
|
||||||
return { api: transApis[translator] || {}, updateApi, resetApi };
|
return {
|
||||||
|
api: {
|
||||||
|
...DEFAULT_TRANS_APIS[translator],
|
||||||
|
...(transApis[translator] || {}),
|
||||||
|
},
|
||||||
|
updateApi,
|
||||||
|
resetApi,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,8 +39,10 @@ export function useTranslate(q, rule, setting) {
|
|||||||
text: q,
|
text: q,
|
||||||
fromLang,
|
fromLang,
|
||||||
toLang,
|
toLang,
|
||||||
apiSetting:
|
apiSetting: {
|
||||||
setting.transApis?.[translator] || DEFAULT_TRANS_APIS[translator],
|
...DEFAULT_TRANS_APIS[translator],
|
||||||
|
...(setting.transApis[translator] || {}),
|
||||||
|
},
|
||||||
});
|
});
|
||||||
setText(trText);
|
setText(trText);
|
||||||
setSamelang(isSame);
|
setSamelang(isSame);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
OPT_TRANS_DEEPL,
|
OPT_TRANS_DEEPL,
|
||||||
OPT_TRANS_DEEPLFREE,
|
OPT_TRANS_DEEPLFREE,
|
||||||
OPT_TRANS_DEEPLX,
|
OPT_TRANS_DEEPLX,
|
||||||
|
OPT_TRANS_NIUTRANS,
|
||||||
OPT_TRANS_BAIDU,
|
OPT_TRANS_BAIDU,
|
||||||
OPT_TRANS_TENCENT,
|
OPT_TRANS_TENCENT,
|
||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
@@ -145,6 +146,27 @@ const genDeeplX = ({ text, from, to, url, key }) => {
|
|||||||
return [url, init];
|
return [url, init];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const genNiuTrans = ({ text, from, to, url, key, dictNo, memoryNo }) => {
|
||||||
|
const data = {
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
apikey: key,
|
||||||
|
src_text: text,
|
||||||
|
dictNo,
|
||||||
|
memoryNo,
|
||||||
|
};
|
||||||
|
|
||||||
|
const init = {
|
||||||
|
headers: {
|
||||||
|
"Content-type": "application/json",
|
||||||
|
},
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
};
|
||||||
|
|
||||||
|
return [url, init];
|
||||||
|
};
|
||||||
|
|
||||||
const genTencent = ({ text, from, to }) => {
|
const genTencent = ({ text, from, to }) => {
|
||||||
const data = {
|
const data = {
|
||||||
header: {
|
header: {
|
||||||
@@ -287,6 +309,7 @@ export const newTransReq = ({ translator, text, from, to }, apiSetting) => {
|
|||||||
case OPT_TRANS_OPENAI:
|
case OPT_TRANS_OPENAI:
|
||||||
case OPT_TRANS_GEMINI:
|
case OPT_TRANS_GEMINI:
|
||||||
case OPT_TRANS_CLOUDFLAREAI:
|
case OPT_TRANS_CLOUDFLAREAI:
|
||||||
|
case OPT_TRANS_NIUTRANS:
|
||||||
args.key = keyPick(translator, args.key);
|
args.key = keyPick(translator, args.key);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -303,6 +326,8 @@ export const newTransReq = ({ translator, text, from, to }, apiSetting) => {
|
|||||||
return genDeeplFree(args);
|
return genDeeplFree(args);
|
||||||
case OPT_TRANS_DEEPLX:
|
case OPT_TRANS_DEEPLX:
|
||||||
return genDeeplX(args);
|
return genDeeplX(args);
|
||||||
|
case OPT_TRANS_NIUTRANS:
|
||||||
|
return genNiuTrans(args);
|
||||||
case OPT_TRANS_BAIDU:
|
case OPT_TRANS_BAIDU:
|
||||||
return genBaidu(args);
|
return genBaidu(args);
|
||||||
case OPT_TRANS_TENCENT:
|
case OPT_TRANS_TENCENT:
|
||||||
|
|||||||
@@ -13,7 +13,9 @@ import {
|
|||||||
OPT_TRANS_GEMINI,
|
OPT_TRANS_GEMINI,
|
||||||
OPT_TRANS_CLOUDFLAREAI,
|
OPT_TRANS_CLOUDFLAREAI,
|
||||||
OPT_TRANS_CUSTOMIZE,
|
OPT_TRANS_CUSTOMIZE,
|
||||||
|
OPT_TRANS_NIUTRANS,
|
||||||
URL_KISS_PROXY,
|
URL_KISS_PROXY,
|
||||||
|
URL_NIUTRANS_REG,
|
||||||
DEFAULT_FETCH_LIMIT,
|
DEFAULT_FETCH_LIMIT,
|
||||||
DEFAULT_FETCH_INTERVAL,
|
DEFAULT_FETCH_INTERVAL,
|
||||||
} from "../../config";
|
} from "../../config";
|
||||||
@@ -62,14 +64,24 @@ function TestButton({ translator, api }) {
|
|||||||
alert.error(
|
alert.error(
|
||||||
<>
|
<>
|
||||||
<div>{i18n("test_failed")}</div>
|
<div>{i18n("test_failed")}</div>
|
||||||
<pre
|
{msg === err.message ? (
|
||||||
style={{
|
<div
|
||||||
maxWidth: 400,
|
style={{
|
||||||
overflow: "auto",
|
maxWidth: 400,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{msg}
|
{msg}
|
||||||
</pre>
|
</div>
|
||||||
|
) : (
|
||||||
|
<pre
|
||||||
|
style={{
|
||||||
|
maxWidth: 400,
|
||||||
|
overflow: "auto",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{msg}
|
||||||
|
</pre>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -98,6 +110,8 @@ function ApiFields({ translator }) {
|
|||||||
prompt = "",
|
prompt = "",
|
||||||
fetchLimit = DEFAULT_FETCH_LIMIT,
|
fetchLimit = DEFAULT_FETCH_LIMIT,
|
||||||
fetchInterval = DEFAULT_FETCH_INTERVAL,
|
fetchInterval = DEFAULT_FETCH_INTERVAL,
|
||||||
|
dictNo = "",
|
||||||
|
memoryNo = "",
|
||||||
} = api;
|
} = api;
|
||||||
|
|
||||||
const handleChange = (e) => {
|
const handleChange = (e) => {
|
||||||
@@ -128,8 +142,23 @@ function ApiFields({ translator }) {
|
|||||||
OPT_TRANS_OPENAI,
|
OPT_TRANS_OPENAI,
|
||||||
OPT_TRANS_GEMINI,
|
OPT_TRANS_GEMINI,
|
||||||
OPT_TRANS_CLOUDFLAREAI,
|
OPT_TRANS_CLOUDFLAREAI,
|
||||||
|
OPT_TRANS_NIUTRANS,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const keyHelper =
|
||||||
|
translator === OPT_TRANS_NIUTRANS ? (
|
||||||
|
<>
|
||||||
|
{i18n("mulkeys_help")}
|
||||||
|
<Link href={URL_NIUTRANS_REG} target="_blank">
|
||||||
|
{i18n("reg_niutrans")}
|
||||||
|
</Link>
|
||||||
|
</>
|
||||||
|
) : mulkeysTranslators.includes(translator) ? (
|
||||||
|
i18n("mulkeys_help")
|
||||||
|
) : (
|
||||||
|
""
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack spacing={3}>
|
<Stack spacing={3}>
|
||||||
{!buildinTranslators.includes(translator) && (
|
{!buildinTranslators.includes(translator) && (
|
||||||
@@ -148,11 +177,7 @@ function ApiFields({ translator }) {
|
|||||||
value={key}
|
value={key}
|
||||||
onChange={handleChange}
|
onChange={handleChange}
|
||||||
multiline={mulkeysTranslators.includes(translator)}
|
multiline={mulkeysTranslators.includes(translator)}
|
||||||
helperText={
|
helperText={keyHelper}
|
||||||
mulkeysTranslators.includes(translator)
|
|
||||||
? i18n("mulkeys_help")
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
@@ -177,6 +202,25 @@ function ApiFields({ translator }) {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{translator === OPT_TRANS_NIUTRANS && (
|
||||||
|
<>
|
||||||
|
<TextField
|
||||||
|
size="small"
|
||||||
|
label={"DictNo"}
|
||||||
|
name="dictNo"
|
||||||
|
value={dictNo}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
|
<TextField
|
||||||
|
size="small"
|
||||||
|
label={"MemoryNo"}
|
||||||
|
name="memoryNo"
|
||||||
|
value={memoryNo}
|
||||||
|
onChange={handleChange}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
|
||||||
<TextField
|
<TextField
|
||||||
size="small"
|
size="small"
|
||||||
label={i18n("fetch_limit")}
|
label={i18n("fetch_limit")}
|
||||||
|
|||||||
Reference in New Issue
Block a user