Angular make translation global

Viewed 971

I use TranslateService from @ngx-translate and every tutorial I found is pretty much the same.

The problem is.. I have in root AppComponent this:

import { TranslateService } from '@ngx-translate/core';

constructor(private translate: TranslateService) {
    translate.addLangs(["en", "fr"]);
    translate.setDefaultLang('en');

    let browserLang = translate.getBrowserLang();
    translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');
}

In header event which change language:

<select #langSelect (change)="translate.use(langSelect.value)">
    <option *ngFor="let lang of translate.getLangs()" [value]="lang" [selected]="lang === translate.currentLang">{{ lang }}</option>
</select>

and I want this to be global, the change event to propagate into my all modules, components etc. Is there a way to do that, or I must inject this service every place I use it?

Thanks.

1 Answers

Either you need to wrap this in a module and include in every page or you cab separate out this component in a different controller altogether and render it as a part of header so that it gets rendered from the layout.

Related