0

enter image description here

Below are my index.js and i18next.js(the config for i18n)

index.js

import React, { Suspense } from 'react'
import i18n from './i18next'
import './i18next'
import ReactDOM from 'react-dom'
import App from './App'
import * as serviceWorker from './serviceWorker'
import Loading from './components/Styled/Loading/Loading'

i18n.init().then(() =>
  ReactDOM.render(
    <React.StrictMode>
      <Suspense fallback={<Loading />}>
        <App />
      </Suspense>
    </React.StrictMode>,
    document.getElementById('root')
  )
)

i18next.js

import i18n from 'i18next'
import { initReactI18next } from 'react-i18next'

import Backend from 'i18next-http-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
// not like to use this?
// have a look at the Quick start guide
// for passing in lng and translations on init

const Languages = ['en', 'fr']

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: 'en',
    debug: true,
    whitelist: Languages,
    keySeparator: false,
    defaultNS: 'translation',
    ns: ['translation'],
    backend: {
      loadPath: `/locales/{{lng}}/{{ns}}.json`,
    },
    load: 'unspecific',
    react: {
      wait: true,
    },    
    interpolation: {
      escapeValue: false,
    },
  })

export default i18n

console enter image description here

So, here's the gist. I use the useTranslation hook in my components and it works perfectly fine in localhost. However, I constantly keep getting the missingKey error on production where the build is deployed. Have tried all the feasible settings from other answers, without success. Apologies for the huge content, I just wanted to be thorough.

nimsrules
  • 1,947
  • 17
  • 21

1 Answers1

0

Figured out the solution. Because I'm serving the build in a sub-directory, I had to append it to loadPath.

.env

REACT_APP_VERSION=$npm_package_version
REACT_APP_PUBLIC_URL='/dy/'

i18next.js

backend: {
  loadPath: `${process.env.PUBLIC_URL}/locales/{{lng}}/{{ns}}.json`,
}
nimsrules
  • 1,947
  • 17
  • 21