ngx-translate custom loader doesn't find translations

2.3k views Asked by At

I decided to implement custom loader for translations because I have an issue that string want to get translated before loader loads correct translations. I followed example on ngx-translate github but now everywhere I get keys instead of values. My custom loader:

    class CustomLoader implements TranslateLoader {
  getTranslation(lang: string): Observable<any> {
    return Observable.of({ KEY: 'value' });
  }
}

in app.module imports I have:

    TranslateModule.forRoot({
  loader: { provide: TranslateLoader, useClass: CustomLoader }
})

My translations are in ./assets/i18n/ and are all json. Anyone know what I need to change so loader will find my translations?

1

There are 1 answers

1
rick On

Try this

export function HttpLoaderFactory(http: Http) {
  return new TranslateHttpLoader(http, "./assets/i18n/locale-", ".json");
} 

and then

TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [Http]
      }
    })