Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jhruehl opened this issue Nov 7, 2017 · 22 comments
Closed

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

jhruehl opened this issue Nov 7, 2017 · 22 comments
Labels
Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@jhruehl
Copy link
Contributor

jhruehl commented Nov 7, 2017

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'.

@jhruehl jhruehl changed the title Magento 2.2.0: Translations for 'Item in Cart' in mini cart missing. Magento 2.2.0: Translations for 'Item in Cart' missing in mini cart. Nov 7, 2017
@magento-engcom-team magento-engcom-team added Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed labels Nov 7, 2017
@magento-engcom-team
Copy link
Contributor

@jhruehl, thank you for your report.
We've created internal ticket(s) MAGETWO-83448 to track progress on the issue.

@magento-engcom-team magento-engcom-team added 2.2.x Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Nov 8, 2017
@avestique
Copy link

avestique commented Nov 23, 2017

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.

@emmathepossum
Copy link

@avestique Unfortunately your solution doesn't work for me.

The translation also doesn't work in the checkout. The code is the same:

magento/module-checkout/view/frontend/web/template/summary/cart-items.html

<strong role="heading">
    <translate args="maxCartItemsToDisplay" if="maxCartItemsToDisplay < getCartLineItemsCount()"/>
    <translate args="'of'" if="maxCartItemsToDisplay < getCartLineItemsCount()"/>
    <span data-bind="text: getCartLineItemsCount()"></span>
    <translate args="'Item in Cart'" if="getCartLineItemsCount() === 1"/>
    <translate args="'Items in Cart'" if="getCartLineItemsCount() > 1"/>
</strong>

@jhruehl
Copy link
Contributor Author

jhruehl commented Nov 30, 2017

Ok. A quite easy temporary solution, to get the missing js translations actually compiled and loaded, is to create an extension, which adds a basic js file to the head via view/frontend/layout/default.xml:
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <script src="Blub_LoadMissingJsTranslations::js/missing-translations.js"/> </head> </page>.

