Commit ac031533 authored by Steven's avatar Steven

chore: update i18n locales imports

parent ac8a374e
import i18n, { FallbackLng, FallbackLngObjList } from "i18next";
import i18n, { BackendModule, FallbackLng, FallbackLngObjList } from "i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import toast from "react-hot-toast";
import { initReactI18next } from "react-i18next";
export const availableLocales = [
......@@ -35,7 +34,21 @@ const fallbacks = {
zh: ["zh-Hans", "en"],
} as FallbackLngObjList;
const LazyImportPlugin: BackendModule = {
type: "backend",
init: function () {},
read: function (language, _, callback) {
if (fallbacks[language]) {
language = fallbacks[language][0];
}
import(`./locales/${language}.json`).then((translation: any) => {
callback(null, translation);
});
},
};
i18n
.use(LazyImportPlugin)
.use(LanguageDetector)
.use(initReactI18next)
.init({
......@@ -48,15 +61,5 @@ i18n
} as FallbackLng,
});
for (const locale of availableLocales) {
import(`./locales/${locale}.json`)
.then((translation) => {
i18n.addResourceBundle(locale, "translation", translation.default, true, true);
})
.catch((err) => {
toast.error(`Failed to load locale "${locale}".\n${err}`, { duration: 5000 });
});
}
export default i18n;
export type TLocale = (typeof availableLocales)[number];
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment