1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule, BrowserAnimationsModule, FormsModule, ReactiveFormsModule, HttpClientModule, TranslateModule.forRoot({ loader: { provide: TranslateLoader, useFactory: (http: HttpClient) => new TranslateHttpLoader(http, './assets/i18n/', '.json'), deps: [HttpClient] }, defaultLanguage: environment.locale }), ], providers: [{ provide: APP_INITIALIZER, useFactory: appInitializerFactory, deps: [TranslateService, Injector], multi: true }], bootstrap: [AppComponent] }) export class AppModule { }
export function appInitializerFactory(translate: TranslateService, injector: Injector) { return () => new Promise<any>((resolve: any) => { // console.log('appInitializerFactory--'); const locationInitialized = injector.get(LOCATION_INITIALIZED, Promise.resolve(null)); locationInitialized.then(() => { const langToSet = environment.locale; translate.setDefaultLang(environment.locale); translate.use(langToSet).subscribe(() => { // console.info(`Successfully initialized '${langToSet}' language.'`); }, err => { // console.error(`Problem with '${langToSet}' language initialization.'`); }, () => { resolve(null); }); }); }); }
|