12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import CN from "./cn";
- import EN from "./en";
- import TW from "./tw";
- import ES from "./es";
- import IT from "./it";
- import TR from "./tr";
- import JP from "./jp";
- import DE from "./de";
- import VI from "./vi";
- import RU from "./ru";
- import CS from "./cs";
- export type { LocaleType } from "./cn";
- export const AllLangs = [
- "en",
- "cn",
- "tw",
- "es",
- "it",
- "tr",
- "jp",
- "de",
- "vi",
- "ru",
- "cs",
- ] as const;
- export type Lang = (typeof AllLangs)[number];
- const LANG_KEY = "lang";
- const DEFAULT_LANG = "en";
- function getItem(key: string) {
- try {
- return localStorage.getItem(key);
- } catch {
- return null;
- }
- }
- function setItem(key: string, value: string) {
- try {
- localStorage.setItem(key, value);
- } catch {}
- }
- function getLanguage() {
- try {
- return navigator.language.toLowerCase();
- } catch {
- console.log("[Lang] failed to detect user lang.");
- return DEFAULT_LANG;
- }
- }
- export function getLang(): Lang {
- const savedLang = getItem(LANG_KEY);
- if (AllLangs.includes((savedLang ?? "") as Lang)) {
- return savedLang as Lang;
- }
- const lang = getLanguage();
- for (const option of AllLangs) {
- if (lang.includes(option)) {
- return option;
- }
- }
- return DEFAULT_LANG;
- }
- export function changeLang(lang: Lang) {
- setItem(LANG_KEY, lang);
- location.reload();
- }
- export default {
- en: EN,
- cn: CN,
- tw: TW,
- es: ES,
- it: IT,
- tr: TR,
- jp: JP,
- de: DE,
- vi: VI,
- ru: RU,
- cs: CS,
- }[getLang()] as typeof CN;
|