question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How do you export TranslateStaticLoader from a SharedModule

See original GitHub issue

So we have a pretty big project (Ionic 2) with lots of smaller ngModules.

We were hoping that you could export a preconfigured TranslateModule like so:

export function createTranslateLoader(http: Http) {
    return new TranslateStaticLoader(http, './assets/i18n', '.json');
}

@NgModule({
    declarations: [MomentDatePipe, NoTagsPipe, SpecialCharsPipe],
    exports: [
        IonicModule,
        MomentDatePipe,
        NoTagsPipe,
        SpecialCharsPipe,
        TranslateModule
    ],
    imports: [
        TranslateModule.forRoot({
            provide: TranslateLoader,
            useFactory: (createTranslateLoader),
            deps: [Http]
        })
    ],
    providers: [] // MUST NOT have any providers
})
export class SharedModule { }

Noticed that you wrote that one should not use forRoot in shared modules, so I’m aware that this probably is not a good idea, but for the purpose of example…

The translations work when other plugins import the shared module AND it is compiled using JiT. However, it doesn’t seem to work when compiled using AoT - the translations strings dont get translated.

TLDR; can you export a preconfigured TranslateModule from a SharedModule?

Thanks for reading

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ocombecommented, Feb 7, 2017

This is fixed in 6.0.0 with the new forChild method and the updated documentation 😃

1reaction
SamVerschuerencommented, Jan 11, 2017

From the docs

Note: Never call a forRoot static method in the SharedModule. You will end up with multiple different instances of a service in your injector tree.

Not sure if we could clarify it even more? The examples are also showing this.

What then, do sub-modules automatically get this pre-configuration if importing TranslationService?

Yes. What happens is that the pre-configuration configures the providers. If a sub-module can’t find an instance in it’s own injector when injecting a service, it will ask the parent if it has an instance for that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - [Question] How do you export TranslateStaticLoader ...
So we have a pretty big project (Ionic 2) with lots of smaller ngModules. We were hoping that you could export a preconfigured...
Read more >
Angular translate Pipe could not be found - Stack Overflow
The documentation only suggests having to export TranslateModule when using a SharedModule. · export the module allow using pipe in other modules ...
Read more >
Sharing modules - Angular
Creating shared modules allows you to organize and streamline your code. ... OrdersPipe, CommonModule, FormsModule ] }) export class SharedModule { }.
Read more >
Ionic view & ngx-translate problem
Hello, i am using ionic2 and the latest ngx translate for multi language ... Translation only works when I do “ionic lab” on...
Read more >
ngx-translate Documentation - CodeAndWeb
If you use a SharedModule that you import in multiple other feature modules, you can export the TranslateModule to make sure you don't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found