Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Middleware in angular 7?

Is there like laravel middleware in angular 7 or how to set language of the site through route? (https://example.com/en/about, https://example.com/de/about)

P.S. Currently I'm using this method for multilingual.

like image 480
Abduhafiz Avatar asked Apr 02 '26 03:04

Abduhafiz


1 Answers

You could subscribe to the router events, and on NavigationStart change the used language in your mentioned translationService approach, though I strongly recommend you to use ngx-translate

this.router.events.subscribe((event: any): void => {
  if (event instanceof NavigationStart) {
     /* You should parse for the language code in the route
       here and use it in the following line. Also make sure that the parsed
       language code is an actual language code */
     this.translationService.use(languageCode).then(() => // do your stuff);
  }
});
like image 164
Julien Ambos Avatar answered Apr 08 '26 06:04

Julien Ambos