ngx-translate / http-loader

A loader for ngx-translate that loads translations with http calls
MIT License
193 stars 69 forks source link

How load translation file for each module #34

Closed launcelot66 closed 6 years ago

launcelot66 commented 6 years ago

I have app.module.ts like this:


@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    Routing,
    AuthModule,
    CatalogsModule,
    DashboardModule
  ],
  exports: [],
  declarations: [AppComponent, SvgIconComponent, NotFoundComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }
**AuthModule:**

function AuthHttpLoaderFactory(httpClient: HttpClient) {
  return new TranslateHttpLoader(httpClient, 'i18n/auth/', '.json');
}

@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: AuthHttpLoaderFactory,
        deps: [HttpClient]
      },
      missingTranslationHandler: [{provide: MissingTranslationHandler, useClass: TranslateHandler}]
    })
  ],
  declarations: [IndexComponent, ...],
  bootstrap: [IndexComponent],
  providers: [...]
})
export class AuthModule {}
**CatalogsModule:**

function CatalogsHttpLoaderFactory(httpClient: HttpClient) {
  return new TranslateHttpLoader(httpClient, 'i18n/catalogs/', '.json');
}

@NgModule({
  imports: [
    CommonModule,
    RouterModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: CatalogsHttpLoaderFactory,
        deps: [HttpClient]
      },
      missingTranslationHandler: [{provide: MissingTranslationHandler, useClass: TranslateHandler}]
    })
  ],
  declarations: [CatalogComponent, ClientComponent]
})
export class CatalogsModule {}
Now if i go to auth page i'm load `/i18n/catalogs/ru.json`, but should have been loaded `/i18n/auth/ru.json` If i swap places AuthModule & CatalogsModule in app.modules.ts - will be loaded `/i18n/auth/ru.json` (as expected). How to load current module translation file?
launcelot66 commented 6 years ago

answer is here -> github.com ngx-translate core issues 752