Closed BorntraegerMarc closed 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..
On the other side: as long as no one loads the language (or it is trigged automatically) the observer does not get the change...
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
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.
@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 :)
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];
}
});
}
}
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
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);
@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 :)
When having the following code:
Is
waitForTranslation
necessary again? And why islanguageChanged
not called when the first language loads? Seems unnecessary to write something like:Thx and kind regards :)