Closed jhruehl closed 6 years ago
@jhruehl, thank you for your report. We've created internal ticket(s) MAGETWO-83448 to track progress on the issue.
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.
@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>
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:
`
`.
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"); });
@jhruehl This doesn't work either. The js is loading, but it's not being translated.
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.
Browser Cache wasn't the issue. I added the 'Item in Cart' translation manually to the js-translation.json
file for now.
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
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.
``` 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 ```
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!
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
@magento-engcom-team Is there any progress on MAGETWO-83448?
@rsulym Try @Userr017 solution again, but with this path:
app/design/frontend/theme/theme/Magento_Checkout/web/template/summary/cart-items.html
@magento-engcom-team Is there any progress on MAGETWO-83448?
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
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
.
Hi @jhruehl. Thank you for your report. The issue has been fixed in magento/magento2#13528 by @mattijv in 2.2-develop branch Related commit(s):
The fix will be available with the upcoming 2.2.4 release.
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.
@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.
Hi @jhruehl. Thank you for your report. The issue has been fixed in magento/magento2#16720 by @sanganinamrata in 2.1-develop branch Related commit(s):
The fix will be available with the upcoming 2.1.15 release.
Hi @jhruehl. Thank you for your report. The issue has been fixed in magento/magento2#16892 by @mage2pratik in 2.3-develop branch Related commit(s):
The fix will be available with the upcoming 2.3.0 release.
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
Steps to reproduce
Expected result
Actual result
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'.