Search code examples
angularroutesmultilingual

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.


Solution

  • 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);
      }
    });