i was facing the same problem, and found a workaround.
I decided to publish angular locales data (coming from cldr) in an asset folder. Cldr data come from node_mopdules\@angular\common\locales (what you would have imported in typescript).
Just before the bootstrap, using APP_INITIALIZER, i load the currect culture data from assets given a specific locale_id.
The trick is to avoid using the import
key word, because dynamic imports are not support in EC2015.
To get the culture data from the js file, i wrote this 'dirty' code :
import { Injectable, Inject, LOCALE_ID } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class LoaderService {
constructor(protected httpClient: HttpClient,
@Inject(LOCALE_ID) protected locale: string) { }
private async loadAngularCulture(locale) {
let angularLocaleText = await this.httpClient.get(`assets/angular-locales/${locale}.js`).toPromise();
// extracting the part of the js code before the data,
// and i didn't need the plural so i just replace plural by null.
const startPos = angularLocaleText.indexOf('export default ');
angularLocaleText = 'return ' + angularLocaleText.substring(startPos + 15).replace('plural', null);
// The trick is here : to read cldr data, i use a function
const f = new Function(angularLocaleText);
const angularLocale = f();
// console.log(angularLocale);
// And now, just registrer the object you just created with the function
registerLocaleData(angularLocale);
}
And is my mainModule, using the APP_INITIALIZER :
export function configFactory(intlLoader: SfkIntlLoaderService) {
return intlLoader.initializer();
}
export function localFunction() {
// the rule to get you language according.
// Here, for demo, i just read it from localstorage
return localStorage.getItem('LANGUGAGE');
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HttpClientModule
)
],
providers: [
LoaderService,
{
provide: LOCALE_ID,
deps: [],
useFactory: localFunction
},
{
provide: APP_INITIALIZER,
useFactory: configFactory,
deps: [LoaderService],
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
Hope this will help !