1

i have a text like within %1 days %1 is dynamic how to translate using data-bind:i18n in html template magento 2.4

<span data-bind="text:methoddata[key].delivery_days_text"></span> 

any one modify this

2 Answers 2

0

To translate a dynamic string like "within %1 days" using data-bind:i18n in Magento 2.4, you can follow these steps:

  1. Update the string in your HTML template to include a placeholder for the dynamic value using %1, like this:

  1. In your JavaScript file, define a variable for the dynamic value and replace the placeholder in the translated string using sprintf function. For example:

    var deliveryDays = 5; // Or get the value dynamically from your data source
    var deliveryDaysText = $.mage.__('within %1 days').replace('%1', deliveryDays);
    methoddata[key].delivery_days_text = deliveryDaysText;
    
  2. Finally, update the text binding in your HTML template to use the translated and formatted value, like this:

This should output a translated string with the dynamic value included, like "within 5 days". Make sure to replace "methoddata[key]" with the actual path to your data object in the JavaScript code.

4
  • in inline cant replace?
    – we testers
    Mar 13, 2023 at 6:03
  • methoddata[key].delivery_days_text this data having 5 (days)
    – we testers
    Mar 13, 2023 at 6:05
  • Try like this: <span data-bind="i18n: 'within %1 days', attr: { title: $t('within %1 days', {'days': methoddata[key].delivery_days_text}) }"></span>
    – Dotsquares
    Mar 13, 2023 at 6:26
  • not worked text translated but %1 is not changed
    – we testers
    Mar 13, 2023 at 11:40
0

I would recommend using

https://github.com/willwright/Mecodeninja_DynamicTranslation

This would include the translation that is marked as dynamic in the js-transation.json for runtime translating variables and is very viable with the minimum overhead of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.