I am working on implementing a multilanguage feature in an Angular app and I need to establish the default language when the site loads. The two languages supported are Spanish and English, with Spanish being the default language. In order to achieve this, I have the following routing setup:
const routes: Routes = [
{
path: ':lang', component: LanguageComponent,
children: [
{ path: 'route1', component: Component1 },
{ path: 'route2', component: Component2 },
{ path: 'route3', component: Component3 },
{ path: 'route4', component: Component4 },
{ path: '', component: Component1 , pathMatch: 'full' },
],
},
{ path: '', redirectTo: 'es', pathMatch: 'full' }
];
I am unsure if this structure of routing is correct for my requirements, which include using the same route for both languages and translating content at runtime using a custom translation service. One main issue I'm facing is that whenever I try to inspect the route params, they show as undefined. Here is how I'm trying to get the current language in my ngOnInit
:
this.activatedRoute.params.subscribe((params: Params) => {
this.currentLanguage = params['lang'];
});
I have also attempted this approach:
this.activatedRoute.params.subscribe((params) => {
this.currentLanguage = params.lang;
});
I suspect that the problem lies in the redirection step, but I'm not certain how to set the ":lang" parameter during that redirect. Can you provide any insight or guidance on this issue?