I18next: Cloning produces error "Cannot read property 'toResolveHierarchy' of undefined"

Created on 10 Jun 2017  Â·  6Comments  Â·  Source: i18next/i18next

I'm trying to create multiple instances of i18next with different lng:

import i18next from 'i18next'

const auto = i18next.createInstance ({
    debug: !!FocusCraft.config.debug,
    load: 'languageOnly',
    lng: navigator.language || navigator.userLanguage,
    fallbackLng: 'en',
    resources: {
        en: {
            hello: 'Hello World!',
        },
        ru: {
            hello: 'Привет мир!',
        },
    },
})

const locales = {
    en: auto.cloneInstance ({ lng: 'en' }),
    ru: auto.cloneInstance ({ lng: 'ru' }),
}

...But once it gets to cloneInstance(), i18next dies with an error:

i18next.js:273 Uncaught TypeError: Cannot read property 'toResolveHierarchy' of undefined
at setLng (i18next.js:273)
at I18n.changeLanguage (i18next.js:290)
at load (i18next.js:159)
at I18n.init (i18next.js:169)
at I18n.cloneInstance (i18next.js:392)
at Object.defineProperty.value (i18n.js:19)
at __webpack_require__ (bootstrap 06f5bf9…:19)
at Object.defineProperty.value (App.jsx:9)
at __webpack_require__ (bootstrap 06f5bf9…:19)
at Object.defineProperty.value (AppContainer.jsx:5)
at __webpack_require__ (bootstrap 06f5bf9…:19)
at Object. (main.jsx:11)
at __webpack_require__ (bootstrap 06f5bf9…:19)
at Object. (emitter.js:2)
at __webpack_require__ (bootstrap 06f5bf9…:19)
at bootstrap 06f5bf9…:65

In this case, I'd really like to keep several instances. I could just create them individually, but clones would be better, as they supposedly share resources. Am I using clones in a wrong way?

Most helpful comment

Calling i18next.createInstance(opts) will not initialize it.

a) pass a callback i18next.createInstance(opts, cb) so init will be called on creating
b) call init on the instance auto.init()

after doing so cloning should be possible.

All 6 comments

Calling i18next.createInstance(opts) will not initialize it.

a) pass a callback i18next.createInstance(opts, cb) so init will be called on creating
b) call init on the instance auto.init()

after doing so cloning should be possible.

@jamuhl Thanks ) You're right, it was written in documentation. Still, for me .cloneInstance ({ lng: 'ru' }) still doesn't work, even if I pass a callback. The only way I managed to get it to work was to do .changeLanguage ('ru') on the cloned instance.

hm...should work...wondering...will need to check doing some unit tests. Will do later as rather busy right now.

should be fixed in [email protected]

solved...

Thanks a lot :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

balazsorban44 picture balazsorban44  Â·  5Comments

dmorrison42 picture dmorrison42  Â·  7Comments

brightpixels picture brightpixels  Â·  8Comments

rahulthewall picture rahulthewall  Â·  4Comments

chiouchu picture chiouchu  Â·  4Comments