'How to load locales in Angular 5

I use NgLocaleLocalization, which is part of the i18n code in Angular. Moving from Angular 4 to 5 I now need to do something like this, as detailed in the docs:

import { registerLocaleData } from '@angular/common';
import localeFr from '@angular/common/locales/fr';

// the second parameter 'fr' is optional
registerLocaleData(localeFr, 'fr');

Which is great for one language. I have loads more (around 50).

Is this a sensible way to do it?:

import locale_ar from "@angular/common/locales/ar";
import locale_bg from "@angular/common/locales/bg";
import locale_cs from "@angular/common/locales/cs";
import locale_da from "@angular/common/locales/da";
// around 45 more import statements
import locale_sv from "@angular/common/locales/sv";


const locales: [string, (string | number | string[] | string[][] | number[] | ((n: number) => number))[]][] = [
    ["ar", locale_ar], ["bg", locale_bg], ["cs", locale_cs], ["da", locale_da],
    // around 45 more items here
    ["sv-SE", locale_sv]
];

(() => {
    for (const locale of locales) {
        registerLocaleData(locale[1], locale[0]);
    }
})();

This, in my opinion, looks a little crazy. But is it the right thing to do?

Also, is there any reason I should defer calling registerLocaleData until I know I need it or is it okay registering them here all at once?

If I do defer registering, do I need to keep track so I don't double register?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source