2023-08-30 18:05:37 +08:00
|
|
|
import { CACHE_NAME } from "../config";
|
2023-08-31 00:18:57 +08:00
|
|
|
import { browser } from "./browser";
|
2023-10-10 18:03:05 +08:00
|
|
|
import { apiBaiduLangdetect } from "../apis";
|
2023-07-20 13:45:41 +08:00
|
|
|
|
|
|
|
|
/**
|
2023-08-30 18:05:37 +08:00
|
|
|
* 清除缓存数据
|
2023-07-20 13:45:41 +08:00
|
|
|
*/
|
2023-08-30 18:05:37 +08:00
|
|
|
export const tryClearCaches = async () => {
|
2023-08-27 17:43:27 +08:00
|
|
|
try {
|
2023-08-30 18:05:37 +08:00
|
|
|
caches.delete(CACHE_NAME);
|
2023-08-27 17:43:27 +08:00
|
|
|
} catch (err) {
|
2023-08-30 18:05:37 +08:00
|
|
|
console.log("[clean caches]", err.message);
|
2023-08-27 17:43:27 +08:00
|
|
|
}
|
2023-07-20 13:45:41 +08:00
|
|
|
};
|
2023-08-31 00:18:57 +08:00
|
|
|
|
|
|
|
|
/**
|
2023-10-10 18:03:05 +08:00
|
|
|
* 语言识别
|
2023-08-31 00:18:57 +08:00
|
|
|
* @param {*} q
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2023-10-10 18:03:05 +08:00
|
|
|
export const tryDetectLang = async (q, useRemote = false) => {
|
|
|
|
|
let lang = "";
|
|
|
|
|
|
2023-08-31 00:18:57 +08:00
|
|
|
try {
|
2023-10-10 18:03:05 +08:00
|
|
|
if (useRemote) {
|
|
|
|
|
lang = await apiBaiduLangdetect(q);
|
|
|
|
|
}
|
|
|
|
|
if (!lang) {
|
|
|
|
|
const res = await browser?.i18n?.detectLanguage(q);
|
|
|
|
|
lang = res?.languages?.[0]?.language;
|
|
|
|
|
}
|
2023-08-31 00:18:57 +08:00
|
|
|
} catch (err) {
|
2023-08-31 13:38:06 +08:00
|
|
|
console.log("[detect lang]", err.message);
|
2023-08-31 00:18:57 +08:00
|
|
|
}
|
2023-10-10 18:03:05 +08:00
|
|
|
|
|
|
|
|
return lang;
|
2023-08-31 00:18:57 +08:00
|
|
|
};
|