Cannot read property 'toResolveHierarchy' of undefined

Viewed 4011

I call the i18next change Language function by passing the language code (en-UK) like that :

var changeLng = function (varLng) {  
  i18next.changeLanguage('en', (err, t) => {
    if (err) return console.log('something went wrong loading', err);
    t('applog'); // -> same as i18next.t
  });
};

I got the following issue :

VM4081 i18next.js:1912 Uncaught TypeError: Cannot read property 'toResolveHierarchy' of undefined
    at setLng (VM4081 i18next.js:1912)
    at I18n.changeLanguage (VM4081 i18next.js:1927)
    at changeLng (VM4079 langUK.js:23)
    at HTMLImageElement.<anonymous> (VM4079 langUK.js:8)

I assume that Something going wrong when i18next tries to execute that :

_this4.languages = _this4.services.languageUtils.toResolveHierarchy(l);

But I can't see what's needed to properly execute this. Thanks in advance for your support, Jack

2 Answers

Did you initialize i18next properly? This code works for me:

i18next.init({
  lng: 'en',
  debug: true,
  resources: {
    en: {
      translation: {
        "key": "hello world"
      }
    }
  }
}, function(err, t) {
  // initialized and ready to go!
  console.log("Ready to go!");
});

var changeLng = function(varLng) {
  i18next.changeLanguage('en', (err, t) => {
    if (err) return console.log('something went wrong loading', err);
    t('applog'); // -> same as i18next.t
  });
};

changeLng()
<script src="https://unpkg.com/i18next/i18next.min.js"></script>

Edit: took this from the documentation

Maybe it is caused by a bad import of i18next or that it hasn't been inited, like "import i18n from 'i18next'". Run the init function firstly, then you can run the changeLanguage.

Related