NextChat-U/app/locales/index.ts

137 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-06-17 02:03:06 +09:00
import cn from "./cn";
import en from "./en";
import pt from "./pt";
2023-06-17 02:03:06 +09:00
import tw from "./tw";
2023-08-25 08:31:33 +09:00
import id from "./id";
2023-06-17 02:03:06 +09:00
import fr from "./fr";
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 no from "./no";
import cs from "./cs";
import ko from "./ko";
2023-06-24 13:30:52 +09:00
import ar from "./ar";
2023-07-06 02:31:45 +09:00
import bn from "./bn";
2024-01-05 22:16:49 +09:00
import sk from "./sk";
2023-05-19 01:59:04 +09:00
import { merge } from "../utils/merge";
2024-09-08 14:23:40 +09:00
import { safeLocalStorage } from "@/app/utils";
2023-03-21 01:17:45 +09:00
2023-06-17 02:03:06 +09:00
import type { LocaleType } from "./cn";
export type { LocaleType, PartialLocaleType } from "./cn";
2024-09-08 14:23:40 +09:00
const localStorage = safeLocalStorage();
2023-06-17 02:03:06 +09:00
const ALL_LANGS = {
cn,
en,
tw,
2023-11-19 20:15:11 +09:00
pt,
2023-06-17 02:03:06 +09:00
jp,
ko,
2023-08-25 08:31:33 +09:00
id,
2023-06-17 02:03:06 +09:00
fr,
es,
it,
tr,
de,
vi,
ru,
cs,
no,
2023-06-24 13:30:52 +09:00
ar,
2023-07-06 02:31:45 +09:00
bn,
sk,
2023-06-17 02:03:06 +09:00
};
export type Lang = keyof typeof ALL_LANGS;
export const AllLangs = Object.keys(ALL_LANGS) as Lang[];
2023-03-21 01:17:45 +09:00
export const ALL_LANG_OPTIONS: Record<Lang, string> = {
cn: "简体中文",
en: "English",
pt: "Português",
tw: "繁體中文",
2023-06-17 02:03:06 +09:00
jp: "日本語",
ko: "한국어",
2023-08-25 08:31:33 +09:00
id: "Indonesia",
fr: "Français",
es: "Español",
it: "Italiano",
tr: "Türkçe",
de: "Deutsch",
vi: "Tiếng Việt",
ru: "Русский",
cs: "Čeština",
2023-06-17 02:03:06 +09:00
no: "Nynorsk",
2023-06-24 13:30:52 +09:00
ar: "العربية",
2023-07-06 02:55:43 +09:00
bn: "বাংলা",
2024-01-05 22:16:49 +09:00
sk: "Slovensky",
};
2023-03-29 02:39:14 +09:00
const LANG_KEY = "lang";
2024-05-20 20:02:46 +09:00
const DEFAULT_LANG = "en";
2023-03-21 01:25:27 +09:00
2024-05-20 20:02:46 +09:00
const fallbackLang = en;
2023-06-17 02:03:06 +09:00
const targetLang = ALL_LANGS[getLang()] as LocaleType;
// if target lang missing some fields, it will use fallback lang string
merge(fallbackLang, targetLang);
export default fallbackLang as LocaleType;
2023-03-21 01:25:27 +09:00
function getItem(key: string) {
2024-09-08 14:23:40 +09:00
return localStorage.getItem(key);
2023-03-21 01:25:27 +09:00
}
function setItem(key: string, value: string) {
2024-09-08 14:23:40 +09:00
localStorage.setItem(key, value);
2023-03-21 01:25:27 +09:00
}
function getLanguage() {
2023-03-29 02:39:14 +09:00
try {
const locale = new Intl.Locale(navigator.language).maximize();
const region = locale?.region?.toLowerCase();
// 1. check region code in ALL_LANGS
if (AllLangs.includes(region as Lang)) {
return region as Lang;
}
// 2. check language code in ALL_LANGS
if (AllLangs.includes(locale.language as Lang)) {
return locale.language as Lang;
}
return DEFAULT_LANG;
2023-03-29 02:39:14 +09:00
} catch {
2023-05-03 16:22:44 +09:00
return DEFAULT_LANG;
2023-03-29 02:39:14 +09:00
}
2023-03-21 01:25:27 +09:00
}
2023-03-21 01:17:45 +09:00
export function getLang(): Lang {
2024-05-20 20:02:46 +09:00
const savedLang = getItem(LANG_KEY);
if (AllLangs.includes((savedLang ?? "") as Lang)) {
return savedLang as Lang;
}
return getLanguage();
2023-03-21 01:17:45 +09:00
}
export function changeLang(lang: Lang) {
2023-03-29 02:39:14 +09:00
setItem(LANG_KEY, lang);
location.reload();
2023-03-21 01:17:45 +09:00
}
export function getISOLang() {
const isoLangString: Record<string, string> = {
cn: "zh-Hans",
tw: "zh-Hant",
};
const lang = getLang();
return isoLangString[lang] ?? lang;
}