0
votes

I didn't understand why I can't get the translation in component ( it works fine in template )

I'm using : Angular 4.4 and I try [email protected] && @8 but it behave like the translation doesn't exist

import { TranslateService } from "@ngx-translate/core";

constructor(private translation: TranslateService) {
this.translation.get("DossierMedicale")
    .subscribe(value => {
        //returns DossierMedicale but in template it's correct
        console.log("aaaa", value);
    });

    //returns DossierMedicale but in template it's correct
    console.log("t2", this.translation.instant("DossierMedicale"));
}
2

2 Answers

1
votes

Try this out and let me know if it didn't work:

import { TranslateService } from "@ngx-translate/core";

constructor(
   private translation: TranslateService,
) {}

ngOnint() {
   console.log(this.getTranslation('DossierMedicale'));
}

getTranslation(wordYouNeedToTranslate: string): string {
   return this.translate.instant(wordYouNeedToTranslate);
}

Just use the instant method, if this doesn't work then please do make a stackblitz and I'll figure it out.

P.S: Do restart your cli...

0
votes

Try this code:

this.translate.stream('DossierMedicale').subscribe(value => console.log("aaa" + value))

By using .stream you not make extra http calls if you have loader seted up to get remote .json with translations.