Home > Back-end >  How to pass variables in TS files in NGX Translate
How to pass variables in TS files in NGX Translate

Time:04-06

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

CodePudding user response:

You want to pass params to translation files like so:

Where item.threshold = 123

<div >{{ 'canUseTreshold' | translate: { restrictionAmount: item.threshold } }}</div>

JSON i18 file

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

Result:

text text text 123 text text

CodePudding user response:

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 })
  • Related