detect lang remote

This commit is contained in:
Gabe Yuan
2023-10-10 18:03:05 +08:00
parent 5af66204c4
commit 5cd6977a6e
5 changed files with 77 additions and 6 deletions

View File

@@ -10,6 +10,8 @@ import {
PROMPT_PLACE_FROM,
PROMPT_PLACE_TO,
KV_SALT_SYNC,
URL_BAIDU_LANGDETECT,
OPT_LANGS_BAIDU,
} from "../config";
import { tryDetectLang } from "../libs";
import { sha256 } from "../libs/utils";
@@ -237,6 +239,30 @@ const apiCustomTranslate = async (
return [trText, isSame];
};
/**
* 百度语言识别
* @param {*} text
* @returns
*/
export const apiBaiduLangdetect = async (text) => {
const res = await fetchPolyfill(URL_BAIDU_LANGDETECT, {
headers: {
"Content-type": "application/json",
},
method: "POST",
body: JSON.stringify({
query: text,
}),
useCache: true,
});
if (res.error === 0) {
return OPT_LANGS_BAIDU.get(res.lan) ?? res.lan;
}
return "";
};
/**
* 统一翻译接口
* @param {*} param0

View File

@@ -68,10 +68,12 @@ export const URL_KISS_RULES_NEW_ISSUE =
export const URL_RAW_PREFIX =
"https://raw.githubusercontent.com/fishjar/kiss-translator/master";
export const URL_MICROSOFT_AUTH = "https://edge.microsoft.com/translate/auth";
export const URL_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
export const OPT_TRANS_GOOGLE = "Google";
export const OPT_TRANS_MICROSOFT = "Microsoft";
export const OPT_TRANS_DEEPL = "DeepL";
export const OPT_TRANS_BAIDU = "Baidu";
export const OPT_TRANS_OPENAI = "OpenAI";
export const OPT_TRANS_CUSTOMIZE = "Custom";
export const OPT_TRANS_ALL = [
@@ -134,12 +136,45 @@ export const OPT_LANGS_SPECIAL = {
["zh-CN", "ZH"],
["zh-TW", "ZH"],
]),
[OPT_TRANS_BAIDU]: new Map([
...OPT_LANGS_FROM.map(([key]) => [key, key.toUpperCase()]),
["zh-CN", "zh"],
["zh-TW", "cht"],
["ar", "ara"],
["bg", "bul"],
["ca", "cat"],
["hr", "hrv"],
["da", "dan"],
["fi", "fin"],
["fr", "fra"],
["hi", "mai"],
["ja", "jp"],
["ko", "kor"],
["ms", "may"],
["mt", "mlt"],
["nb", "nor"],
["ro", "rom"],
["ru", "ru"],
["sl", "slo"],
["es", "spa"],
["sv", "swe"],
["ta", "tam"],
["te", "tel"],
["uk", "ukr"],
["vi", "vie"],
]),
[OPT_TRANS_OPENAI]: new Map(
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
),
[OPT_TRANS_CUSTOMIZE]: new Map([["auto", ""]]),
};
export const OPT_LANGS_LIST = OPT_LANGS_TO.map(([lang]) => lang);
export const OPT_LANGS_BAIDU = new Map(
Array.from(OPT_LANGS_SPECIAL[OPT_TRANS_BAIDU].entries()).map(([k, v]) => [
v,
k,
])
);
export const OPT_STYLE_NONE = "style_none"; // 无
export const OPT_STYLE_LINE = "under_line"; // 下划线

View File

@@ -23,7 +23,7 @@ export function useTranslate(q, rule, setting) {
try {
setLoading(true);
const deLang = await tryDetectLang(q);
const deLang = await tryDetectLang(q, true);
if (deLang && toLang.includes(deLang)) {
setSamelang(true);
} else {

View File

@@ -1,5 +1,6 @@
import { CACHE_NAME } from "../config";
import { browser } from "./browser";
import { apiBaiduLangdetect } from "../apis";
/**
* 清除缓存数据
@@ -13,15 +14,24 @@ export const tryClearCaches = async () => {
};
/**
* 本地语言识别
* 语言识别
* @param {*} q
* @returns
*/
export const tryDetectLang = async (q) => {
export const tryDetectLang = async (q, useRemote = false) => {
let lang = "";
try {
if (useRemote) {
lang = await apiBaiduLangdetect(q);
}
if (!lang) {
const res = await browser?.i18n?.detectLanguage(q);
return res?.languages?.[0]?.language;
lang = res?.languages?.[0]?.language;
}
} catch (err) {
console.log("[detect lang]", err.message);
}
return lang;
};

View File

@@ -421,7 +421,7 @@ export class Translator {
try {
addLoading(node, loadingId);
const deLang = await tryDetectLang(text);
const deLang = await tryDetectLang(text, true);
if (deLang && toLang.includes(deLang)) {
return;
}