I trying to set up i18next inside a monorepo using NX and Module Federation and I’m getting 404, I’m using i18next-http-backend to load the translations async.
I’ve tried to put public/locales/en/translation.json inside the host app, inside the root of the project, and so on. None of them worked for me.
import i18n from 'i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import { initReactI18next } from 'react-i18next';
import HttpApi, { HttpBackendOptions } from 'i18next-http-backend';
i18n
// loads translations from your server
// https://github.com/i18next/i18next-http-backend
.use(HttpApi)
// detect user language
// learn more: https://github.com/i18next/i18next-browser-languageDetector
.use(LanguageDetector)
// pass the i18n instance to react-i18next.
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init<HttpBackendOptions>({
// resources,
fallbackLng: 'pt-BR',
debug: true,
load: 'currentOnly',
supportedLngs: ['en', 'pt-BR'],
detection: {
lookupCookie: 'i18nextLng',
lookupLocalStorage: 'i18nextLng',
order: ['localStorage', 'cookie'],
caches: ['localStorage', 'cookie'],
},
interpolation: {
escapeValue: false,
},
backend: {
loadPath: '../../../../public/locales/{{lng}}/{{ns}}.json',
},
});
export default i18n;
2
Answers
In my experience, i18next uses the path of wherever your main file that is served lives. So in this case, i18next makes a request to localhost:4200/public/…, where localhost:4200 is the root of the specific project you served with nx. So you would have to move your public folder into the same level as the main file of the served application.
Replace with your path