Magento – Magento 2 JS Translation Not Working

js-translationmagento2.2.2

Is this a magento bug or is something I am doing it working

I had created my translation file in my module i18n. For testing purpose I just modified few text.

app/code/Vendor/Namespace/i18n/en_US.csv

"Do you want to remove the item from cart?","Just Remove?"
"Login","Sign In"

In My JS File I have this piece of code which I need to translate

        deleteItem: function (data) {
            var self = this;
            confirm({
                content: $t('Do you want to remove the item from cart?'),
                actions: {
                    confirm: function () {
                        alert('Done');
                    },
                    always: function (event) {
                        event.stopImmediatePropagation();
                    }
                }
            });

        },

But the translation for the code does not work. When I deploy static content it does not adds the translation in js-translation.json file.

Does any one have a workaround?

Best Answer

It may be due to Magento bug. In vendor/magento/module-translation/Model/Json/PreProcessor.php

$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);

needs to be:

$area = $this->areaList->getArea($areaCode);
$area->load(\Magento\Framework\App\Area::PART_DESIGN);
$area->load(\Magento\Framework\App\Area::PART_TRANSLATE);

https://github.com/magento/magento2/issues/8508#issuecomment-279332346

Related Topic