2023-03-28 17:39:14 +00:00
|
|
|
import CN from "./cn";
|
|
|
|
import EN from "./en";
|
|
|
|
import TW from "./tw";
|
2023-03-31 00:48:57 +00:00
|
|
|
import ES from "./es";
|
2023-04-02 17:23:50 +00:00
|
|
|
import IT from "./it";
|
2023-03-20 16:17:45 +00:00
|
|
|
|
2023-03-28 17:39:14 +00:00
|
|
|
export type { LocaleType } from "./cn";
|
2023-03-20 16:17:45 +00:00
|
|
|
|
2023-04-02 17:23:50 +00:00
|
|
|
export const AllLangs = ["en", "cn", "tw", "es", "it"] as const;
|
2023-03-28 17:51:22 +00:00
|
|
|
type Lang = (typeof AllLangs)[number];
|
2023-03-20 16:17:45 +00:00
|
|
|
|
2023-03-28 17:39:14 +00:00
|
|
|
const LANG_KEY = "lang";
|
2023-03-20 16:25:27 +00:00
|
|
|
|
|
|
|
function getItem(key: string) {
|
2023-03-28 17:39:14 +00:00
|
|
|
try {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
} catch {
|
|
|
|
return null;
|
|
|
|
}
|
2023-03-20 16:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setItem(key: string, value: string) {
|
2023-03-28 17:39:14 +00:00
|
|
|
try {
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
} catch {}
|
2023-03-20 16:25:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLanguage() {
|
2023-03-28 17:39:14 +00:00
|
|
|
try {
|
|
|
|
return navigator.language.toLowerCase();
|
|
|
|
} catch {
|
|
|
|
return "cn";
|
|
|
|
}
|
2023-03-20 16:25:27 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 16:17:45 +00:00
|
|
|
export function getLang(): Lang {
|
2023-03-28 17:39:14 +00:00
|
|
|
const savedLang = getItem(LANG_KEY);
|
2023-03-20 16:17:45 +00:00
|
|
|
|
2023-03-28 17:39:14 +00:00
|
|
|
if (AllLangs.includes((savedLang ?? "") as Lang)) {
|
|
|
|
return savedLang as Lang;
|
|
|
|
}
|
2023-03-20 16:17:45 +00:00
|
|
|
|
2023-03-28 17:39:14 +00:00
|
|
|
const lang = getLanguage();
|
2023-03-20 16:17:45 +00:00
|
|
|
|
2023-03-28 17:39:14 +00:00
|
|
|
if (lang.includes("zh") || lang.includes("cn")) {
|
|
|
|
return "cn";
|
|
|
|
} else if (lang.includes("tw")) {
|
|
|
|
return "tw";
|
2023-03-31 00:48:57 +00:00
|
|
|
} else if (lang.includes("es")) {
|
|
|
|
return "es";
|
2023-04-02 17:23:50 +00:00
|
|
|
} else if (lang.includes("it")) {
|
|
|
|
return "it";
|
2023-03-28 17:39:14 +00:00
|
|
|
} else {
|
|
|
|
return "en";
|
|
|
|
}
|
2023-03-20 16:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function changeLang(lang: Lang) {
|
2023-03-28 17:39:14 +00:00
|
|
|
setItem(LANG_KEY, lang);
|
|
|
|
location.reload();
|
2023-03-20 16:17:45 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 17:23:50 +00:00
|
|
|
export default { en: EN, cn: CN, tw: TW, es: ES, it: IT }[getLang()];
|