Then just add view/frontend/js/missing-translations.js:
require([ 'mage/translate' ], function ($t) { // put missing js translations here, e.g. // $t("some text to be translated"); });

@emmathepossum
Copy link

@jhruehl This doesn't work either. The js is loading, but it's not being translated.

@jhruehl
Copy link
Contributor Author

jhruehl commented Nov 30, 2017

Actually the js-file in the extension is just a mock. We overwrite it in our default theme via design/frontend/[theme]/default/[vendor_extension]/web/js/missing-translations.js:
require([ 'mage/translate' ], function ($t) { // put missing js translations here, e.g. // $t("some text to be translated"); $t('Item in Cart'); });

But it could also be that you have to delete your local browser storage. One time I lost hours, because phrases in one js file weren't translated, though everything was correct.

The problem at that time was my browser having cached an old js translation file and didn't reload it automatically.

Opening your page in a private browser window should suffice, if the browser cache/storage is now the problem.

@emmathepossum
Copy link

Browser Cache wasn't the issue. I added the 'Item in Cart' translation manually to the js-translation.json file for now.

@Triloworld
Copy link

Triloworld commented Dec 14, 2017

Found core problem of this issue on site.
Thx @jhruehl for tip but won't solve it.
Step to reproduce problem:
Be sure that you have translation in "mage-translation-storage" of "My Cart" in this example.

Add

app/design/frontend/[theme]/default/web/js/missing-translations.js

require([ 'ko','mage/translate' ], function ($t) {
    console.log($t("My Cart"));
});

This is in minicart text which don't translate correct.
After site load, check console. This text isn't translate.
Next, run in console the same code:

require([ 'ko','mage/translate' ], function ($t) {
    console.log($t("My Cart"));
});

Text is translate.

Even more. This content of script loading fine:

require([ 'mage/translate','domReady!' ], function ($t) {
    console.log($t("My Cart")); // Dom ready so this take little more time. This can be just everything to change order/slow down loading of site.
});

Bug is in order of script loading or some ajax request that don't return value correct.
Add breakpoint in: pub/static/.../mage/translate.js to see what happens

@Triloworld
Copy link

Triloworld commented Dec 14, 2017

I have one more think. It is connected to this one.

After digging in backend found this one file:

vendor/magento/framework/Translate.php

protected function _getFileData($file)
 {
     $data = [];
     if ($this->directory->isExist($this->directory->getRelativePath($file))) {
         $this->_csvParser->setDelimiter(',');
         $data = $this->_csvParser->getDataPairs($file);
     }
     return $data;
 }

Translate does not take into account different modules. Same string and other scope are just considered to be equal.

I provide example to show problem.

Click to expand test code showing problem - run in php online to check result

<?php

//TEST FOR STRING: "Show"

$data = [];
$keyIndex = 0;
$valueIndex = 1;

$csvData = array (
  0 => 
  array (
    0 => 'Show',
    1 => 'ABC:',
    2 => 'module',
    3 => 'Magento_Catalog',
  ),
  1 => 
  array (
    0 => 'Show',
    1 => 'CBA',
    2 => 'module',
    3 => 'Magento_Checkout',
  )
);

foreach ($csvData as $rowData) {
    
    
    if (isset($rowData[$keyIndex])) {
      
        $data[$rowData[$keyIndex]] = isset($rowData[$valueIndex]) ? $rowData[$valueIndex] : null;
    
    } else {
        echo "Else:
        ";
        var_dump(count($csvData));
        var_dump(count($data));
    } 
}

echo "Final:
";
var_dump(count($csvData)); //GET 2
var_dump(count($data)); //GET ONLY 1

@Userr017
Copy link

Userr017 commented Dec 21, 2017

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

@rsulym
Copy link

rsulym commented Dec 23, 2017

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

@Tonvdpol
Copy link

I think I have a workaround.

Create the directory:

app/design/frontend/your_vendor/your_theme/Magento_Checkout/templates

Copy cart.phtml to this directory from:

/vendor/magento/module-checkout/view/frontend/templates/cart.phtml

Add this code anywhere in cart.phtml:
$dummy = __('Item in Cart');

Now regenerate the static files.

Side note: any phtml will do. Apparently Magento 2 scans all phtml files for __('...') and adds these to js-translation.json

@torhoehn
Copy link
Contributor

torhoehn commented Jan 3, 2018

@magento-engcom-team Is there any progress on MAGETWO-83448?

@emmathepossum
Copy link

@rsulym Try @Userr017 solution again, but with this path:
app/design/frontend/theme/theme/Magento_Checkout/web/template/summary/cart-items.html

@Amadeco
Copy link

Amadeco commented Jan 11, 2018

@magento-engcom-team Is there any progress on MAGETWO-83448?

@maxxeh1
Copy link

maxxeh1 commented Feb 5, 2018

In magento 2.2 (you can probably do this in other versions as well) you can add data-bind="i18n: 'Item in Cart'" to
Line 32 in module-checkout/view/frontend/web/template/minicart/content.html
Of course over-ride this file. This then adds the translation to js-translation.json to also translate the Checkout

@mattijv
Copy link
Contributor

mattijv commented Feb 6, 2018

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.

mattijv added a commit to mattijv/magento2 that referenced this issue Feb 7, 2018
@magento-engcom-team
Copy link
Contributor

Hi @jhruehl. Thank you for your report.
The issue has been fixed in #13528 by @mattijv in 2.2-develop branch
Related commit(s):

The fix will be available with the upcoming 2.2.4 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.2.x The issue has been fixed in 2.2 release line label Feb 9, 2018
magento-engcom-team pushed a commit that referenced this issue Feb 9, 2018
…tions.json #13528

 - Merge Pull Request #13528 from mattijv/magento2:2.2-develop
 - Merged commits:
   1. 9c35c45
@ghost
Copy link

ghost commented Feb 15, 2018

Best,

Not fixed still the same issue.
Just like the mini cart issue, this is still in the magento 2.2.2 version.

Regards, and hoping for solutions.

@mattijv
Copy link
Contributor

mattijv commented Feb 15, 2018

@koopjesboom

See my comment. Copy the lines of code and add them to any suitable module's di.xml. Then empty the pub/static/frontend folder and run

php bin/magento setup:upgrade;
php bin/magento setup:di:compile;
php bin/magento setup:static-content:deploy {{LANGUAGES}};
php bin/magento cache:flush;

Also clean your browser cache. The minicart should be translated correctly. If not, try the above again but make sure you list the language you want to be translated to first in the third step.

@sidolov
Copy link
Contributor

sidolov commented Jul 12, 2018

Hi @jhruehl. Thank you for your report.
The issue has been fixed in #16720 by @sanganinamrata in 2.1-develop branch
Related commit(s):

The fix will be available with the upcoming 2.1.15 release.

@sidolov sidolov added the Fixed in 2.1.x The issue has been fixed in 2.1 release line label Jul 12, 2018
magento-engcom-team added a commit that referenced this issue Jul 12, 2018
…s-translations.json #16720

 - Merge Pull Request #16720 from sanganinamrata/magento2:2.1-develop-PR-port-13528
 - Merged commits:
   1. aa0e051
@sidolov
Copy link
Contributor

sidolov commented Jul 21, 2018

Hi @jhruehl. Thank you for your report.
The issue has been fixed in #16892 by @mage2pratik in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.0 release.

@sidolov sidolov added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Jul 21, 2018
magento-engcom-team added a commit that referenced this issue Jul 21, 2018
…e js-translations.json #16892

 - Merge Pull Request #16892 from mage2pratik/magento2:2.3-develop-PR-port-13528
 - Merged commits:
   1. 18d11f5
magento-engcom-team pushed a commit that referenced this issue Jul 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed in 2.1.x The issue has been fixed in 2.1 release line Fixed in 2.2.x The issue has been fixed in 2.2 release line Fixed in 2.3.x The issue has been fixed in 2.3 release line Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests