1

Translation added to i18n files, phrases in <?php echo __("XXX");?>can be translated, but the those in $.mage.__('XXX') cannot be translated.

Any work around to translate phrases in $.mage.__('XXX')?

7ochem
  • 7,532
  • 14
  • 51
  • 80
Ricky.C
  • 2,182
  • 9
  • 33
  • 54
  • You can check with http://magento.stackexchange.com/questions/99966/how-to-translate-js-error-message-or-text-in-magento-2 – Krishna ijjada Oct 17 '16 at 04:55
  • have you keep mage/translate in your js? – Rakesh Jesadiya Oct 17 '16 at 05:24
  • @fschmengler No duplication found in js-translation.json – Ricky.C Oct 18 '16 at 02:47
  • I had the same problem like "Cannot translate phases in $.mage.__('XXX')" when phrase in phtml worked perfect. So, the solution was ingeniously simple.

    $t(...) and $.mage analizes only files *.js

    Consequently it does not fall into the js-translation dictionary.

    So when rendering a section script in phtml files, you should directly insert the already translated phrases with wrappers __('Translated phrase') .

    – Galina Apr 06 '18 at 13:56

1 Answers1

2

Let me answer the question myself:

Adding the code below to phtml file:

<?php
$_data = array(
    'Are you sure you would like to remove this item from the shopping cart?' => __('Are you sure you would like to remove this item from the shopping cart?'),
);
?>
<script type="text/javascript">
    require(["jquery", "mage/translate"], 
    function ($) {
        $.mage.translate.add(<?php echo Zend_Json::encode($_data) ?>)
    });
</script>
Ricky.C
  • 2,182
  • 9
  • 33
  • 54