简体   繁体   English

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

[英]How to pass variables in TS files in NGX Translate

as we know,in HTML file, we can use pipe to pass variable like this, so that different language can turn into inversion, how can I make it in ts file.我们知道,在HTML文件中,我们可以使用pipe这样传递变量,这样不同的语言就可以反转,我如何在ts文件中做到这一点。

<div class="condition">{{ 'canUseTreshold' | translate: { restrictionAmount: item.threshold } }}</div>

here is my ts code: ${this.translate.instant('full')}${item.thresholdStr}${this.translate.instant('deliveryFeeDiscount')}${idx.== discountList?length - 1: '、'; ''};这是我的 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: '、'; ''};

You want to pass params to translation files like so:你想像这样将参数传递给翻译文件:

Where item.threshold = 123其中item.threshold = 123

<div class="condition">{{ 'canUseTreshold' | translate: { restrictionAmount: item.threshold } }}</div>

JSON i18 file JSON i18文件

"canUseTreshold": "text text text {{restrictionAmount}} text text"

Result:结果:

text text text 123 text text

I don't know if I understood correctly, but do you want to pass variables for translation in ts?不知道我理解的对不对,在ts中翻译要传变量吗?

If you check the documentation of how the instant method works, you will notice that the second parameter is an object where you can pass your variables.如果查看即时方法工作原理的文档,您会注意到第二个参数是 object,您可以在其中传递变量。

ngx-translate ngx翻译

instant(key: string|Array, interpolateParams?: Object): string|Object: Gets the instant translated value of a key (or an array of keys). instant(key: string|Array, interpolateParams?: Object): string|Object:获取键(或键数组)的即时翻译值。 /.\ This method is synchronous and the default file loader is asynchronous. /.\ 这个方法是同步的,默认的文件加载器是异步的。 You are responsible for knowing when your translations have been loaded and it is safe to use this method.您有责任了解您的翻译何时加载以及使用此方法是安全的。 If you are not sure then you should use the get method instead.如果您不确定,那么您应该改用 get 方法。

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

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

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