i18next-xhr-backend icon indicating copy to clipboard operation
i18next-xhr-backend copied to clipboard

How to listen to onInitialized event even if it occurred before my code ran

Open bepetersn opened this issue 5 years ago • 2 comments

Hi, I have noticed some annoying behavior which is as follows:

I listen for the initialized event (i18next.on('initialized')) to make sure that everything is setup before my app's code runs -- I need to make sure my translations are there. However, if my code gets run at a point after the initialized event occurs, it never runs at all, because it is inside this check.

Is there an easy way to listen for the event to fire or if it has already fired, then do something? I read this is how addEventListener functions and I wish I could just get the same behavior with on.

Thanks

bepetersn avatar Apr 01 '19 21:04 bepetersn

Solved this myself. I checked for isInitialized, and if it's not set, listen to onInitialized event, otherwise if it is set, everything should be fine, IF you only care about the fallbackLng being loaded. I didn't need to worry about the other possibility myself.

bepetersn avatar Apr 01 '19 22:04 bepetersn

in react-i18next our check looks like this for called loadNamespace: https://github.com/i18next/react-i18next/blob/master/src/utils.js#L24

jamuhl avatar Apr 02 '19 05:04 jamuhl