I18next: Can't determine if i18next has been initialized post initialization.

Created on 5 Oct 2016  路  7Comments  路  Source: i18next/i18next

I am currently trying to determine if i18next has been initialized asynchronously prior to requesting translations.

For example:

i18next.init();

setTimeout(function () {
    i18next.on('initialized', function () {
        console.log('may not run?');
    }
}, 500);

This is had originally been implemented in #380, but was removed during the 2.0.0 rewrite.

Is there a way to detect if i18next is already initialized?

Personally I would prefer if the callback in i18next.on('initialized', callback) was run immediately if i18next has already been initialized.

Most helpful comment

I ended up doing something like this:

if(!i18next.isInitialized) {
    // wait for initalization
    i18next.on('initialized', mycodefunc);
} else {
    mycode();
}

All 7 comments

why not using i18next.init({}, callback) ?!?

the event 'initialized can be triggered n times...depending on how often init is called...so no way to get that if already run

but without the timeout it would work

i18next.init().on('initialized', () => {})

I have a helper library that I use to load a default i18next configuration, and do some auto-translation.

I only want to load the default configuration if a configuration has not already been loaded.

I would like to do this without passing additional information into my helper library.

why not just check if there is i18next.options has some keys?

That should work fine. I was originally testing for a different key, and it would show as initialized even though it had not yet finished.

Thank you!

why not just check if there is i18next.options has some keys?

How would I do that exactly? Can you give a very brief example?

the options can be found on i18next.options

// second init i like to be sure is done:
i18next.init({ second: true });

// check
i18next.on('initialized', () => {
  if (i18next.options && i18next.options.second) {
    // seems 2nd init was done
  }
});

I ended up doing something like this:

if(!i18next.isInitialized) {
    // wait for initalization
    i18next.on('initialized', mycodefunc);
} else {
    mycode();
}
Was this page helpful?
0 / 5 - 0 ratings