0

Currently i have a knockout template for minicart like this:

<!-- ko if: getCartParam('summary_count') -->
    <div class="items-total">
        <!-- ko if: getCartParam('summary_count') == 1 -->
            <!-- ko i18n: 'There is' --><!-- /ko -->
        <!-- /ko -->
        <!-- ko if: getCartParam('summary_count') > 1 -->
            <!-- ko i18n: 'There are' --><!-- /ko -->
        <!-- /ko -->   
        <span class="count"><!-- ko text: getCartParam('summary_count') --><!-- /ko --></span>
        <!-- ko if: getCartParam('summary_count') == 1 -->
            <!-- ko i18n: 'item' --><!-- /ko -->
        <!-- /ko -->
        <!-- ko if: getCartParam('summary_count') > 1 -->
            <!-- ko i18n: 'items' --><!-- /ko -->
        <!-- /ko -->
    </div>        
<!-- /ko -->

The text will generate a text like this:

There is 1 item or There are 3 items the number is the variable i get from getCartParam('summary_count') , the problem arise when i need to translate it, for example into this:

Hanya ada 1 barang
Terdapat 2 barang
Terdapat 5 barang

Using php or phtml file i can easily translate like this:

__('There is %1 item in the cart',$item->getQty())
0

The wording is split up, so you need multiple translations excluding the summary count:

  • There is
  • There are
  • item
  • items

An alternative is to create the string in a JS file and then you can translate it with a variable:

$.mage.__('Hello %1').replace('%1', yourVariable);

Taken from the dev docs

2
  • can i use that code in knockout html template ? – Tiny Dancer Oct 13 '20 at 1:10
  • You can create the variable/property in Javascript and then use it in HTML templates with something like this data-bind="text: yourVariable" – Ben Crook Oct 13 '20 at 8:28
0

add your translation in your csv file like this,

  • "'There is %1 item in the cart',%2","'There is %1 item in the cart',%2"
  • after run the below commands
  • bin/magento s:up
  • bin/magento s:s:d -f
  • bin/magento c:f
1
  • How does that work when the translations do not include variables? For example <!-- ko i18n: 'There is' --><!-- /ko --> - The only text there is "There is", there is no variable. – Ben Crook Oct 13 '20 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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