-
-
Notifications
You must be signed in to change notification settings - Fork 585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to load separate translation files per module and per component in angular 5 ? #886
Comments
if you search around there are some solutions but those do not work, I am also having this problem and so far i can not find any way around it. |
Hi @Vishnu0522 |
@adelloste Using this approach, I run into the issue described here #876 Do you know a way around this issue? |
How to load translations per module is described in the documentation. If you want to load on a component by component basis you can try something like this: import { Component, OnInit } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; export const EN_TRANSLATIONS = { @component({ translationsUrl = 'assets/i18n'; constructor(private translate: TranslateService, private http: HttpClient) { ngOnInit() { loadTranslations(locale: string) { Tested w/ Angular 6 |
Here is well explained. However, I didn't get it working in my project. Seems that even with the flag |
Experiencing the same as @david-dlc-cerezo, the xhr call is only made for the file specified in the forRoot factory, the lazy-loaded child modules factories are called, but XHR request does not fire. As of, translation service only has the root translations. |
@shane-arthur I finally kind of get it working but I'm not sure how/why 🤣 One thing I did it was to add @NgModule({
imports: [
...,
TranslateModule.forChild({
loader: {
provide: TranslateLoader,
useFactory: MyImporterFactory
},
isolate: true
}),
...
],
providers: [
...,
TranslateService
]
})
export class MyModule {} But I can ensure this was what finally isolated the Angular documentation about providers says:
The original @Vishnu0522 question asked also about loading a different set of translations for each module... Well if for a module you should provide a |
I just tested a PoC about that... and worked! 🎉 To have a different class MyTranslateLoader implements TranslateLoader {
constructor() {}
public getTranslation(lang: string): Observable<any> {
const translations = ...// Obtain your translations as you wish
return of(translations);
}
}
// AoT requires an exported function for factories
export function MyTranslateLoaderFactory() {
return new MyTranslateLoader();
}
@Component({
selector: 'my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.scss'],
providers: [
TranslateService,
{
provide: TranslateLoader,
useFactory: MyTranslateLoaderFactory
}
]
})
export class SchoolEditionComponent implements OnInit {
constructor(
private readonly translate: TranslateService,
) {}
ngOnInit() {
// This will show the loaded translations
this.translate.getTranslation('en').subscribe(translations => console.log(translations));
}
} |
@david-dlc-cerezo the solution from Medium was working absolutely fine earlier but now it doesn't work. I have Translate service provided in lazy loaded module as well. but unable to split the translate file per module.Isolate: true does not work at all. This is in angular 6 as well as 7. It was working perfectly fine not sure what affected it and where. |
I was struggling with the same issue and got it working when using isolate: true and setting a default language(and current if needed) again. For app module:
For lazy module:
For setting a current langauge I'm using this in a module container component:
I'm not using http loader due to browser cache issues. |
@akiik I didn't know what you meant by "a module container component" so I initialize the TranslateService inside the constructor of my lazy-loaded module and it worked!!!! |
In case you use: ps.: isolation=true didn't worked for me. Maybe because of lazy-loading sub-modules which contains the components. |
Expected/desired behavior
I am new in angular , Our application is large and we don't want to load whole translation in single call . We want to load translation in per module as well as per component wise.
My environment is :
Angular CLI: 6.0.8
Node: 8.10.0
typescript 2.7.2
The text was updated successfully, but these errors were encountered: