-
-
Notifications
You must be signed in to change notification settings - Fork 588
TranslateService fails to load translations in standalone component within micro-frontend architecture #1539
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
Comments
I'm stuck with the same issue. I'm ussing Module Federation to build my microfrontend architecture. I'm configured ngx-translate in the app bootstraping files and I added i18n json files to each mfe. When I navigate to host app, translations don`t work but if I navigate to functional mfe url, I can see translations. I want each mfe translations to be independent from host. |
Can you please make a stand-alone demo in a GitHub repo for this? |
I have a similar issue. It is random. It feel like remote loaded component do not share the same instance of translateService. |
Hey @ap-sunilkamble! I'll try to help you solve your issue, but I'll need your help ;) 1. Move to standaloneReplace the old importProvidersFrom([
TranslateModule.forChild({
loader: {
provide: TranslateLoader,
useFactory: httpLoaderFactory,
deps: [HttpClient],
},
}),
]), With the new provideTranslateService({
loader: {
provide: TranslateLoader,
useFactory: httpLoaderFactory,
deps: [HttpClient]
}
}) 2. Replace .forChildFollow the following answer to handle per route load. Bonus. Instantiate sooner@stherrienaspnet This one could help you The following is known to be error prone, prefer calling export class StandaloneComponent implements OnInit {
constructor(private translate: TranslateService) {}
ngOnInit(): void {
this.translate.setDefaultLang('en');
this.translate.use('en');
}
} Why not directly inside the export function initApp(
translateService: TranslateService,
) {
return () => {
const fallbackLang = 'en';
const browserLang = translateService.getBrowserLang();
const usedLang = localStorage.getItem('LNG_KEY') || browserLang?.match(/en|fr|it/) ? browserLang : fallbackLang;
translateService.setDefaultLang(fallbackLang);
translateService.use(usedLang);
}
}
//...
providers: [
provideAppInitializer( async () => {
const translateService = inject(TranslateService) ;
initApp(translateService)();
}
] |
@rbalet @CodeAndWeb : https://github.com/sunilmkamble17/mfe_profile.git
https://github.com/sunilmkamble17/mfe_host.git
In this setup, both projects have their own/individual en.json files |
Uh oh!
There was an error while loading. Please reload this page.
We are encountering an issue where TranslateService does not load translations in a standalone component when used within a micro-frontend setup. The translation files are accessible directly via the browser, but the translations are not applied within the component.
I have the following setup for my microfrontend angular19 + native federation
host --running on-- localhost:4200
profile --running on-- localhost:4202
settings --running on-- localhost:4203
reports --running on-- localhost:4204
each project has its own/separate en.json
Current behavior
Navigating directly to the host app (e.g., http://localhost:4200) loads en.json and translations work. But if I navigate from host app to reports app the respective en.json from the reports app is not getting called.
Visiting http://localhost:4204/assets/i18n/en.json directly returns the JSON correctly.
TranslateService.use('en') is called in FindReportsComponent in the MFE.
MFE uses TranslateHttpLoader with import.meta.url to determine its own base path.
Expected behavior
When navigating to the reports app via the host app using native federation, the translation file should be fetched and The standalone component should display translated strings as defined in en.json
How do you think that we should fix this?
As per the documentation, it is understood that we can have two separate mechanisms,
TranslateModule.forRoot() and TranslateModule.forChild() here forChild() is not working as expected
Minimal reproduction of the problem with instructions
Set up a host Angular application.
Create a remote Angular application exposing a standalone component.
In the remote application's app.config.ts, configure the translation loader
Environment
Angular Version: 18
@ngx-translate/core Version: 16.0.4
@ngx-translate/http-loader Version: 16.0.4
Architecture: Micro-frontend using Angular's standalone components
Translation Files Location: /assets/i18n/en.json
Browser:
For Tooling issues:
package.json
The text was updated successfully, but these errors were encountered: