Files
Termix/src/i18n/i18n.ts
T

108 lines
3.5 KiB
TypeScript
Raw Normal View History

2026-05-06 15:12:07 -05:00
import i18n, { type BackendModule, type ResourceKey } from "i18next";
2025-09-12 14:42:00 -05:00
import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
+1
2025-12-31 22:20:12 -06:00
import enTranslation from "../locales/en.json";
2026-05-06 15:12:07 -05:00
type LocaleModule = { default: ResourceKey };
const localeLoaders = {
af: () => import("../locales/translated/af_ZA.json"),
ar: () => import("../locales/translated/ar_SA.json"),
bn: () => import("../locales/translated/bn_BD.json"),
bg: () => import("../locales/translated/bg_BG.json"),
ca: () => import("../locales/translated/ca_ES.json"),
cs: () => import("../locales/translated/cs_CZ.json"),
da: () => import("../locales/translated/da_DK.json"),
de: () => import("../locales/translated/de_DE.json"),
el: () => import("../locales/translated/el_GR.json"),
"es-ES": () => import("../locales/translated/es_ES.json"),
fi: () => import("../locales/translated/fi_FI.json"),
fr: () => import("../locales/translated/fr_FR.json"),
he: () => import("../locales/translated/he_IL.json"),
hi: () => import("../locales/translated/hi_IN.json"),
hu: () => import("../locales/translated/hu_HU.json"),
id: () => import("../locales/translated/id_ID.json"),
it: () => import("../locales/translated/it_IT.json"),
ja: () => import("../locales/translated/ja_JP.json"),
ko: () => import("../locales/translated/ko_KR.json"),
nl: () => import("../locales/translated/nl_NL.json"),
no: () => import("../locales/translated/no_NO.json"),
pl: () => import("../locales/translated/pl_PL.json"),
"pt-PT": () => import("../locales/translated/pt_PT.json"),
"pt-BR": () => import("../locales/translated/pt_BR.json"),
ro: () => import("../locales/translated/ro_RO.json"),
ru: () => import("../locales/translated/ru_RU.json"),
sr: () => import("../locales/translated/sr_SP.json"),
"sv-SE": () => import("../locales/translated/sv_SE.json"),
th: () => import("../locales/translated/th_TH.json"),
tr: () => import("../locales/translated/tr_TR.json"),
uk: () => import("../locales/translated/uk_UA.json"),
vi: () => import("../locales/translated/vi_VN.json"),
"zh-CN": () => import("../locales/translated/zh_CN.json"),
"zh-TW": () => import("../locales/translated/zh_TW.json"),
} satisfies Record<string, () => Promise<LocaleModule>>;
const supportedLngs = ["en", ...Object.keys(localeLoaders)];
const localeBackend: BackendModule = {
type: "backend",
init: () => {},
read: (language, _namespace, callback) => {
if (language === "en") {
callback(null, enTranslation);
return;
}
const loadLocale = localeLoaders[language];
if (!loadLocale) {
callback(new Error(`Unsupported language: ${language}`), false);
return;
}
loadLocale()
.then((module) => callback(null, module.default))
.catch((error: unknown) => {
callback(
error instanceof Error ? error : new Error(String(error)),
false,
);
});
},
};
2025-09-03 00:14:49 -05:00
i18n
2026-05-06 15:12:07 -05:00
.use(localeBackend)
2025-09-12 14:42:00 -05:00
.use(LanguageDetector)
.use(initReactI18next)
2025-09-03 00:14:49 -05:00
.init({
2026-05-06 15:12:07 -05:00
supportedLngs,
2025-09-12 14:42:00 -05:00
fallbackLng: "en",
2025-09-03 00:14:49 -05:00
debug: false,
2025-09-12 14:42:00 -05:00
2025-09-03 00:14:49 -05:00
detection: {
2025-09-12 14:42:00 -05:00
order: ["localStorage", "cookie"],
caches: ["localStorage", "cookie"],
lookupLocalStorage: "i18nextLng",
lookupCookie: "i18nextLng",
2025-09-03 00:14:49 -05:00
checkWhitelist: true,
},
2025-09-12 14:42:00 -05:00
resources: {
en: {
translation: enTranslation,
},
2025-09-03 00:14:49 -05:00
},
2026-05-06 15:12:07 -05:00
partialBundledLanguages: true,
2025-09-12 14:42:00 -05:00
2025-09-03 00:14:49 -05:00
interpolation: {
2025-09-12 14:42:00 -05:00
escapeValue: false,
2025-09-03 00:14:49 -05:00
},
2025-09-12 14:42:00 -05:00
2025-09-03 00:14:49 -05:00
react: {
2025-09-12 14:42:00 -05:00
useSuspense: false,
2025-09-03 00:14:49 -05:00
},
});
2025-09-12 14:42:00 -05:00
export default i18n;