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?
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 :)
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 creatingb) call init on the instance
auto.init()after doing so cloning should be possible.