I'm using ngx-translate with no-problems in views, using pipes. What i need to do is to use it in a component, for example to show an error message, or define a datatable column default content.
I'm trying to do something like:
translate.instant("AREA.NEW");
or
translate.get("AREA.NEW").subscribe((res: string) => {
console.log(res);
});
I've tried calling it in ngOnInit() and ngAfterViewInit()
But in both cases i just get "AREA.NEW", not the translated word. I asume the json dictionary is loaded after my call, so i don't realize how make it work.
Import the TranslateService and use it wherever you want.
import { TranslateService } from '@ngx-translate/core';
export class YourComponent {
constructor(private translateService: TranslateService) {
console.log('translation', this.translateService.instant('my_i18n_json_defined_key'));
}
}
It's work.
constructor(private readonly translateService: TranslateService) { }
ngOnInit() {
this.translateService.get(['']).subscribe(translations => {
console.info(this.translateService.instant('key1'));
console.info(this.translateService.instant('key2'));
console.info(this.translateService.instant('key3'));
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With