tolgee / tolgee-js

Tolgee JavaScript libraries monorepo
https://tolgee.io
MIT License
218 stars 24 forks source link

i18next integration fails to fetch some translations due to how resources are loaded on update #3305

Closed perebusquets closed 3 months ago

perebusquets commented 3 months ago

Hi, I'm opening this issue to discuss the fact that, for some reason, the i18next-tolgee adapter is failing to load some translations.

After some debugging, I figured out the issue was here:

        tolgee.getAllRecords().forEach(({ language, namespace, data }) => {
            if (i18n.getResourceBundle(language, namespace)) {
                i18n.removeResourceBundle(language, namespace);
                i18n.addResources(language, namespace, Object.fromEntries(data));
            }
        });

and it spats out the following error: image

I'm not sure why only some translations fail (maybe a race condition on the side of i18next due to eliminating and recreating them one after the other?).

Anyway, looking at the i18next documentation, I saw that there's the addResourceBundle function for adding/overwriting resources, so with the changes proposed in this PR it works like a charm.

https://github.com/tolgee/tolgee-js/pull/3304

Please let me know if I can help with anything to accelerate solving this issue, and thank you for your awesome platform! ;)