question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't find a way to make it work, error with 'options' or 'getFixedT'

See original GitHub issue

Hi,

I’m trying to configure i18next with my React project and I can’t make it work at all…

Whatever I try, I get either this :

TypeError: Cannot read property 'options' of undefined
    at new Translate (/Users/ank49/Documents/React Projects/Portfolio React i18n/node_modules/react-i18next/dist/commonjs/translate.js:65:32)

or this :

TypeError: Cannot read property 'getFixedT' of undefined
    at Translate.componentWillMount (/Users/ank49/Documents/React Projects/Portfolio React i18n/node_modules/react-i18next/dist/commonjs/translate.js:84:46)

I initialized my i18n with the React and WebPack 2 example as the following :

import i18n from 'i18next'
import LanguageDetector from 'i18next-browser-languagedetector'

i18n.use(LanguageDetector)
    .init({
      loadPath:    'locales/{{lng}}/{{ns}}.json',
      preload:     ['en-US', 'fr-FR'],
      fallbackLng: 'fr-FR',
      // wait: true, // globally set to wait for loaded translations in translate hoc

      // have a common namespace used around the full app
      ns: ['common',
        'navbar',
        'footer',
        'header',
        'about',
        'experience',
        'skills',
        'p_app',
        'p_web',
        'cv',
        'social',
        'events',
        'contact',
        'support'],

      defaultNS: 'common',

      debug: true,

      // cache: {
      //   enabled: true
      // },

      interpolation: {
        escapeValue: false, // not needed for react!!
      },
      wait:          true,
    })

export default i18n

and my component I’m trying to translate is as follow :

import React, { Component } from 'react'
import PropTypes from 'prop-types'
import withStyles from 'isomorphic-style-loader/lib/withStyles'
import Typist from 'react-typist'
import { translate } from 'react-i18next'
import { Parenthesis } from '../../components'

import style from './header.scss'

class Header extends Component {
  static propTypes = {
    t: PropTypes.func.isRequired,
  }

  render() {
    const { t } = this.props

    return (
      <div>

        <section className="header">
          <div className="header_content">
            <div className="header_content_inner">
              <h1 id="homeTextHead">{t('header.title')}</h1>
              <hr className="bg_dark"/>
              <br/>
              <Typist avgTypingDelay={90} className="typed_text">
                {t('header.name')}
                <br/>
                <Parenthesis>{t('header.pseudo')}</Parenthesis>
                <br/>
                <br/>
                {t('header.description')}
              </Typist>
            </div>
          </div>
        </section>

      </div>
    )
  }
}

export default withStyles(style)(translate(['header'], { wait: true })(Header))

I also tried with the decorator but I got the same issues…

@translate(['header'])
class Header extends Component {
...
}

export default withStyles(style)(Header)

Any help is welcome, thanks in advance guys !

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:36 (18 by maintainers)

github_iconTop GitHub Comments

10reactions
juan-manuel-alberrocommented, Feb 15, 2018

If anyone is having the error XMLHttpRequest is not defined using gatsby build, just install the package npm i -S xmlhttprequest and then in the top of gatsby-node.js file add:

// XMLHttpRequest polyfill
global.XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

And now the build command works as expected, you can verify it by running gatsby serve.

7reactions
seduboiscommented, Nov 15, 2017

I’ve read that, but it’s quite cumbersome. The closest I got is detailed below. I had to fuse all translation files into one per language otherwise it was beyond my webpack skills, but that defeats the point of loading just the relevant component’s translations by XHR. Besides, even with this I still have warnings during gatsby build. I think all these implementation details should be part of the package.

// src/utils/i18n.js, make sure to just import it from somewhere
import i18n from 'i18next'
import XHRBackend from 'i18next-xhr-backend'
import LanguageDetector from 'i18next-browser-languagedetector'
import { reactI18nextModule } from 'react-i18next'

const config = {
  fallbackLng: 'en',
  interpolation: {
    escapeValue: false, // code escaping in interpolated text is not needed with react
  },
  react: {
    wait: false,
  },
}

if(module.hot) {
  // config.debug = true
  config.resources = require('../locales')

  const translations = ['../locales/en.json', '../locales/fr.json']
  module.hot.accept(translations, () => reloadTranslations())
} else {
  i18n.use(XHRBackend)
}

i18n
  .use(LanguageDetector)
  .use(reactI18nextModule)
  .init(config)

function reloadTranslations() {
  const translations = require('../locales')
  Object.keys(translations).forEach(lang => {
    Object.keys(translations[lang]).forEach(namespace => {
      i18n.addResourceBundle(
        lang,
        namespace,
        translations[lang][namespace],
        true,
        true
      )
    })
  })

  i18n.emit('loaded')
}

export default i18n



// src/locales/index.js
const en = require('./en.json')
const fr = require('./fr.json')
export default { en, fr }

// src/locales/en.json
{
  "MyComponent": {
    "key1": "value1",
    ...
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

i18next + React + Webpack - getFixedT is not a function
My initial problem was loading the locales. The i18n couldn't find the files after the build, because the webpack doesn't recognize the i18n-xhr ......
Read more >
API - i18next documentation
init. i18next.init(options, callback) // -> returns a Promise. The default export of the i18next module is an i18next instance ready to be initialized...
Read more >
i18next + React + Webpack - getFixedT is not a function-Reactjs
I'm Trying to Inject An NPM package As A Plugin on A Next. · How do i pass a variable to multiple layers...
Read more >
How to use the i18next.isInitialized function in i18next - Snyk
isInitialized function in i18next. To help you get started, we've selected a few i18next examples, based on popular ways it is used in...
Read more >
next-i18next - npm
The easiest way to translate your NextJs apps. ... How does it work? ... First, create a next-i18next.config.js file in the root of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found