Skip to content

TranslateService fails to load translations in standalone component within micro-frontend architecture​ #1539

Open
@ap-sunilkamble

Description

@ap-sunilkamble

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

import { ApplicationConfig, importProvidersFrom } from '@angular/core';
import { provideRouter } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';

export const httpLoaderFactory = (http: HttpClient) =>
  new TranslateHttpLoader(http, './assets/i18n/', '.json');

export const appConfig: ApplicationConfig = {
  providers: [
    provideRouter([]),
    importProvidersFrom([
      TranslateModule.forChild({
        loader: {
          provide: TranslateLoader,
          useFactory: httpLoaderFactory,
          deps: [HttpClient],
        },
      }),
    ]),
  ],
};
  1. In the standalone component, inject TranslateService and set the default language:
import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-standalone-component',
  templateUrl: './standalone-component.component.html',
})
export class StandaloneComponent implements OnInit {
  constructor(private translate: TranslateService) {}

  ngOnInit(): void {
    this.translate.setDefaultLang('en');
    this.translate.use('en');
  }
}
  1. Expose the standalone component via Module Federation. 6. In the host application, navigate to the remote component.

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:

  • Chrome (desktop)

For Tooling issues:

  • Node version: v22.13.0
  • Platform: Windows

package.json

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions