detect lang remote
This commit is contained in:
@@ -10,6 +10,8 @@ import {
|
|||||||
PROMPT_PLACE_FROM,
|
PROMPT_PLACE_FROM,
|
||||||
PROMPT_PLACE_TO,
|
PROMPT_PLACE_TO,
|
||||||
KV_SALT_SYNC,
|
KV_SALT_SYNC,
|
||||||
|
URL_BAIDU_LANGDETECT,
|
||||||
|
OPT_LANGS_BAIDU,
|
||||||
} from "../config";
|
} from "../config";
|
||||||
import { tryDetectLang } from "../libs";
|
import { tryDetectLang } from "../libs";
|
||||||
import { sha256 } from "../libs/utils";
|
import { sha256 } from "../libs/utils";
|
||||||
@@ -237,6 +239,30 @@ const apiCustomTranslate = async (
|
|||||||
return [trText, isSame];
|
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
|
* @param {*} param0
|
||||||
|
|||||||
@@ -68,10 +68,12 @@ export const URL_KISS_RULES_NEW_ISSUE =
|
|||||||
export const URL_RAW_PREFIX =
|
export const URL_RAW_PREFIX =
|
||||||
"https://raw.githubusercontent.com/fishjar/kiss-translator/master";
|
"https://raw.githubusercontent.com/fishjar/kiss-translator/master";
|
||||||
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_BAIDU_LANGDETECT = "https://fanyi.baidu.com/langdetect";
|
||||||
|
|
||||||
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";
|
||||||
|
export const OPT_TRANS_BAIDU = "Baidu";
|
||||||
export const OPT_TRANS_OPENAI = "OpenAI";
|
export const OPT_TRANS_OPENAI = "OpenAI";
|
||||||
export const OPT_TRANS_CUSTOMIZE = "Custom";
|
export const OPT_TRANS_CUSTOMIZE = "Custom";
|
||||||
export const OPT_TRANS_ALL = [
|
export const OPT_TRANS_ALL = [
|
||||||
@@ -134,12 +136,45 @@ export const OPT_LANGS_SPECIAL = {
|
|||||||
["zh-CN", "ZH"],
|
["zh-CN", "ZH"],
|
||||||
["zh-TW", "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_TRANS_OPENAI]: new Map(
|
||||||
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
|
OPT_LANGS_FROM.map(([key, val]) => [key, val.split(" - ")[0]])
|
||||||
),
|
),
|
||||||
[OPT_TRANS_CUSTOMIZE]: new Map([["auto", ""]]),
|
[OPT_TRANS_CUSTOMIZE]: new Map([["auto", ""]]),
|
||||||
};
|
};
|
||||||
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_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_NONE = "style_none"; // 无
|
||||||
export const OPT_STYLE_LINE = "under_line"; // 下划线
|
export const OPT_STYLE_LINE = "under_line"; // 下划线
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ export function useTranslate(q, rule, setting) {
|
|||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const deLang = await tryDetectLang(q);
|
const deLang = await tryDetectLang(q, true);
|
||||||
if (deLang && toLang.includes(deLang)) {
|
if (deLang && toLang.includes(deLang)) {
|
||||||
setSamelang(true);
|
setSamelang(true);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { CACHE_NAME } from "../config";
|
import { CACHE_NAME } from "../config";
|
||||||
import { browser } from "./browser";
|
import { browser } from "./browser";
|
||||||
|
import { apiBaiduLangdetect } from "../apis";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清除缓存数据
|
* 清除缓存数据
|
||||||
@@ -13,15 +14,24 @@ export const tryClearCaches = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本地语言识别
|
* 语言识别
|
||||||
* @param {*} q
|
* @param {*} q
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export const tryDetectLang = async (q) => {
|
export const tryDetectLang = async (q, useRemote = false) => {
|
||||||
|
let lang = "";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
if (useRemote) {
|
||||||
|
lang = await apiBaiduLangdetect(q);
|
||||||
|
}
|
||||||
|
if (!lang) {
|
||||||
const res = await browser?.i18n?.detectLanguage(q);
|
const res = await browser?.i18n?.detectLanguage(q);
|
||||||
return res?.languages?.[0]?.language;
|
lang = res?.languages?.[0]?.language;
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log("[detect lang]", err.message);
|
console.log("[detect lang]", err.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return lang;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -421,7 +421,7 @@ export class Translator {
|
|||||||
try {
|
try {
|
||||||
addLoading(node, loadingId);
|
addLoading(node, loadingId);
|
||||||
|
|
||||||
const deLang = await tryDetectLang(text);
|
const deLang = await tryDetectLang(text, true);
|
||||||
if (deLang && toLang.includes(deLang)) {
|
if (deLang && toLang.includes(deLang)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user