如何翻译 angular 4 中的 ts 变量?
How to translate ts variables in angular 4?
如何翻译模板或 ts 文件本身的 angular 变量
HTML 代码 - someComponent.html
<button (click)="changeLang('en')">En</button>
<button (click)="changeLang('de')">de</button>
<p>{{rate}}</p>
Ts 文件 - someComponent.ts
rate:string="productRate";
changeLang(lang){
this.translate.use(lang);
this.traslate.get(this.rate).subscribe(res=>{this.rate = res;});
}
Json 文件-en.json
{ "productRate": "Product Rate" }
Json 文件-de.json
{ "productRate": "Produktpreis" }
我知道如何使用管道在模板中执行此操作,但无法在 ts 中执行此操作。
我引用了堆栈溢出但无法获得结果。请帮忙
来自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
您必须将 TranslateService 作为依赖项注入并按以下方式执行,
constructor(private translate: TranslateService) {
let foo:string = this.translate.get('productRate');
}
如何翻译模板或 ts 文件本身的 angular 变量
HTML 代码 - someComponent.html
<button (click)="changeLang('en')">En</button>
<button (click)="changeLang('de')">de</button>
<p>{{rate}}</p>
Ts 文件 - someComponent.ts
rate:string="productRate";
changeLang(lang){
this.translate.use(lang);
this.traslate.get(this.rate).subscribe(res=>{this.rate = res;});
}
Json 文件-en.json
{ "productRate": "Product Rate" }
Json 文件-de.json
{ "productRate": "Produktpreis" }
我知道如何使用管道在模板中执行此操作,但无法在 ts 中执行此操作。
我引用了堆栈溢出但无法获得结果。请帮忙
来自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
您必须将 TranslateService 作为依赖项注入并按以下方式执行,
constructor(private translate: TranslateService) {
let foo:string = this.translate.get('productRate');
}