0

When I am changing the language the route gets appended to previous routes instead of just changing the language for exe- http://localhost:4200/en/about-us if change the language to he then it becomes http://localhost:4200/he/about-us/about-us I am changing the language like this.

constructor(
    public translate: TranslateService,
    private localize: LocalizeRouterService
){}
chooseLanguage('he') { 
        this.translate.use(lng);
        this.localize.changeLanguage(lng)
}

Please let me know if I am doing something wrong

Sheikh
  • 11
  • 1

1 Answers1

0
this.localize.changeLanguage(lng) here what you doing that might be affecting changing the route

if you using ngx-translate you can change the language just by using the below method

if you want to change the route as well checkout out this solution Angular2 - route with language

constructor(private translate: TranslateService) {
    translate.setDefaultLang('en'); // default language (in app.component)
  }
  
  useLanguage(language: string) {
    this.translate.use(language);
  }
PK2995
  • 143
  • 2
  • 9
  • I need to also changes route from previous language to the chosen one – Sheikh Jul 04 '22 at 08:36
  • why do you want to change the route – PK2995 Jul 04 '22 at 08:45
  • if you want to change route as well follow the below one while you navigating https://stackoverflow.com/questions/45096053/angular2-route-with-language – PK2995 Jul 04 '22 at 08:47
  • I am using https://github.com/gilsdav/ngx-translate-route for route management – Sheikh Jul 04 '22 at 09:24
  • have you followed this example https://stackblitz.com/edit/ngx-translate-routes-example?file=src%2Fapp%2Fapp.module.ts here we setting up this config in app.module so it will take all over the application once above one stackblitz working fine in your code then you can use the above angular route language change example – PK2995 Jul 04 '22 at 09:42
  • this example is translating the route but in my case I am putting a prefix in the route /en or /he – Sheikh Jul 04 '22 at 10:28
  • ngx-translate-route we using for that purpose only if you want to prefix go through this https://stackoverflow.com/questions/45096053/angular2-route-with-language – PK2995 Jul 05 '22 at 08:06