magento2: Magento 2.2.0: Translations for 'Item in Cart' missing in mini cart.

Since updating one of our stores to Magento 2.2 the translations for ‘Item in Cart’ are not compiled into the var/view_processed/pub/static/frontend/{Magento-Theme}/{language code}/js-translation.json files, though the translations for ‘Items in Cart’ are correctly loaded.

Preconditions

  1. Magento 2.2.0
  2. PHP 7.0 and 7.1 (different local setups and different testservers)
  3. MySQL 5.5.5-10.1.23-MariaDB

Steps to reproduce

  1. Have a store view in another language than en_US.
  2. Set a translation for ‘Item in Cart’ for that language.
  3. Open your store with the store view according to language, where you expect the translation to be done.
  4. Add exactly one item to your cart.
  5. Open the mini cart.

Expected result

  1. ‘Item to Cart’ translated to store view given language.

Actual result

  1. ‘Item to Cart’ is not translated.

Details

So the problem or at least a symptom of the problem seems to be in the ‘vendor/magento/module-checkout/view/frontend/web/template/minicart/content.html’ template. Strangely not all ‘translate’-nodes are triggering the Magento 2 Content Compiler to actually set the requested translations into the theme and language given js-translations.json files. Though the lines 33 - <translate args="'Item in Cart'" if="getCartLineItemsCount() === 1"/> and 34 - <translate args="'Items in Cart'" if="getCartLineItemsCount() > 1"/> are almost completely identical, the translations for ‘Items in Cart’ are loaded, but not for ‘Item in Cart’.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 22 (9 by maintainers)

Commits related to this issue

Most upvoted comments

The reason why some strings are not translated is because they are by default only in html files in tags like

<translate args="'Item in Cart'" if="getCartLineItemsCount() === 1"/>

The Magento Translation module has four different patterns it uses to detect translatable phrases. Only the translations for strings matching these patterns are translated into the js-translation.json. You can see the patterns in Magento/Translation/etc/di.xml. Unfortunately none of those patterns match the translate args tags.

To fix this without needing to explicitly call for a translation on all the missing phrases, you can add the following lines into pretty much any module’s di.xml:

    <type name="Magento\Translation\Model\Js\Config">
        <arguments>
            <argument name="patterns" xsi:type="array">
                <item name="translate_args" xsi:type="string"><![CDATA[~translate args\=("|'|"')([^\'].*?)('"|'|")~]]></item>
            </argument>
        </arguments>
    </type>

This adds a pattern that matches the translate args tags to the translation config.

@magento-engcom-team Suggested fix for the issue is to add the above pattern (or something similar) to the relevant part of Magento/Translation/etc/di.xml.

I added file cart-items.html and this code. Now “Item in Cart” translating for different language works.

<div class="title" data-role="title">
    <strong role="heading"><span data-bind="text: getItemsQty()"></span>
        <!-- ko if: getItemsQty() == 1 -->
        <!-- ko i18n: 'Item in Cart' --><!-- /ko -->
        <!-- /ko -->
        <!-- ko if: getItemsQty() > 1 -->
        <!-- ko i18n: 'Items in Cart' --><!-- /ko -->
        <!-- /ko -->
    </strong>
</div>

app/design/frontend/theme/theme/Magento_Checkout/web/template/cart-items.html

hi, any of solutions provided above doesnt work for me (M2.2.2). does anybody have working solution? thanks in advance!

The problem is that there was not js translation before <translate> tag.

I solved it temporary by overriding template src/vendor/magento/module-checkout/view/frontend/web/template/minicart/content.html

and add there js translation:

<div class="items-total"> to 
<div class="items-total" data-translate-hidden="attr: { item_in_cart: $t('Item in Cart') }"

you can use any name for attribute here.

This solution works for me.