Not all AngularJS filters are $locale aware after lang+locale changed #9159
Description
AFFECTS: All AngularJS versions, including latest master.
REPRODUCEABLE: Always, any browser or OS
PREREQUISITES
Everybody is aware of the AngularJS applications start phases, in particular the config phase which allows developers to load/decorate/prepare different items.
Still, there is a real need in AngularJS to lazy load stuff (modules, ctrls, locale, *).
BUG/PROBLEM INSIDE ANGULARJS
AngularJS filters are using the $locale in order to format different types of data: numbers, currencies, dates, date times, etc. The problem is that some of these filters have locally cached versions of the $locale and do not take the latest updated $locale service.
- Number filter - https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js#L118
- Currency filter - https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js#L53
(others might as well have this problem)
POSSIBLE SOLUTION
All the filters should be $locale aware, as the date filter for instance ( https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js#L444 ). This one doesn't cache the $locale.{WHATEVER} and it works great.
.. AND THE CODE
Move the " var formats = $locale.NUMBER_FORMATS;" inside the returned function:
function numberFilter($locale) {
return function(number, fractionSize) {
var formats = $locale.NUMBER_FORMATS;
return formatNumber(number, formats.PATTERNS[0], formats.GROUP_SEP, formats.DECIMAL_SEP,
fractionSize);
};
}
REAL USE CASE:
We have an AngularJS application (which will go in production in a few weeks FYI) which requires loading of i18n look&feel on the fly (based on the language + culture combination). So if the user changes his lang_locale or just logs in with another account in the app, we would load the user lang_locale and rely on AngularJS for the number/currency/dates formatting.
ACTUAL IMPLEMENTATION
We decorated the $locale service and created a "setLocale" method which allows changing the $locale properties after the config phase of the app.
NOTE: Another title for this bug could have been: "Dynamically loading $locale files without refreshing the whole page"