简体   繁体   English

如何在角度 4 中转换 ts 变量?

[英]How to translate ts variables in angular 4?

How to translate angular variables in template or ts file itself如何在模板或 ts 文件本身中转换角度变量
HTML code - someComponent.html HTML 代码 - someComponent.html

<button (click)="changeLang('en')">En</button>  
<button (click)="changeLang('de')">de</button>  
<p>{{rate}}</p>

Ts File - someComponent.ts Ts 文件 - 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 json文件-en.json

{ "productRate": "Product Rate" }  

Json file-de.json json 文件-de.json

{ "productRate": "Produktpreis" }  

I know how to do it in template using pipe but unable to do it in ts.我知道如何使用管道在模板中执行此操作,但无法在 ts 中执行此操作。
I had referenced stack overflow but unable to get the result.我引用了堆栈溢出但无法获得结果。 Please help请帮忙

From Docs来自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 get(key: string|Array, interpolateParams?: Object): Observable: 获取一个键(或一个键数组)的翻译值,如果未找到该值,则获取该键

You have to inject TranslateService as a dependency and do it as,您必须将 TranslateService 作为依赖项注入,并将其作为,

constructor(private translate: TranslateService) {
    let foo:string = this.translate.get('productRate');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM