Home > Back-end >  ngx-translate: Event to detect changing language (Angular)
ngx-translate: Event to detect changing language (Angular)

Time:01-09

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().

CodePudding user response:

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

  •  Tags:  
  • Related