0

I have this in a service- I18nProviderService:

createTranslationProviders(response) {
  const locale = 'de';
  let translationProvider: any;
  translationProvider = [{
      provide: TRANSLATIONS,
      useValue: response
    },
    {
      provide: TRANSLATIONS_FORMAT,
      useValue: 'xlf'
    },
    {
      provide: LOCALE_ID,
      useValue: locale
    }
  ];

  return translationProvider;
}

getTranslationProvider() {

  this.getTranslationFile()
    .subscribe(
      (response: Response) => this.createTranslationProviders(response),
      (error) => console.log(error)
    );
}

and this in my app.module.ts

export function startupServiceFactory(myService: I18nProviderService): Function {
  return () => myService.getTranslationProvider();
}



@NgModule({
  declarations: [

  ],
  imports: [
    BrowserModule,
    ReactiveFormsModule,
    HttpModule,
    NguiAutoCompleteModule
  ],
  providers: [I18nProviderService,
    {
      provide: APP_INITIALIZER,
      useFactory: (myService: I18nProviderService) => () => myService.getTranslationProvider(),
      deps: [I18nProviderService],
      multi: true
    },
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Why is the TRANSLATIONS_FORMAT, LOCALE_ID and TRANSLATIONS not mapped correctly. Means, if I trace the LOCALE_ID in my app.component.ts it has the default value.. not "de". I like to load my xlf translation file before app is init.

shammelburg
  • 6,974
  • 7
  • 26
  • 34
KRav
  • 47
  • 10
  • You do not want to `.subscribe` inside your service. That code belongs in a consuming component. Preferably on `OnInit` and releasing the subscription with `OnDestroy` as to prevent leaks. – Neil Lunn May 11 '17 at 09:10
  • You should return `Promise` from `getTranslationProvider` and you can't declare providers this way. See http://stackoverflow.com/questions/43639136/angular4-app-initializer-wont-delay-initialization – yurzui May 11 '17 at 09:15
  • I created a plunker: http://plnkr.co/edit/dNYEJvvysKfy9AD2ObzN?p=preview But my new local id is not set – KRav May 28 '17 at 17:15

0 Answers0