Skip to content

Commit

Permalink
feat: add niutrans api
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabe Yuan committed Apr 12, 2024
1 parent d6f7aad commit f00e8ff
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/apis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
OPT_TRANS_DEEPL,
OPT_TRANS_DEEPLFREE,
OPT_TRANS_DEEPLX,
OPT_TRANS_NIUTRANS,
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
OPT_TRANS_OPENAI,
Expand Down Expand Up @@ -219,6 +220,14 @@ export const apiTranslate = async ({
trText = res.data;
isSame = to === res.source_lang;
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:
// trText = res.trans_result?.data.map((item) => item.dst).join(" ");
// isSame = res.trans_result?.to === res.trans_result?.from;
Expand Down
4 changes: 4 additions & 0 deletions src/config/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,8 @@ export const I18N = {
zh: `网页修复选择器`,
en: `Fixer Selector`,
},
reg_niutrans: {
zh: `获取小牛翻译密钥`,
en: `Get NiuTrans APIKey`,
},
};
18 changes: 18 additions & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_DEEPLFREE_TRAN = "https://www2.deepl.com/jsonrpc";
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 =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36";
Expand All @@ -99,6 +101,7 @@ export const OPT_TRANS_MICROSOFT = "Microsoft";
export const OPT_TRANS_DEEPL = "DeepL";
export const OPT_TRANS_DEEPLX = "DeepLX";
export const OPT_TRANS_DEEPLFREE = "DeepLFree";
export const OPT_TRANS_NIUTRANS = "NiuTrans";
export const OPT_TRANS_BAIDU = "Baidu";
export const OPT_TRANS_TENCENT = "Tencent";
export const OPT_TRANS_OPENAI = "OpenAI";
Expand All @@ -117,6 +120,7 @@ export const OPT_TRANS_ALL = [
OPT_TRANS_DEEPL,
OPT_TRANS_DEEPLFREE,
OPT_TRANS_DEEPLX,
OPT_TRANS_NIUTRANS,
OPT_TRANS_OPENAI,
OPT_TRANS_GEMINI,
OPT_TRANS_CLOUDFLAREAI,
Expand Down Expand Up @@ -193,6 +197,12 @@ export const OPT_LANGS_SPECIAL = {
["zh-CN", "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_LANGS_FROM.map(([key]) => [key, key]),
["zh-CN", "zh"],
Expand Down Expand Up @@ -469,6 +479,14 @@ export const DEFAULT_TRANS_APIS = {
fetchLimit: 1,
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]: {
url: "https://api.openai.com/v1/chat/completions",
key: "",
Expand Down
14 changes: 12 additions & 2 deletions src/hooks/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export function useApi(translator) {

const updateApi = useCallback(
async (obj) => {
const api = transApis[translator] || {};
const api = {
...DEFAULT_TRANS_APIS[translator],
...(transApis[translator] || {}),
};
Object.assign(transApis, { [translator]: { ...api, ...obj } });
await updateSetting({ transApis });
},
Expand All @@ -20,5 +23,12 @@ export function useApi(translator) {
await updateSetting({ transApis });
}, [translator, transApis, updateSetting]);

return { api: transApis[translator] || {}, updateApi, resetApi };
return {
api: {
...DEFAULT_TRANS_APIS[translator],
...(transApis[translator] || {}),
},
updateApi,
resetApi,
};
}
6 changes: 4 additions & 2 deletions src/hooks/Translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export function useTranslate(q, rule, setting) {
text: q,
fromLang,
toLang,
apiSetting:
setting.transApis?.[translator] || DEFAULT_TRANS_APIS[translator],
apiSetting: {
...DEFAULT_TRANS_APIS[translator],
...(setting.transApis[translator] || {}),
},
});
setText(trText);
setSamelang(isSame);
Expand Down
25 changes: 25 additions & 0 deletions src/libs/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
OPT_TRANS_DEEPL,
OPT_TRANS_DEEPLFREE,
OPT_TRANS_DEEPLX,
OPT_TRANS_NIUTRANS,
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
OPT_TRANS_OPENAI,
Expand Down Expand Up @@ -145,6 +146,27 @@ const genDeeplX = ({ text, from, to, url, key }) => {
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 data = {
header: {
Expand Down Expand Up @@ -287,6 +309,7 @@ export const newTransReq = ({ translator, text, from, to }, apiSetting) => {
case OPT_TRANS_OPENAI:
case OPT_TRANS_GEMINI:
case OPT_TRANS_CLOUDFLAREAI:
case OPT_TRANS_NIUTRANS:
args.key = keyPick(translator, args.key);
break;
default:
Expand All @@ -303,6 +326,8 @@ export const newTransReq = ({ translator, text, from, to }, apiSetting) => {
return genDeeplFree(args);
case OPT_TRANS_DEEPLX:
return genDeeplX(args);
case OPT_TRANS_NIUTRANS:
return genNiuTrans(args);
case OPT_TRANS_BAIDU:
return genBaidu(args);
case OPT_TRANS_TENCENT:
Expand Down
70 changes: 57 additions & 13 deletions src/views/Options/Apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
OPT_TRANS_GEMINI,
OPT_TRANS_CLOUDFLAREAI,
OPT_TRANS_CUSTOMIZE,
OPT_TRANS_NIUTRANS,
URL_KISS_PROXY,
URL_NIUTRANS_REG,
DEFAULT_FETCH_LIMIT,
DEFAULT_FETCH_INTERVAL,
} from "../../config";
Expand Down Expand Up @@ -62,14 +64,24 @@ function TestButton({ translator, api }) {
alert.error(
<>
<div>{i18n("test_failed")}</div>
<pre
style={{
maxWidth: 400,
overflow: "auto",
}}
>
{msg}
</pre>
{msg === err.message ? (
<div
style={{
maxWidth: 400,
}}
>
{msg}
</div>
) : (
<pre
style={{
maxWidth: 400,
overflow: "auto",
}}
>
{msg}
</pre>
)}
</>
);
} finally {
Expand Down Expand Up @@ -98,6 +110,8 @@ function ApiFields({ translator }) {
prompt = "",
fetchLimit = DEFAULT_FETCH_LIMIT,
fetchInterval = DEFAULT_FETCH_INTERVAL,
dictNo = "",
memoryNo = "",
} = api;

const handleChange = (e) => {
Expand Down Expand Up @@ -128,8 +142,23 @@ function ApiFields({ translator }) {
OPT_TRANS_OPENAI,
OPT_TRANS_GEMINI,
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 (
<Stack spacing={3}>
{!buildinTranslators.includes(translator) && (
Expand All @@ -148,11 +177,7 @@ function ApiFields({ translator }) {
value={key}
onChange={handleChange}
multiline={mulkeysTranslators.includes(translator)}
helperText={
mulkeysTranslators.includes(translator)
? i18n("mulkeys_help")
: ""
}
helperText={keyHelper}
/>
</>
)}
Expand All @@ -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
size="small"
label={i18n("fetch_limit")}
Expand Down

0 comments on commit f00e8ff

Please sign in to comment.