I’m trying to set up i18n dynamic import for a library, so I have an init function:
import i18n from "i18next";
import resourcesToBackend from "i18next-resources-to-backend";
import { initReactI18next } from "react-i18next";
i18n
.use(
resourcesToBackend(
(language: string, namespace: string) =>
import(`locales/${language}/${namespace}.json`)
)
)
.use(initReactI18next)
.init({
initImmediate: false,
debug: true,
fallbackLng: "en",
lng: "en",
ns: ["checklist", "complexity"],
interpolation: {
escapeValue: false,
},
});
export default i18n;
And the translations work when I import components from the library like this:
import {
PasswordChecklist,
PasswordComplexityIndicator,
PasswordMeter,
passwordStrength,
} from "../../src";
But i18n throws an errors when I try to import them from a production build folder:
Error: Cannot find module 'locales/en/checklist.json'
Error: Cannot find module 'locales/en/complexity.json'
import {
PasswordChecklist,
PasswordComplexityIndicator,
PasswordMeter,
passwordStrength,
} from "../../dist";
I expect all *.json files can be dynamically imported