Create React App - loading i18n translations from relative path

2.6k Views Asked by At

My React app loads from a relative path by setting the homepage variable in package.json. However i18next-http-backend still tries to load the translations from http://localhost:3000/locales/en/translation.json instead of http://localhost:3000/myapp/locales/en/translation.json. I even tried setting the loadPath option (not sure of the syntax though), but that didn't work. Please see below:

i18n
  .use(Backend)
  .use(initReactI18next)
  .init({
    fallbackLng: 'en',
    debug: true,
    loadPath: `${process.env.PUBLIC_URL}/locales/{{lng}}/{{ns}}.json`,

    interpolation: {
      escapeValue: false // not needed for react as it escapes by default
    }
  })

How can I make this work?

1

There are 1 best solutions below

1
On BEST ANSWER

The loadPath needs to be set as part of the initialisation of the Backend, so you need to do the following...

i18n
  .use(Backend)
  .use(initReactI18next)
  .init({
    fallbackLng: 'en',
    debug: true,
    backend: {
        loadPath: `${process.env.PUBLIC_URL}/locales/{{lng}}/{{ns}}.json`,
    }
    interpolation: {
      escapeValue: false // not needed for react as it escapes by default
    }
  })