Search code examples
angularrouteslocalizationangular2-routingurl-routing

Angular 5 localize-router set route with country/lang code


Here is my problem. I have to set up routing in Angular so that in URL there will be a country code and a language. for example

https://example.com/usa/en

The tricky part is that this localization parts can change so I have a button to switch country and language so it will be /gb/en or /usa/fr.

How can I get it working like this using localize-router and ngx-translate.

Any help much appreciated.


Solution

  • Pardon me if I got your question wrong, but if I understand it correctly you can follow below approach:

    In your AppRoutingModule configure routes as below:

    const routes: Routes = [
      { 
         path:'' , pathMatch: 'full', component: SomeComponent,
         children: [
            { path: ':country-code/:lang-code' , component: SomeOtherComponent }
         ]
      }
    ]
    

    You can now redirect by your button click event and it changes country-code and lang-code accordingly and you can retrieve these values in your SomeOtherComponent to perform related tasks.