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

@@ -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 {
const res = await browser?.i18n?.detectLanguage(q);
return res?.languages?.[0]?.language;
if (useRemote) {
lang = await apiBaiduLangdetect(q);
}
if (!lang) {
const res = await browser?.i18n?.detectLanguage(q);
lang = res?.languages?.[0]?.language;
}
} catch (err) {
console.log("[detect lang]", err.message);
}
return lang;
};