tflori / angular-translator

translation module for angular
https://tflori.github.io/angular-translator/
MIT License
21 stars 6 forks source link

Question about languageChanged #20

Closed BorntraegerMarc closed 7 years ago

BorntraegerMarc commented 7 years ago

When having the following code:

        this.translateService.languageChanged.subscribe(() => {
            this.translateService.waitForTranslation().then(() => {
                let textToDisplay = this.translateService.instant('bla') as string;
            });
        });

Is waitForTranslation necessary again? And why is languageChanged not called when the first language loads? Seems unnecessary to write something like:

        this.translateService.waitForTranslation().then(() => {
            let textToDisplay= this.translateService.instant('bla') as string;
        });
        this.translateService.languageChanged.subscribe(() => {
            this.translateService.waitForTranslation().then(() => {
                let textToDisplay= this.translateService.instant('bla') as string;
            });
        });

Thx and kind regards :)

tflori commented 7 years ago

you mean the observerable could only get the changed language when language already loaded? yes, that would make such things easier.

We can change this without a change to the API so maybe we can implement this before 2.0..

tflori commented 7 years ago

On the other side: as long as no one loads the language (or it is trigged automatically) the observer does not get the change...

BorntraegerMarc commented 7 years ago

you mean the observerable could only get the changed language when language already loaded? yes, that would make such things easier.

No didn't mean that. Here is how I think we would make it easier:

How about we implement just one obervable? Called languageChanged. Doesn't matter if the language was loaded the first time or another one was loaded afterwards. It always get's executed when the current language is availible

tflori commented 7 years ago

let's assume someone is only using translate component and pipe. the language is loaded and he changes the language. both subscribe language changed. but when he does not waitForTranslation the observer get not called. we can automatically load the current language on init and after language changes but it sounds that we might load a something that is not used.

BorntraegerMarc commented 7 years ago

@tflori Sorry, I did not quite understand that. My proposal was only to replace waitForTranslation and languageChanged with languageChanged. Because the user usually does not care why something gets changed but only that it gets changed.

But maybe I don't get your point :)

tflori commented 7 years ago

what you suggest is more like a observable languageLoaded. The thing is that no language get's loaded automatically.

In your case I suggest a slightly different code:

class Test {
  public translations: Object = {};

  constructor(private translateService: TranslateService) {
    translateService.languageChanged.subscribe(() => this.getTranslations());
    this.getTranslations();
  }

  private getTranslations() {
    this.translateService.translate(['A', 'B', 'C']).then((translations) => {
      this.translations = {
        this.translations.A = translations[0];
        this.translations.B = translations[1];
        this.translations.C = translations[2];
      }
    });
  }
} 
BorntraegerMarc commented 7 years ago

Well, the method still get's called two times but I guess you know the architecture better :) Let's close the issue and I follow your guidelines

tflori commented 7 years ago

I found another way to solve this issue with the current interface.

Currently you simply change the language:

translateService.lang = 'it';

If you want the languageChanged to fire after the language got loaded you can use this workaround:

const lang = 'it';
translateService.waitForTranslation(lang).then(() => translateService.lang = lang);
BorntraegerMarc commented 7 years ago

@tflori yep, this would probably work. I'll try it out ASAP. But nonetheless seems a bit like a hack for what I wanted to achieve :)