Skip to content

Instantly share code, notes, and snippets.

@theodesp
Last active April 7, 2019 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theodesp/6f0350a671fe3032e49b812564843044 to your computer and use it in GitHub Desktop.
Save theodesp/6f0350a671fe3032e49b812564843044 to your computer and use it in GitHub Desktop.
import { ITranslationService, I18NEXT_SERVICE } from 'angular-i18next';
import { Component, ViewEncapsulation, Inject, OnInit } from '@angular/core';
@Component({
selector: 'header-language',
encapsulation: ViewEncapsulation.None,
templateUrl: './header.component.html',
})
export class HeaderComponent implements OnInit {
language = 'en';
languages: string[] = ['en', 'el'];
constructor(
@Inject(I18NEXT_SERVICE) private i18NextService: ITranslationService
) {}
ngOnInit() {
this.i18NextService.events.initialized.subscribe((e) => {
if (e) {
this.updateState(this.i18NextService.language);
}
});
}
changeLanguage(lang: string){
if (lang !== this.i18NextService.language) {
this.i18NextService.changeLanguage(lang).then(x => {
this.updateState(lang);
document.location.reload();
});
}
}
private updateState(lang: string) {
this.language = lang;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment