mashpie / i18n-node

Lightweight simple translation module for node.js / express.js with dynamic json storage. Uses common __('...') syntax in app and templates.
MIT License
3.08k stars 419 forks source link

Element with id="locale" and global registration crashes #481

Closed MadsAppvice closed 3 years ago

MadsAppvice commented 3 years ago

If the registration is set to global when using i18n on a webpage, "this" in the i18nTranslate function will be set to the window. If there is an element with the id="locale", the browser makes an easy reference to the element in the windows object, thus window.locale becomes the element.

This makes the getLocaleFromObject choose this element as the locale, which is obviously not the intention.

The workaround is to overwrite the reference to the element: window.locale = undefined; When initialising i18n.

A solution would be to add a check on the locale to be a string, and if there is a static catalog to make sure it is a part of that.

mashpie commented 3 years ago

this package is for server side only. And global should be used in single user cli for example.

MadsAppvice commented 3 years ago

Great, it works flawlessly as a translation engine for a chrome extension though:-) And this has been my only issue so far, which was pretty simple to work around.

mashpie commented 3 years ago

interesting :) so there is shim for fs and such?

MadsAppvice commented 3 years ago

Well I could imagine that could be done, but no. I simply used webpack to load language files and add them statically. The size of the project makes the size of the language files negligible.

But it would be doable to shim fs and make fetches instead with a little setup.

Cheers and thanks for a nice module:-)