简体   繁体   中英

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.

<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: '、'; ''}; ${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

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

JSON i18 file

"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?

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.

ngx-translate

instant(key: string|Array, interpolateParams?: Object): string|Object: Gets the instant translated value of a key (or an array of keys). /.\ 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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