Compare commits

..

8 Commits

Author SHA1 Message Date
Gabe Yuan
72ccfc8aec v1.8.11 2024-05-23 20:06:48 +08:00
Gabe Yuan
d117c5dc10 feat: baidu dict can be disabled 2024-05-23 00:08:10 +08:00
Gabe Yuan
9312783f44 feat: lang detector can be selected 2024-05-22 23:33:30 +08:00
Gabe Yuan
e5b16ebfd3 Merge remote-tracking branch 'origin/master' into dev 2024-05-22 10:19:04 +08:00
Gabe Yuan
5d1d65c2d3 feat: the temperature and maxTokens of the openai can be configured 2024-05-21 23:15:46 +08:00
Gabe
9ca1309cec Merge pull request #126 from kebyn/master
fix: option translation
2024-05-21 20:02:53 +08:00
kebyn
a03afc05f5 fix: option translation 2024-05-21 08:56:09 +00:00
Gabe Yuan
0198963584 fix: deeplx: replace auto to blank string 2024-05-21 11:55:17 +08:00
18 changed files with 240 additions and 23 deletions

2
.env
View File

@@ -2,7 +2,7 @@ GENERATE_SOURCEMAP=false
REACT_APP_NAME=KISS Translator REACT_APP_NAME=KISS Translator
REACT_APP_NAME_CN=简约翻译 REACT_APP_NAME_CN=简约翻译
REACT_APP_VERSION=1.8.10 REACT_APP_VERSION=1.8.11
REACT_APP_HOMEPAGE=https://github.com/fishjar/kiss-translator REACT_APP_HOMEPAGE=https://github.com/fishjar/kiss-translator

View File

