Q:

Get param in angular 4

I want to add a multilanguage feature to an angular app and I want to set the default language when the site loads. I have two languages, spanish and english and the default language would be spanish. To achieve that I have the following routing defined:
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' }
];

First of all is that I'm not sure whether that's the correct structure of the routing considering what I want to achieve which is having the same route set for both languages and perform the translation at runtime with a translation service that I developed. My main problem is that when I try to inspect the params of the route I always get undefined. This is my onInit:
this.activatedRoute.params.subscribe((params: Params) => {
  this.currentLanguage = params['lang'];
});

I have also tried:
this.activatedRoute.params.subscribe((params) => {
  this.currentLanguage = params.lang;
});

I think the problem is in the redirect but I don't know how to set the ":lang" parameter when I perform that redirection. Does that make sense?

A:

I think the best solution will be to create AppTranslateService which will provide the language set in LanguageComponent:
@Injectable()
export class AppTranslateService {
  public lang: string = 'es';
}

@Component({/*...*/})
export class LanguageComponent {
  constructor(appTranslateService: AppTranslateService, activatedRoute: ActivatedRoute) {
    activatedRoute.params.subscribe((params) => {
      appTranslateService.lang = params['lang'];
    });
  }
}

and just read appTranslateService.lang variable in other components.
Alternatively you can get the desired param directly in your components from Angular's Router injection, e.g. this way: router.url.split('/')[1] (or do it once in a service).

