stefalda / react-localization

Simple module to localize the React interface using the same syntax used in the ReactNativeLocalization module.
MIT License
373 stars 58 forks source link

library mutating locale objects? #132

Open x3malmusic opened 2 years ago

x3malmusic commented 2 years ago

so let's say i have a locale object with two languages that have one key "data". This data is equal to an array of objects, something like this: { en: { data: [{ id: 1 }, {id: 2}] }, es: { data: [{id: 3}] } }

and i have my own mechanism which injects locale based on the route in my app, so on each page i'm doing setContent and setLanguage when change route or changing language.

So the weird thing is when change language i got a plenty of warnings looking something like this:

key '1' not found in localizedStrings for language id and this warnings appeared only if locale has an array of objects

i've started to wonder why is that (although everything just working fine)? After spending some time i've found out that my locale object has changed when i've changed locale and instead of this:

{ en: { data: [{ id: 1 }, {id: 2}] }, es: { data: [{id: 3}] } }

i got this:

{ en: { data: [{ id: 1 }, {id: 2}] }, es: { data: [{id: 3}, {id: 2}] } }

as you can see the lib has concatinated an array from the 'es' locale. So i changed data in 'es', from array to just string '1' and i got an error when i tried to change language. Further digging in your code showed me that you have a some kind of fallback values if one of values from the default locale is missing, which i guess is fine... if only it was inside of a LocalizedStrings object without changing the locale object itself.

Ok when this happened first i thought maybe i did something wrong when injecting locales or whatever, but no, when i changed a bit my code and start using just plain locale objects, my app had continued to work and contain a correct data in my locales without changing them.

So if it a bug or feature? Who knows, but mutating objects that is passed to methods is definitely not a good idea

stefalda commented 2 years ago

Hi, it's a feature! the idea is to have a fallback for missing keys...

x3malmusic commented 2 years ago

but this is really weird behavior) i mean u passing object to method and then that object is changed for whatever reason...

so mb adding some option for having or not having that kind of behavior is a good idea? because what if i intentionally want to have a different set of options for different locales? This fallback feature makes this impossible to do =(