Looking to set up a site using one of Google's recommended practices for multi-regional and multilingual websites, particularly the model of utilizing subdirectories with gTLD's (ex. www.stackoverflow.com/en/questions/ask, www.stackoverflow.com/fr/questions/ask, etc).
Depending on the top-level subdirectory I'd like to set default language and fetch the appropriate JSON data. To futher explain, when a user navigates directly to www.mywebsite.com/en/home the default language will be set to en
and my content service will fetch the en.json
file.
In the example above my routes would be configured like so:
export const routes: RouterConfig = [
{ path: ':country', component: Home },
{ path: ':country/home', component: Home },
{ path: ':country/about', component: About },
{ path: ':country/contact', component: Contact }
];
I'm trying to figure out how to grab the :country
route parameter (if URL is www.mywebsite.com/en/about/ I'm trying to get the :country
route parameter value of en
) when in the Home
, About
, or Contact
component. Here's a couple of things I've tried:
export class Home implements OnInit {
constructor(
private route: ActivatedRoute
) {}
private sub: any;
ngOnInit(){
this.sub = this.route.params.subscribe(params => {
let country = params['country'];
console.log(country) // undefined :(
});
}
}
I've also tried using route.snapshot
like so:
export class Home implements OnInit {
constructor(
private route: ActivatedRoute
) {}
ngOnInit(){
let country = this.route.snapshot.params['country'];
console.log(country) // undefined :(
}
}
Also have tried working with this solution to no avail: Angular 2 access parent routeparams from child component
If anyone has an idea of another approach please share!