[英]ngx-translate : How to provide context for translations in html and ts files?
[英]How to pass variables in TS files in NGX Translate
我们知道,在HTML文件中,我们可以使用pipe这样传递变量,这样不同的语言就可以反转,我如何在ts文件中做到这一点。
<div class="condition">{{ 'canUseTreshold' | translate: { restrictionAmount: item.threshold } }}</div>
这是我的 ts 代码: ${this.translate.instant('full')}${item.thresholdStr}${this.translate.instant('deliveryFeeDiscount')}${idx.== discountList?length - 1: '、'; ''};
${this.translate.instant('full')}${item.thresholdStr}${this.translate.instant('deliveryFeeDiscount')}${idx.== discountList?length - 1: '、'; ''};
你想像这样将参数传递给翻译文件:
其中item.threshold = 123
<div class="condition">{{ 'canUseTreshold' | translate: { restrictionAmount: item.threshold } }}</div>
JSON i18文件
"canUseTreshold": "text text text {{restrictionAmount}} text text"
结果:
text text text 123 text text
不知道我理解的对不对,在ts中翻译要传变量吗?
如果查看即时方法工作原理的文档,您会注意到第二个参数是 object,您可以在其中传递变量。
instant(key: string|Array, interpolateParams?: Object): string|Object:获取键(或键数组)的即时翻译值。 /.\ 这个方法是同步的,默认的文件加载器是异步的。 您有责任了解您的翻译何时加载以及使用此方法是安全的。 如果您不确定,那么您应该改用 get 方法。
this.translate.instant('full', { restrictionAmount: item.threshold })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.