@ngx-translate with lazy-loaded module in Angular 5

Saeb Panahifar picture Saeb Panahifar · Jan 24, 2018 · Viewed 9.3k times · Source

I'm using @ngx-translate for language handling in an Angular 5 app I'm creating. The app has two feature modules, one lazy loaded and one eager loaded.

The problem is that the translate pipe works fine in the eager-loaded module but not the lazy-loaded one. How can I fix that?

Answer

DanielWaw picture DanielWaw · May 18, 2018

In my lazyload modules i had to add this to imports:

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

also in lazyloaded component i did something like that:

import {TranslateService} from '@ngx-translate/core';

in constructor:

private translate: TranslateService

and finally onInit:

this.translate.use(language);

And it is working just fine.