mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update i18n locales imports
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
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];
|
||||
|
Reference in New Issue
Block a user