feat: mutual translation effect with the target language

This commit is contained in:
Gabe Yuan
2023-12-11 15:54:54 +08:00
parent 95d19417c3
commit bdd5ed7fc7
5 changed files with 54 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ import Stack from "@mui/material/Stack";
import { useI18n } from "../../hooks/I18n";
import { DEFAULT_TRANS_APIS, OPT_TRANS_BAIDU } from "../../config";
import { useEffect, useState } from "react";
import { apiTranslate } from "../../apis";
import { apiTranslate, apiBaiduLangdetect } from "../../apis";
import { isValidWord } from "../../libs/utils";
import CopyBtn from "./CopyBtn";
import DictCont from "./DictCont";
@@ -16,6 +16,9 @@ export default function TranCont({
translator,
fromLang,
toLang,
toLang2 = "en",
setToLang,
setToLang2,
transApis,
}) {
const i18n = useI18n();
@@ -32,6 +35,16 @@ export default function TranCont({
setError("");
setDictResult(null);
// 互译
if (toLang !== toLang2 && toLang2 !== "none") {
const detectLang = await apiBaiduLangdetect(text);
if (detectLang === toLang) {
setToLang(toLang2);
setToLang2(toLang);
return;
}
}
const apiSetting =
transApis[translator] || DEFAULT_TRANS_APIS[translator];
const tranRes = await apiTranslate({
@@ -63,7 +76,16 @@ export default function TranCont({
setLoading(false);
}
})();
}, [text, translator, fromLang, toLang, transApis]);
}, [
text,
translator,
fromLang,
toLang,
toLang2,
setToLang,
setToLang2,
transApis,
]);
return (
<>