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

122 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-03-28 17:39:14 +00:00
import CN from "./cn";
import EN from "./en";
import TW from "./tw";
import FR from "./fr";
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-04-10 09:04:30 +00:00
import TR from "./tr";
2023-04-10 14:01:54 +00:00
import JP from "./jp";
2023-04-16 05:07:54 +00:00
import DE from "./de";
2023-05-07 07:54:09 +00:00
import VI from "./vi";
import RU from "./ru";
2023-04-24 20:54:24 +00:00
import NO from "./no";
2023-05-10 13:48:36 +00:00
import CS from "./cs";
2023-05-17 14:59:35 +00:00
import KO from "./ko";
2023-05-18 16:59:04 +00:00
import { merge } from "../utils/merge";
2023-03-20 16:17:45 +00:00
export type { LocaleType, RequiredLocaleType } from "./cn";
2023-03-20 16:17:45 +00:00
2023-04-16 05:07:54 +00:00
export const AllLangs = [
"en",
"cn",
"tw",
"fr",
2023-04-16 05:07:54 +00:00
"es",
"it",
"tr",
"jp",
"de",
2023-05-07 07:54:09 +00:00
"vi",
"ru",
2023-05-10 13:48:36 +00:00
"cs",
2023-05-17 14:59:35 +00:00
"ko",
2023-04-16 05:07:54 +00:00
] as const;
2023-04-23 17:15:44 +00:00
export type Lang = (typeof AllLangs)[number];
2023-03-20 16:17:45 +00:00
export const ALL_LANG_OPTIONS: Record<Lang, string> = {
cn: "简体中文",
en: "English",
tw: "繁體中文",
fr: "Français",
es: "Español",
it: "Italiano",
tr: "Türkçe",
jp: "日本語",
de: "Deutsch",
vi: "Tiếng Việt",
ru: "Русский",
cs: "Čeština",
ko: "한국어",
};
2023-03-28 17:39:14 +00:00
const LANG_KEY = "lang";
2023-05-03 07:22:44 +00:00
const DEFAULT_LANG = "en";
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 {
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 {
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
for (const option of AllLangs) {
if (lang.includes(option)) {
return option;
}
2023-03-28 17:39:14 +00:00
}
2023-05-03 07:22:44 +00:00
return DEFAULT_LANG;
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-05-18 16:59:04 +00:00
const fallbackLang = EN;
const targetLang = {
2023-04-16 05:07:54 +00:00
en: EN,
cn: CN,
tw: TW,
fr: FR,
2023-04-16 05:07:54 +00:00
es: ES,
it: IT,
tr: TR,
jp: JP,
de: DE,
2023-05-07 07:54:09 +00:00
vi: VI,
ru: RU,
2023-04-24 20:54:24 +00:00
no: NO,
2023-05-10 13:48:36 +00:00
cs: CS,
2023-05-17 14:59:35 +00:00
ko: KO,
}[getLang()] as typeof CN;
2023-05-18 16:59:04 +00:00
// if target lang missing some fields, it will use fallback lang string
merge(fallbackLang, targetLang);
export default fallbackLang as typeof CN;