Using TranslateModule.forChild() in an Angular lib
See original GitHub issueCurrent behavior
I am trying to use ngx-translate inside an Angular 5 lib.
I have read from docs that “The forRoot static method is a convention that provides and configures services at the same time. Make sure you only call this method in the root module of your application, most of the time called AppModule”
So I thought that I should use Translate.forRoot() in my application and Translate.forChild() in my lib module
The problem is that when I use forRoot in my App and forChild in my lib I always get the following error:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[TranslateService -> TranslateStore]:
StaticInjectorError(Platform: core)[TranslateService -> TranslateStore]:
NullInjectorError: No provider for TranslateStore!
Error: StaticInjectorError(AppModule)[TranslateService -> TranslateStore]:
StaticInjectorError(Platform: core)[TranslateService -> TranslateStore]:
NullInjectorError: No provider for TranslateStore!
I have tested and it works when I use forRoot in application and lib, but I have to use this.translate.use(“myLanguage”) because seems I have two instances of TranslateService and this doesn’t seems to be the proper way.
Expected behavior
My lib should work using Translate.forChild({}) and my application using Translate.forRoot({})
How do you think that we should fix this?
Minimal reproduction of the problem with instructions
Application module
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/i18n/', '.json');
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient],
}
})
],
providers: [MyService],
bootstrap: [AppComponent]
})
Lib module
export function createTranslateLoader(http: HttpClient) {
return new TranslateHttpLoader(http, './assets/my-other-path/i18n/', '.json');
}
@NgModule({
imports: [
NoopAnimationsModule,
CommonModule
TranslateModule.forChild({
loader: {
provide: TranslateLoader,
useFactory: (createTranslateLoader),
deps: [HttpClient],
}
})
],
declarations: [
MessageButtonComponent,
],
exports: [
MessageButtonComponent
],
providers: [
TranslateService
],
entryComponents: [
]
})
Environment
ngx-translate version: 9.1.1
Angular version: core 5.2.0
Browser:
- [x] Chrome (desktop) version 67
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: v8.11.2
- Platform: Windows 10
Others:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:17
- Comments:18 (1 by maintainers)
Top Related StackOverflow Question
This worked for me, In tsconfig.ts of the main application add this to paths and restart your application:
Hi, faced this issue just yesterday with version 11.0.1 and Angular & CLI 8, what worked for me was:
Like state in the documentation in my shared module i had:
and in my AppModule, wich also imports the shared module , i add to provide the TranslateStore
Not sure if this is supposed to be done this way but this did resolve the original error message “NullInjectorError: No provider for TranslateStore!”.
But now everything is translated except the lazy loaded modules. (every component loaded inside the <router-outlet> had missing translations). I then notice there were at least two instances of the TranslateService, the one create for components outside of the router outlet, and another one for the lazy loaded modules.
What fixed this was setting the isolate: false when registering the translate module in my SharedModule.
This issue ultimately seems to depend a lot with the way the angular project and module registration is set up. In my case the SharedModule and the AppModule do share the same i18n files, but i do realize that if i want to get different i18n files for the SharedModule and the AppModule it does make sense to have different instances of the TranslateService and would have to register the the TranslateModule.forRoot in the AppModule and keep the flag isolate: true in my Shared Module
Hope this can help…