7

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

1
  • 1
    It's unclear what you're asking. Commented Jul 14, 2017 at 6:25

1 Answer 1

6

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

});
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks and how i get (or set) :lang parameter from url?
For accessing the routes from other components, you can do like this: this.lang = someLanguage; this.router.navigate([this.lang,'users','sometimes']);
If you need to get the 'lang' parameter from the url. You can subscribe the params on ngOnInit of any component to where you want it. this.route.params.subscribe(value => { let lang = value['lang'); });
This not working: this.route.params.subscribe(value => { let lang = value['lang'); });. In url is /en/sometimes and params are empty. this.route is type of ActivatedRoute?
Can you view my updated comment? Yes, route is of type ActivatedRoute
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.