@@ -1,7 +1,7 @@
{ {
"name": "kiss-translator", "name": "kiss-translator",
"description": "A minimalist bilingual translation Extension & Greasemonkey Script", "description": "A minimalist bilingual translation Extension & Greasemonkey Script",
"version": "1.8.10", "version": "1.8.11",
"author": "Gabe<yugang2002@gmail.com>", "author": "Gabe<yugang2002@gmail.com>",
"private": true, "private": true,
"dependencies": { "dependencies": {

View File

@@ -2,7 +2,7 @@
"manifest_version": 2, "manifest_version": 2,
"name": "__MSG_app_name__", "name": "__MSG_app_name__",
"description": "__MSG_app_description__", "description": "__MSG_app_description__",
"version": "1.8.10", "version": "1.8.11",
"default_locale": "en", "default_locale": "en",
"author": "Gabe<yugang2002@gmail.com>", "author": "Gabe<yugang2002@gmail.com>",
"homepage_url": "https://github.com/fishjar/kiss-translator", "homepage_url": "https://github.com/fishjar/kiss-translator",

View File

@@ -2,7 +2,7 @@
"manifest_version": 3, "manifest_version": 3,
"name": "__MSG_app_name__", "name": "__MSG_app_name__",
"description": "__MSG_app_description__", "description": "__MSG_app_description__",
"version": "1.8.10", "version": "1.8.11",
"default_locale": "en", "default_locale": "en",
"author": "Gabe<yugang2002@gmail.com>", "author": "Gabe<yugang2002@gmail.com>",
"homepage_url": "https://github.com/fishjar/kiss-translator", "homepage_url": "https://github.com/fishjar/kiss-translator",

View File

@@ -24,6 +24,8 @@ import {
OPT_TRANS_CUSTOMIZE_5, OPT_TRANS_CUSTOMIZE_5,
URL_CACHE_TRAN, URL_CACHE_TRAN,
KV_SALT_SYNC, KV_SALT_SYNC,
URL_GOOGLE_TRAN,
URL_MICROSOFT_LANGDETECT,
URL_BAIDU_LANGDETECT, URL_BAIDU_LANGDETECT,
URL_BAIDU_SUGGEST, URL_BAIDU_SUGGEST,
URL_BAIDU_TTS, URL_BAIDU_TTS,
@@ -31,9 +33,11 @@ import {
URL_TENCENT_TRANSMART, URL_TENCENT_TRANSMART,
OPT_LANGS_TENCENT, OPT_LANGS_TENCENT,
OPT_LANGS_SPECIAL, OPT_LANGS_SPECIAL,
OPT_LANGS_MICROSOFT,
} from "../config"; } from "../config";
import { sha256 } from "../libs/utils"; import { sha256 } from "../libs/utils";
import interpreter from "../libs/interpreter"; import interpreter from "../libs/interpreter";
import { msAuth } from "../libs/auth";
/** /**
* 同步数据 * 同步数据
@@ -59,6 +63,52 @@ export const apiSyncData = async (url, key, data) =>
*/ */
export const apiFetch = (url) => fetchData(url); export const apiFetch = (url) => fetchData(url);
/**
* Google语言识别
* @param {*} text
* @returns
*/
export const apiGoogleLangdetect = async (text) => {
const params = {
client: "gtx",
dt: "t",
dj: 1,
ie: "UTF-8",
sl: "auto",
tl: "zh-CN",
q: text,
};
const input = `${URL_GOOGLE_TRAN}?${queryString.stringify(params)}`;
const res = await fetchData(input, {
headers: {
"Content-type": "application/json",
},
useCache: true,
});
return res.src;
};
/**
* Microsoft语言识别
* @param {*} text
* @returns
*/
export const apiMicrosoftLangdetect = async (text) => {
const [token] = await msAuth();
const res = await fetchData(URL_MICROSOFT_LANGDETECT, {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${token}`,
},
method: "POST",
body: JSON.stringify([{ Text: text }]),
useCache: true,
});
return OPT_LANGS_MICROSOFT.get(res[0].language) ?? res[0].language;
};
/** /**
* 百度语言识别 * 百度语言识别
* @param {*} text * @param {*} text

View File

@@ -185,7 +185,17 @@ const genTencent = ({ text, from, to }) => {
return [URL_TENCENT_TRANSMART, init]; return [URL_TENCENT_TRANSMART, init];
}; };
const genOpenAI = ({ text, from, to, url, key, prompt, model }) => { const genOpenAI = ({
text,
from,
to,
url,
key,
prompt,
model,
temperature,
maxTokens,
}) => {
prompt = prompt prompt = prompt
.replaceAll(INPUT_PLACE_FROM, from) .replaceAll(INPUT_PLACE_FROM, from)
.replaceAll(INPUT_PLACE_TO, to); .replaceAll(INPUT_PLACE_TO, to);
@@ -202,8 +212,8 @@ const genOpenAI = ({ text, from, to, url, key, prompt, model }) => {
content: text, content: text,
}, },
], ],
temperature: 0, temperature,
max_tokens: 256, max_tokens: maxTokens,
}; };
const init = { const init = {

View File

@@ -141,6 +141,7 @@ function showTransbox({
transApis, transApis,
darkMode, darkMode,
uiLang, uiLang,
langDetector,
}) { }) {
if (!tranboxSetting?.transOpen) { if (!tranboxSetting?.transOpen) {
return; return;
@@ -172,6 +173,7 @@ function showTransbox({
tranboxSetting={tranboxSetting} tranboxSetting={tranboxSetting}
transApis={transApis} transApis={transApis}
uiLang={uiLang} uiLang={uiLang}
langDetector={langDetector}
/> />
</CacheProvider> </CacheProvider>
</React.StrictMode> </React.StrictMode>

View File

@@ -656,7 +656,7 @@ export const I18N = {
}, },
use_simple_style: { use_simple_style: {
zh: `使用简洁界面`, zh: `使用简洁界面`,
en: `Click outside to close the pop-up window`, en: `Use a simple interface`,
}, },
show: { show: {
zh: `显示`, zh: `显示`,
@@ -930,4 +930,8 @@ export const I18N = {
zh: `翻译移除时运行,入参为: 翻译节点。`, zh: `翻译移除时运行,入参为: 翻译节点。`,
en: `Run when translation is removed, the input parameters are: translation node.`, en: `Run when translation is removed, the input parameters are: translation node.`,
}, },
english_dict: {
zh: `英文词典`,
en: `English Dictionary`,
},
}; };

View File

@@ -78,9 +78,16 @@ export const URL_RAW_PREFIX =
"https://raw.githubusercontent.com/fishjar/kiss-translator/master"; "https://raw.githubusercontent.com/fishjar/kiss-translator/master";
export const URL_CACHE_TRAN = `https://${APP_LCNAME}/translate`; export const URL_CACHE_TRAN = `https://${APP_LCNAME}/translate`;
// api.cognitive.microsofttranslator.com
export const URL_MICROSOFT_TRAN = export const URL_MICROSOFT_TRAN =
"https://api-edge.cognitive.microsofttranslator.com/translate"; "https://api-edge.cognitive.microsofttranslator.com/translate";
export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth"; export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth";
export const URL_MICROSOFT_LANGDETECT =
"https://api-edge.cognitive.microsofttranslator.com/detect?api-version=3.0";
export const URL_GOOGLE_TRAN =
"https://translate.googleapis.com/translate_a/single";
export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect"; export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
export const URL_BAIDU_SUGGEST = "https://fanyi.baidu.com/sug"; export const URL_BAIDU_SUGGEST = "https://fanyi.baidu.com/sug";
export const URL_BAIDU_TTS = "https://fanyi.baidu.com/gettts"; export const URL_BAIDU_TTS = "https://fanyi.baidu.com/gettts";
@@ -95,6 +102,8 @@ export const URL_NIUTRANS_REG =
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";
export const OPT_DICT_BAIDU = "Baidu";
export const OPT_TRANS_GOOGLE = "Google"; export const OPT_TRANS_GOOGLE = "Google";
export const OPT_TRANS_MICROSOFT = "Microsoft"; export const OPT_TRANS_MICROSOFT = "Microsoft";
export const OPT_TRANS_DEEPL = "DeepL"; export const OPT_TRANS_DEEPL = "DeepL";
@@ -140,6 +149,13 @@ export const OPT_TRANS_ALL = [
OPT_TRANS_CUSTOMIZE_5, OPT_TRANS_CUSTOMIZE_5,
]; ];
export const OPT_LANGDETECTOR_ALL = [
OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT,
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
];
export const OPT_LANGS_TO = [ export const OPT_LANGS_TO = [
["en", "English - English"], ["en", "English - English"],
["zh-CN", "Simplified Chinese - 简体中文"], ["zh-CN", "Simplified Chinese - 简体中文"],
@@ -202,7 +218,7 @@ export const OPT_LANGS_SPECIAL = {
]), ]),
[OPT_TRANS_DEEPLX]: new Map([ [OPT_TRANS_DEEPLX]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key.toUpperCase()]), ...OPT_LANGS_FROM.map(([key]) => [key, key.toUpperCase()]),
["auto", ""], ["auto", "auto"],
["zh-CN", "ZH"], ["zh-CN", "ZH"],
["zh-TW", "ZH"], ["zh-TW", "ZH"],
]), ]),
@@ -318,6 +334,12 @@ export const OPT_LANGS_SPECIAL = {
]), ]),
}; };
export const OPT_LANGS_LIST = OPT_LANGS_TO.map(([lang]) => lang); export const OPT_LANGS_LIST = OPT_LANGS_TO.map(([lang]) => lang);
export const OPT_LANGS_MICROSOFT = new Map(
Array.from(OPT_LANGS_SPECIAL[OPT_TRANS_MICROSOFT].entries()).map(([k, v]) => [
v,
k,
])
);
export const OPT_LANGS_BAIDU = new Map( export const OPT_LANGS_BAIDU = new Map(
Array.from(OPT_LANGS_SPECIAL[OPT_TRANS_BAIDU].entries()).map(([k, v]) => [ Array.from(OPT_LANGS_SPECIAL[OPT_TRANS_BAIDU].entries()).map(([k, v]) => [
v, v,
@@ -467,6 +489,7 @@ export const DEFAULT_TRANBOX_SETTING = {
followSelection: false, // 翻译框是否跟随选中文本 followSelection: false, // 翻译框是否跟随选中文本
triggerMode: OPT_TRANBOX_TRIGGER_CLICK, // 触发翻译方式 triggerMode: OPT_TRANBOX_TRIGGER_CLICK, // 触发翻译方式
extStyles: "", // 附加样式 extStyles: "", // 附加样式
enDict: OPT_DICT_BAIDU, // 英文词典
}; };
// 订阅列表 // 订阅列表
@@ -500,6 +523,8 @@ const defaultOpenaiApi = {
key: "", key: "",
model: "gpt-4", model: "gpt-4",
prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`, prompt: `You will be provided with a sentence in ${INPUT_PLACE_FROM}, and your task is to translate it into ${INPUT_PLACE_TO}.`,
temperature: 0,
maxTokens: 256,
fetchLimit: 1, fetchLimit: 1,
fetchInterval: 500, fetchInterval: 500,
}; };
@@ -513,7 +538,7 @@ const defaultOllamaApi = {
}; };
export const DEFAULT_TRANS_APIS = { export const DEFAULT_TRANS_APIS = {
[OPT_TRANS_GOOGLE]: { [OPT_TRANS_GOOGLE]: {
url: "https://translate.googleapis.com/translate_a/single", url: URL_GOOGLE_TRAN,
key: "", key: "",
fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量 fetchLimit: DEFAULT_FETCH_LIMIT, // 最大任务数量
fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间 fetchInterval: DEFAULT_FETCH_INTERVAL, // 任务间隔时间
@@ -634,6 +659,7 @@ export const DEFAULT_SETTING = {
csplist: DEFAULT_CSPLIST.join(",\n"), // 禁用CSP名单 csplist: DEFAULT_CSPLIST.join(",\n"), // 禁用CSP名单
// disableLangs: [], // 不翻译的语言(移至rule作废) // disableLangs: [], // 不翻译的语言(移至rule作废)
transInterval: 500, // 翻译间隔时间 transInterval: 500, // 翻译间隔时间
langDetector: OPT_TRANS_MICROSOFT, // 远程语言识别服务
}; };
export const DEFAULT_RULES = [GLOBLA_RULE]; export const DEFAULT_RULES = [GLOBLA_RULE];

View File

@@ -30,7 +30,11 @@ export function useTranslate(q, rule, setting) {
return; return;
} }
const deLang = await tryDetectLang(q, detectRemote === "true"); const deLang = await tryDetectLang(
q,
detectRemote === "true",
setting.langDetector
);
if (deLang && (toLang.includes(deLang) || skipLangs.includes(deLang))) { if (deLang && (toLang.includes(deLang) || skipLangs.includes(deLang))) {
setSamelang(true); setSamelang(true);
} else { } else {

View File

@@ -1,8 +1,26 @@
import { CACHE_NAME } from "../config"; import {
CACHE_NAME,
OPT_TRANS_GOOGLE,
OPT_TRANS_MICROSOFT,
OPT_TRANS_BAIDU,
OPT_TRANS_TENCENT,
} from "../config";
import { browser } from "./browser"; import { browser } from "./browser";
import { apiBaiduLangdetect } from "../apis"; import {
apiGoogleLangdetect,
apiMicrosoftLangdetect,
apiBaiduLangdetect,
apiTencentLangdetect,
} from "../apis";
import { kissLog } from "./log"; import { kissLog } from "./log";
const langdetectMap = {
[OPT_TRANS_GOOGLE]: apiGoogleLangdetect,
[OPT_TRANS_MICROSOFT]: apiMicrosoftLangdetect,
[OPT_TRANS_BAIDU]: apiBaiduLangdetect,
[OPT_TRANS_TENCENT]: apiTencentLangdetect,
};
/** /**
* 清除缓存数据 * 清除缓存数据
*/ */
@@ -19,12 +37,16 @@ export const tryClearCaches = async () => {
* @param {*} q * @param {*} q
* @returns * @returns
*/ */
export const tryDetectLang = async (q, useRemote = false) => { export const tryDetectLang = async (
q,
useRemote = false,
langDetector = OPT_TRANS_MICROSOFT
) => {
let lang = ""; let lang = "";
if (useRemote) { if (useRemote) {
try { try {
lang = await apiBaiduLangdetect(q); lang = await langdetectMap[langDetector](q);
} catch (err) { } catch (err) {
kissLog(err, "detect lang remote"); kissLog(err, "detect lang remote");
} }

View File

@@ -15,6 +15,16 @@ export const limitNumber = (num, min = 0, max = 100) => {
return number; return number;
}; };
export const limitFloat = (num, min = 0, max = 100) => {
const number = parseFloat(num);
if (Number.isNaN(number) || number < min) {
return min;
} else if (number > max) {
return max;
}
return number;
};
/** /**
* 匹配是否为数组中的值 * 匹配是否为数组中的值
* @param {*} arr * @param {*} arr

View File

@@ -38,7 +38,7 @@ import { useApi } from "../../hooks/Api";
import { apiTranslate } from "../../apis"; import { apiTranslate } from "../../apis";
import Box from "@mui/material/Box"; import Box from "@mui/material/Box";
import Link from "@mui/material/Link"; import Link from "@mui/material/Link";
import { limitNumber } from "../../libs/utils"; import { limitNumber, limitFloat } from "../../libs/utils";
function TestButton({ translator, api }) { function TestButton({ translator, api }) {
const i18n = useI18n(); const i18n = useI18n();
@@ -121,6 +121,8 @@ function ApiFields({ translator }) {
memoryNo = "", memoryNo = "",
reqHook = "", reqHook = "",
resHook = "", resHook = "",
temperature = 0,
maxTokens = 256,
} = api; } = api;
const handleChange = (e) => { const handleChange = (e) => {
@@ -132,6 +134,12 @@ function ApiFields({ translator }) {
case "fetchInterval": case "fetchInterval":
value = limitNumber(value, 0, 5000); value = limitNumber(value, 0, 5000);
break; break;
case "temperature":
value = limitFloat(value, 0, 2);
break;
case "maxTokens":
value = limitNumber(value, 0, 2 ** 15);
break;
default: default:
} }
updateApi({ updateApi({
@@ -225,6 +233,27 @@ function ApiFields({ translator }) {
</> </>
)} )}
{translator.startsWith(OPT_TRANS_OPENAI) && (
<>
<TextField
size="small"
label={"Temperature"}
type="number"
name="temperature"
value={temperature}
onChange={handleChange}
/>
<TextField
size="small"
label={"Max Tokens"}
type="number"
name="maxTokens"
value={maxTokens}
onChange={handleChange}
/>
</>
)}
{translator === OPT_TRANS_NIUTRANS && ( {translator === OPT_TRANS_NIUTRANS && (
<> <>
<TextField <TextField

View File

@@ -17,6 +17,8 @@ import {
UI_LANGS, UI_LANGS,
TRANS_NEWLINE_LENGTH, TRANS_NEWLINE_LENGTH,
CACHE_NAME, CACHE_NAME,
OPT_TRANS_MICROSOFT,
OPT_LANGDETECTOR_ALL,
OPT_SHORTCUT_TRANSLATE, OPT_SHORTCUT_TRANSLATE,
OPT_SHORTCUT_STYLE, OPT_SHORTCUT_STYLE,
OPT_SHORTCUT_POPUP, OPT_SHORTCUT_POPUP,
@@ -113,6 +115,7 @@ export default function Settings() {
blacklist = DEFAULT_BLACKLIST.join(",\n"), blacklist = DEFAULT_BLACKLIST.join(",\n"),
csplist = DEFAULT_CSPLIST.join(",\n"), csplist = DEFAULT_CSPLIST.join(",\n"),
transInterval = 500, transInterval = 500,
langDetector = OPT_TRANS_MICROSOFT,
} = setting; } = setting;
const { isHide = false } = fab || {}; const { isHide = false } = fab || {};
@@ -231,6 +234,22 @@ export default function Settings() {
</Select> </Select>
</FormControl> </FormControl>
<FormControl size="small">
<InputLabel>{i18n("detect_lang_remote")}</InputLabel>
<Select
name="langDetector"
value={langDetector}
label={i18n("detect_lang_remote")}
onChange={handleChange}
>
{OPT_LANGDETECTOR_ALL.map((item) => (
<MenuItem value={item} key={item}>
{item}
</MenuItem>
))}
</Select>
</FormControl>
{isExt ? ( {isExt ? (
<> <>
<FormControl size="small"> <FormControl size="small">

View File

@@ -9,6 +9,7 @@ import {
OPT_LANGS_TO, OPT_LANGS_TO,
OPT_TRANBOX_TRIGGER_CLICK, OPT_TRANBOX_TRIGGER_CLICK,
OPT_TRANBOX_TRIGGER_ALL, OPT_TRANBOX_TRIGGER_ALL,
OPT_DICT_BAIDU,
} from "../../config"; } from "../../config";
import ShortcutInput from "./ShortcutInput"; import ShortcutInput from "./ShortcutInput";
import FormControlLabel from "@mui/material/FormControlLabel"; import FormControlLabel from "@mui/material/FormControlLabel";
@@ -63,6 +64,7 @@ export default function Tranbox() {
followSelection = false, followSelection = false,
triggerMode = OPT_TRANBOX_TRIGGER_CLICK, triggerMode = OPT_TRANBOX_TRIGGER_CLICK,
extStyles = "", extStyles = "",
enDict = OPT_DICT_BAIDU,
} = tranboxSetting; } = tranboxSetting;
return ( return (
@@ -143,6 +145,18 @@ export default function Tranbox() {
))} ))}
</TextField> </TextField>
<TextField
select
size="small"
name="enDict"
value={enDict}
label={i18n("english_dict")}
onChange={handleChange}
>
<MenuItem value={"-"}>{i18n("disable")}</MenuItem>
<MenuItem value={OPT_DICT_BAIDU}>{OPT_DICT_BAIDU}</MenuItem>
</TextField>
<TextField <TextField
size="small" size="small"
label={i18n("tranbtn_offset_x")} label={i18n("tranbtn_offset_x")}

View File

@@ -101,7 +101,15 @@ function Header({
); );
} }
function TranForm({ text, setText, tranboxSetting, transApis, simpleStyle }) { function TranForm({
text,
setText,
tranboxSetting,
transApis,
simpleStyle,
langDetector,
enDict,
}) {
const i18n = useI18n(); const i18n = useI18n();
const [editMode, setEditMode] = useState(false); const [editMode, setEditMode] = useState(false);
@@ -233,7 +241,10 @@ function TranForm({ text, setText, tranboxSetting, transApis, simpleStyle }) {
</> </>
)} )}
{(!simpleStyle || !isValidWord(text) || !toLang.startsWith("zh")) && ( {(!simpleStyle ||
!isValidWord(text) ||
!toLang.startsWith("zh") ||
enDict === "-") && (
<TranCont <TranCont
text={text} text={text}
translator={translator} translator={translator}
@@ -242,11 +253,16 @@ function TranForm({ text, setText, tranboxSetting, transApis, simpleStyle }) {
toLang2={tranboxSetting.toLang2} toLang2={tranboxSetting.toLang2}
transApis={transApis} transApis={transApis}
simpleStyle={simpleStyle} simpleStyle={simpleStyle}
langDetector={langDetector}
/> />
)} )}
{enDict !== "-" && (
<>
<DictCont text={text} /> <DictCont text={text} />
<SugCont text={text} /> <SugCont text={text} />
</>
)}
</Stack> </Stack>
); );
} }
@@ -268,6 +284,8 @@ export default function TranBox({
followSelection, followSelection,
setFollowSelection, setFollowSelection,
extStyles, extStyles,
langDetector,
enDict,
}) { }) {
const [mouseHover, setMouseHover] = useState(false); const [mouseHover, setMouseHover] = useState(false);
return ( return (
@@ -300,6 +318,8 @@ export default function TranBox({
tranboxSetting={tranboxSetting} tranboxSetting={tranboxSetting}
transApis={transApis} transApis={transApis}
simpleStyle={simpleStyle} simpleStyle={simpleStyle}
langDetector={langDetector}
enDict={enDict}
/> />
</DraggableResizable> </DraggableResizable>
</ThemeProvider> </ThemeProvider>

View File

@@ -5,10 +5,11 @@ import Stack from "@mui/material/Stack";
import { useI18n } from "../../hooks/I18n"; import { useI18n } from "../../hooks/I18n";
import { DEFAULT_TRANS_APIS } from "../../config"; import { DEFAULT_TRANS_APIS } from "../../config";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { apiTranslate, apiBaiduLangdetect } from "../../apis"; import { apiTranslate } from "../../apis";
import CopyBtn from "./CopyBtn"; import CopyBtn from "./CopyBtn";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import Alert from "@mui/material/Alert"; import Alert from "@mui/material/Alert";
import { tryDetectLang } from "../../libs";
export default function TranCont({ export default function TranCont({
text, text,
@@ -18,6 +19,7 @@ export default function TranCont({
toLang2 = "en", toLang2 = "en",
transApis, transApis,
simpleStyle, simpleStyle,
langDetector,
}) { }) {
const i18n = useI18n(); const i18n = useI18n();
const [trText, setTrText] = useState(""); const [trText, setTrText] = useState("");
@@ -33,7 +35,7 @@ export default function TranCont({
let to = toLang; let to = toLang;
if (toLang !== toLang2 && toLang2 !== "none") { if (toLang !== toLang2 && toLang2 !== "none") {
const detectLang = await apiBaiduLangdetect(text); const detectLang = await tryDetectLang(text, true, langDetector);
if (detectLang === toLang) { if (detectLang === toLang) {
to = toLang2; to = toLang2;
} }
@@ -55,7 +57,7 @@ export default function TranCont({
setLoading(false); setLoading(false);
} }
})(); })();
}, [text, translator, fromLang, toLang, toLang2, transApis]); }, [text, translator, fromLang, toLang, toLang2, transApis, langDetector]);
if (simpleStyle) { if (simpleStyle) {
return ( return (

View File

@@ -10,6 +10,7 @@ import {
OPT_TRANBOX_TRIGGER_CLICK, OPT_TRANBOX_TRIGGER_CLICK,
OPT_TRANBOX_TRIGGER_HOVER, OPT_TRANBOX_TRIGGER_HOVER,
OPT_TRANBOX_TRIGGER_SELECT, OPT_TRANBOX_TRIGGER_SELECT,
OPT_DICT_BAIDU,
} from "../../config"; } from "../../config";
import { isMobile } from "../../libs/mobile"; import { isMobile } from "../../libs/mobile";
import { kissLog } from "../../libs/log"; import { kissLog } from "../../libs/log";
@@ -20,6 +21,7 @@ export default function Slection({
tranboxSetting, tranboxSetting,
transApis, transApis,
uiLang, uiLang,
langDetector,
}) { }) {
const { const {
hideTranBtn = false, hideTranBtn = false,
@@ -33,6 +35,7 @@ export default function Slection({
btnOffsetY, btnOffsetY,
boxOffsetX = 0, boxOffsetX = 0,
boxOffsetY = 10, boxOffsetY = 10,
enDict = OPT_DICT_BAIDU,
} = tranboxSetting; } = tranboxSetting;
const boxWidth = const boxWidth =
@@ -234,6 +237,8 @@ export default function Slection({
followSelection={followSelection} followSelection={followSelection}
setFollowSelection={setFollowSelection} setFollowSelection={setFollowSelection}
extStyles={extStyles} extStyles={extStyles}
langDetector={langDetector}
enDict={enDict}
/> />
)} )}