Search code examples
angularservicesubscribengx-translatedate-pipe

translateService.onLangChange.subscribe does not trigger when lang changes in OnInit


So I had translateService.onLangChange.subscribe in a component where it worked fine but a made a separate service were it would subscribe to langChange and set a localeDate for me. But when I change the lang it doesnt register it and does nothing.

ngOnInit(): void {
    this.getLocaleDateFormat();
    this.translateService.onLangChange.subscribe(() => this.getLocaleDateFormat());
  }
getLocaleDateFormat() {
    // To add another lang it has to be added to Languages bellow to match the locale_id of the lang and in main module it has to be imported and registered
    this.dateAdapter.setLocale(Languages[this.translateService.currentLang]);
    this.currLang = Languages[this.translateService.currentLang];
    console.log('locale-date-adapters currLang: ' + this.currLang);
    return this.currLang;
  }

also everytime the lang change I'd like my component to get that value from getLocaleDateFormat(), without having

this.translateService.onLangChange.subscribe(() => thisLocaleDateAdapterService.getLocaleDateFormat()); 

in my component. I tried subscribing to it but got an error that it isn't a function.

this is what I tried:

this.localeDateAdapterService.getLocaleDateFormat().subscribe(currLang => this.currLang = currLang);

I basically want it to be listening to getLocaleDateFormat and change the value when it changes.

UPDATE: I just realized that the problem is in the OnInit thats not trigering the lang change. LangChange is trigger inside the service in function but not in OnInit


Solution

  • Forgot to put the service that is calling the onLang in the main module Provider.