14

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?

2

4 Answers 4

12

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.

Sign up to request clarification or add additional context in comments.

8 Comments

this should be the correct answer...because in this case we'll be using forChild method used only for lazy loading modules with ngx-translate and as appear in the docs. In my case i was missing the injection part in the component, and that is not described in the docs.
@Takatalvi What do you mean with injection part?
i can't remember clearly right now but reading the code i think the injection part was use the setDefaultLang with the language used in the main module, in my case was stored in localstorage, and then use it in the component of that module. IE: this.translate.setDefaultLang(lang); this.translate.use(lang);
this load first time only, if you changed the lang through ui component, will not working
@DanielWaw This was a complete life saver! I've been stuck on trying to make this work for a whole day. This works like a charm! Definitely the fix I've been looking for.
|
10

I've also been struggling with the same problem and have yet to find a feasible answer.

The kind folks at Angular are working on i18n, but this may take more time.

While not ideal, you might want to check out the following article:

“How to split your i18n file per lazy loaded module with ngx-translate?” @frogeret https://medium.com/@TuiZ/how-to-split-your-i18n-file-per-lazy-loaded-module-with-ngx-translate-3caef57a738f

1 Comment

I am able to run the app in dev mode i.e. when I run the app as ng serve but, I'm unable to build the app in prod mode i.e. ng build --prod. Any suggestion?
0

You can check the ngstack/translate library that works for Angular and Ionic apps. Also provides support for lazy loading, page title translations, custom pipes and many other great features.

Comments

0

To implement @ngx-translate with lazy-loaded modules in an Angular application, you have to import TranslateModule twice: once in the main module as the root and once in each lazy-loaded module (or use a shared module) as a child.

For example:

app.module.ts

@NgModule({
    imports: [
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: (createTranslateLoader),
                deps: [HttpClient]
            }
        })
    ]
})

lazy-loaded.module.ts

@NgModule({
    imports: [
        TranslateModule.forChild()
    ]
})

Then, the translate pipe will work fine in both eager-loaded and lazy-loaded components.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.