Skip to content

Question Regarding Lazy Loading with new Angular 17+ API #1538

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

Open
Galileon-venta opened this issue Apr 9, 2025 · 3 comments
Open

Question Regarding Lazy Loading with new Angular 17+ API #1538

Galileon-venta opened this issue Apr 9, 2025 · 3 comments

Comments

@Galileon-venta
Copy link

With Standalone Components are there any resources regarding lazy loading additional Translations for lazy loaded Components? Is it still only possible using importProvidersFrom()?

@rbalet
Copy link
Collaborator

rbalet commented May 15, 2025

Hey @Galileon-venta.
That is a really good question.

there is (yet ; ) no official way to support it and if wished I could do a PR, but here is a way you could deal with lazy loading in Angular 17+

1. Create a resolver

export const HomeTranslationsResolver: ResolveFn<void> = async () => {
  const http = inject(HttpClient);
  const translate = inject(TranslateService);
  const loader = new TranslateHttpLoader(http, './assets/i18n/home/', '.json');

  loader.getTranslation(translate.currentLang).subscribe((translations) => {
    translate.setTranslation(translate.currentLang, translations, true);
    // True: merges all files together
    // False: Replace the files
  })
};

2. use it int he route where you'd need it.

// ...
{
  path: 'home',
  resolve: {
     HomeTranslationsResolver,
  },
  loadComponent: () => /...
}

Please, close this issue if it does answer your question ; )

Happy coding <3

Note: HttpBackend

If this PRs get accepted, you'll have to use inject(HttpBackend) instead ;)


@CodeAndWeb Should I create a new resolver ?

  • Yes: what about adding it inside the http-loader library ?
  • No: Would you find a separate library handy ?

@Galileon-venta
Copy link
Author

Interesting approach; Without testing it in actual code, it seems to me that this would not include live language change possibilities but rather would require a page reload while changing the language. I assume it's not that hard to add a subscription for the language change here, but it offers potential problems to keep in mind when it comes to clean-up. Would love to see an official suggestion to reduce the possible varieties of implementation here.

@rbalet
Copy link
Collaborator

rbalet commented May 27, 2025

@Galileon-venta good catch, I'll analyse the code, modify it and see if I can get a PR to add official support for this kind of resolver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants