How to translate angular variables in template or ts file itself
HTML code - someComponent.html
<button (click)="changeLang('en')">En</button>
<button (click)="changeLang('de')">de</button>
<p>{{rate}}</p>
Ts File - someComponent.ts
rate:string="productRate";
changeLang(lang){
this.translate.use(lang);
this.traslate.get(this.rate).subscribe(res=>{this.rate = res;});
}
Json file-en.json
{ "productRate": "Product Rate" }
Json file-de.json
{ "productRate": "Produktpreis" }
I know how to do it in template using pipe but unable to do it in ts.
I had referenced stack overflow but unable to get the result. Please help
From Docs
get(key: string|Array, interpolateParams?: Object): Observable: Gets the translated value of a key (or an array of keys) or the key if the value was not found
You have to inject TranslateService as a dependency and do it as,
constructor(private translate: TranslateService) {
let foo:string = this.translate.get('productRate');
}