Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup react-i18next with NX Monorepo

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;

enter image description here

enter image description here

like image 611
niltonxp Avatar asked Feb 03 '26 17:02

niltonxp


1 Answers

import resourcesToBackend from 'i18next-resources-to-backend';
i18n 
.use(resourcesToBackend((language: string, namespace: string) => import(`../locales/${language}/${namespace}.json`)));

Replace with your path

like image 156
Vladislav Avatar answered Feb 05 '26 05:02

Vladislav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!