feat: use i18next-resources-to-backend to lazy load i18n (#2738)

* feat: use i18next-resources-to-backend to lazy load i18n file

* feat: change source in yarn.lock
This commit is contained in:
DacongDA
2024-02-23 22:35:59 +08:00
committed by GitHub
parent 042a8d0ad6
commit dc3131c683
3 changed files with 38 additions and 59 deletions

View File

@ -14,56 +14,9 @@
import i18n from "i18next";
import en from "./locales/en/data.json";
import zh from "./locales/zh/data.json";
import es from "./locales/es/data.json";
import fr from "./locales/fr/data.json";
import de from "./locales/de/data.json";
import id from "./locales/id/data.json";
import ja from "./locales/ja/data.json";
import ko from "./locales/ko/data.json";
import ru from "./locales/ru/data.json";
import vi from "./locales/vi/data.json";
import pt from "./locales/pt/data.json";
import it from "./locales/it/data.json";
import ms from "./locales/ms/data.json";
import tr from "./locales/tr/data.json";
import ar from "./locales/ar/data.json";
import he from "./locales/he/data.json";
import nl from "./locales/nl/data.json";
import pl from "./locales/pl/data.json";
import fi from "./locales/fi/data.json";
import sv from "./locales/sv/data.json";
import uk from "./locales/uk/data.json";
import kk from "./locales/kk/data.json";
import fa from "./locales/fa/data.json";
import * as Conf from "./Conf";
import {initReactI18next} from "react-i18next";
const resources = {
en: en,
zh: zh,
es: es,
fr: fr,
de: de,
id: id,
ja: ja,
ko: ko,
ru: ru,
vi: vi,
pt: pt,
it: it,
ms: ms,
tr: tr,
ar: ar,
he: he,
nl: nl,
pl: pl,
fi: fi,
sv: sv,
uk: uk,
kk: kk,
fa: fa,
};
import resourcesToBackend from "i18next-resources-to-backend";
function initLanguage() {
let language = localStorage.getItem("language");
@ -157,18 +110,24 @@ function initLanguage() {
return language;
}
i18n.use(initReactI18next).init({
lng: initLanguage(),
await i18n.use(resourcesToBackend(async(language, namespace) => {
const res = await import(`./locales/${language}/data.json`);
return res.default[namespace];
}
))
.use(initReactI18next)
.init({
lng: initLanguage(),
ns: Object.keys(en),
fallbackLng: Conf.DefaultLanguage,
resources: resources,
keySeparator: false,
keySeparator: false,
interpolation: {
escapeValue: true,
},
// debug: true,
saveMissing: true,
});
interpolation: {
escapeValue: true,
},
// debug: true,
saveMissing: true,
});
export default i18n;