angular-dynamic-locale icon indicating copy to clipboard operation
angular-dynamic-locale copied to clipboard

Properties assigned to $locale by other decorators are being removed

Open mil-mly opened this issue 10 years ago • 3 comments

Hello everybody :-)

I've found a problem on lines 83-85:

if (!newObject[key]) {
  delete oldObject[key];
}

This code removes all the properties of oldObject that does not exist in newObject, which removes properties assigned by other decorators of $locale service. Is this desired behavior?

mil-mly avatar Jun 16 '15 09:06 mil-mly

This is the desired behavior. Say you added some properties to some of the server side $locale files, then when the locale changes, then it should reflect the $locale as it comes from the server. I think there are two options:

  • Modify the locale files server side
  • Have a whitelist of properties that are not modified

lgalfaso avatar Jun 16 '15 10:06 lgalfaso

But this will also remove all the properties you set on $locale in other decorators. Consider following code:

$provide.decorator('$locale', function ($delegate) {
  $delegate.myProperty = true;
  return $delegate;
});

I would expect to have $locale decorated by myProperty, but then comes tmh.dynamicLocale which removes the property as it does not exist in newly loaded $locale.

mil-mly avatar Jun 16 '15 10:06 mil-mly

There is no way for this library to know if a property was added by a decorator or it is part of the original $locale. Do you expect that if you override any of the existing values using a decorator that these remain?

This is not a simple question that has a generic answer with a solution that works for everybody. I would recommend that you do

$provide.decorator('$locale', function modifyLocale($delegate) {
  $delegate.myProperty = true;
  return $delegate;
});

and then, when you set the locale, to do

tmhDynamicLocale.set('it').then(modifyLocale);

The other option would be to add interceptors at the tmhDynamicLocaleProvider level

lgalfaso avatar Jun 16 '15 13:06 lgalfaso