繁体   English   中英

如何在 NGX Translate 中传递 TS 文件中的变量

[英]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,您可以在其中传递变量。

ngx翻译

instant(key: string|Array, interpolateParams?: Object): string|Object:获取键(或键数组)的即时翻译值。 /.\ 这个方法是同步的,默认的文件加载器是异步的。 您有责任了解您的翻译何时加载以及使用此方法是安全的。 如果您不确定,那么您应该改用 get 方法。

this.translate.instant('full', { restrictionAmount: item.threshold })

暂无
暂无

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

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