ChatGPT-Next-Web/app/locales/index.ts

140 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-06-16 17:03:06 +00:00
import cn from "./cn";
import en from "./en";
import pt from "./pt";
2023-06-16 17:03:06 +00:00
import tw from "./tw";
2023-08-24 23:31:33 +00:00
import id from "./id";
2023-06-16 17:03:06 +00: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 04:30:52 +00:00
import ar from "./ar";
2023-07-05 17:31:45 +00:00
import bn from "./bn";
2024-01-05 13:16:49 +00:00
import sk from "./sk";
2023-05-18 16:59:04 +00:00
import { merge } from "../utils/merge";
2023-03-20 16:17:45 +00:00
2023-06-16 17:03:06 +00:00
import type { LocaleType } from "./cn";
export type { LocaleType, PartialLocaleType } from "./cn";
const ALL_LANGS = {
cn,
en,
tw,
2023-11-19 11:15:11 +00:00
pt,
2023-06-16 17:03:06 +00:00
jp,
ko,
2023-08-24 23:31:33 +00:00
id,
2023-06-16 17:03:06 +00:00
fr,
es,
it,
tr,
de,
vi,
ru,
cs,
no,
2023-06-24 04:30:52 +00:00
ar,
2023-07-05 17:31:45 +00:00
bn,
sk,
2023-06-16 17:03:06 +00:00
};
export type Lang = keyof typeof ALL_LANGS;
export const AllLangs = Object.keys(ALL_LANGS) as Lang[];
2023-03-20 16:17:45 +00:00
export const ALL_LANG_OPTIONS: Record<Lang, string> = {
cn: "简体中文",
en: "English",
pt: "Português",
tw: "繁體中文",
2023-06-16 17:03:06 +00:00
jp: "日本語",
ko: "한국어",
2023-08-24 23:31:33 +00: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-16 17:03:06 +00:00
no: "Nynorsk",
2023-06-24 04:30:52 +00:00
ar: "العربية",
2023-07-05 17:55:43 +00:00
bn: "বাংলা",
2024-01-05 13:16:49 +00:00
sk: "Slovensky",
};
2023-03-28 17:39:14 +00:00
const LANG_KEY = "lang";
2024-05-20 11:02:46 +00:00
const DEFAULT_LANG = "en";
2023-03-20 16:25:27 +00:00
2024-05-20 11:02:46 +00:00
const fallbackLang = en;
2023-06-16 17:03:06 +00: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-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 {
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-28 17:39:14 +00:00
} catch {
2023-05-03 07:22:44 +00:00
return DEFAULT_LANG;
2023-03-28 17:39:14 +00:00
}
2023-03-20 16:25:27 +00:00
}
2023-03-20 16:17:45 +00:00
export function getLang(): Lang {
2024-05-20 11:02:46 +00:00
const savedLang = getItem(LANG_KEY);
if (AllLangs.includes((savedLang ?? "") as Lang)) {
return savedLang as Lang;
}
return getLanguage();
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
}
export function getISOLang() {
const isoLangString: Record<string, string> = {
cn: "zh-Hans",
tw: "zh-Hant",
};
const lang = getLang();
return isoLangString[lang] ?? lang;
}