bluray
bluray

Reputation: 1963

Angular2 - route with language

Hello i would like to create routes with language in this format:

www.domain.com/lang/sometimes

Example:

www.domain.com/en/sometimes
www.domain.com/de/sometimes

Is it possible write to route something like:

RouterModule.forChild({
   path: ':lang/sometimes', component: TestComponent
})

Is it possible? How to set to url default language? For example when app starting, set dynamically lang parameter to url.

Thank you for your advices

Upvotes: 7

Views: 7086

Answers (1)

nirazlatu
nirazlatu

Reputation: 993

You can do something like this then. You can create two routes, one for default route and another for other Routes.

 RouterModule.forChild([
  { path: 'english/users/sometimes', component: UserComponent, useAsDefault: true },
  { path: ':lang/users/sometimes', component: UserCOmponent }
])

Added: For subscribing to the param:

import { ActivatedRoute } from '@angular/router';

constructior(private route: ActivatedRoute)

ngOnInit(){
this.route.params.subscribe(value => {
    let lang = value['lang']);
    console.log(lang);    

});
}

Upvotes: 6

Related Questions