引导应用程序angular2 / 4/6时,ngx转换在启动服务中不起作用

时间:2018-11-02 21:36:45

标签: angular internationalization translation ngx-translate

我有如下所示的应用程序模块,并且我有一个启动服务,该服务将在应用程序引导之前获取主数据。当我尝试在启动服务中警告翻译后的文本时,它不起作用。但是,如果我花一些时间,它会起作用。如何知道我们需要给多少时间。我猜翻译文件en.json,fr.json文件在我的启动服务调用以获取翻译时未加载。

imports: [
    // :CORE MODULE: //
    BrowserModule,
    BrowserAnimationsModule,
    HttpClientModule,
    **TranslateModule.forRoot({
        loader: {
            provide: TranslateLoader,
            useFactory: (createTranslateLoader),
            deps: [HttpClient]
        }
    }),**
    FormsModule,
    CommonModule, //<====added

    //:3RD PARTY MODULE://
    BootstrapModalModule,

    //:APPLICTION MODULES: //
    **AppLoadModule**, //Startupdata before APP loaded
    AppRoutingModule,
    FooterModule,
    ErrorModule,
    AccessDeniedModule,
    NotFoundModule,
    RouterModule.forRoot([]),
    ToasterModule.forChild(),
],

Appload模块

export function load(appLoadService: AppLoadService) {
  return () => appLoadService.load();
}

@NgModule({
  imports: [HttpClientModule],
  providers: [
    AppLoadService,
    {
      provide: APP_INITIALIZER,
      useFactory: load,
      deps: [AppLoadService],
      multi: true
    }
  ]
})
export class AppLoadModule {}

@Injectable()
export class AppLoadService {

    constructor(
        private httpClient: HttpClient,
        private alertService: AlertService,
        private injector: Injector,
        private $localStorage: LocalStorageService,
    ) { }

    public get router(): Router { //this creates router property on your service.
        return this.injector.get(Router);
    }

    public get translate(): TranslateService { //this creates router property on your service.
        return this.injector.get(TranslateService);
    }

    load(): Promise<any> {
        console.log('1.App Loader: STARTED', new Date().toLocaleTimeString());
        return new Promise(resolve => {
            const promise = this.httpClient.get('assets/settings/config.json')
                .subscribe(
                    res_config => {


                        setTimeout(() => {
                            this.translate.get('logout').subscribe((res: string) => {
                                alert(res); // Not Translating if I give setTimeOut Zero
                            });
                        }, 2000);

0 个答案:

没有答案