Search code examples
angulartypescriptangular-componentsngx-translateangular13

ngx-translate: Event to detect changing language (Angular)


I want to detect the switch of the "current language" using ngx-translate library.

Which js event I can use to achieve that?

To better understand what I mean, please check this simple example: https://stackblitz.com/edit/github-yvbmgu

I want to detect the change of the currentLang from the "diff" component, on ngOnInit().


Solution

  • You can subscribe to TranslateService's onLangChange observable to listen to language change events.

        ngOnInit() {
          console.log(this.translate.currentLang);
          this.translate.onLangChange.subscribe((event: LangChangeEvent) => {
            console.log(event.lang);
          });
        }
    

    TranslateService API