7
votes

[EDIT]
This is outdated. The translation is not done anymore as described below
[/EDIT]
In Magento 1 the translatable texts needed in javascript were added in the jstranslate.xml file in the etc folder of a module and they were automatically added to the translator js object.
How can I add translatable texts to be used in javascript in Magento 2?
I found how to use text translations in Magento 2 but I don't know how to add new ones.

Here is an example on how to use them:

alert($.mage.__('Text to translate here'));

locked by Marius Jan 12 '16 at 15:02

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. See the help center for guidance on writing a good question.

Read more about locked posts here.

  • I have the same problem. I found out that since the beta release the translation script is changed. It now refers to a file called 'js-translation.json'. This file is found in the static directory of my theme. Did you already figured out how to add data to this file? Where do the translation phrases come from in this file? – Silvan Oct 21 '15 at 8:21
4
votes

OK. I found a solution but it seams ugly to me.
In the layout handle of the page I need I add this:

<referenceContainer name="after.body.start">
    <block class="Magento\Framework\View\Element\Template" name="some-name-here" template="[Namespace]_[Module]::translate.phtml"/>
</referenceContainer>

then create the template translate.phtml in [Namespace]/[Module]/view/[frontend|adminhtml]/templates with this content:

<?php
$_data = array(
    'Text one to translate' => __('Text one to translate'),
    'Text two to translate' => __('Text two to translate'),
);
?>
<script type="text/javascript">
    require(["jquery","mage/translate"], function($){
        $.mage.translate.add(<?php echo Zend_Json::encode($_data) ?>)
    });
</script>

I really hope there is an other way of doing this.

1
vote

The default src for the translations in frontend seems to be /app/code/Magento/Translation/view/base/templates/translate.phtml

pointing to this block /app/code/Magento/Translation/Block/Js.php

which has this as dataprovider /app/code/Magento/Translation/Model/Js/DataProvider.php

So you could change the dependency to an own dataprovider, or intercept this one to add own translations, or add an own block of this type with own dataprovider.

  • That's the exact file that lead me to what I posted in my own answer. The problem is that in translate.phtml there is nothing related to getTranslatedJson. It's just an 'orphan' phtml file. It could be easily related to a \Magento\Framework\View\Element\Template block class. The effect would be the same. – Marius Oct 6 '14 at 14:41
  • I'm really hoping this is unfinished business and it will be fixed. Thanks. – Marius Oct 6 '14 at 14:43
  • One month later and 2 days for us into Magento 2 and there is still the same problem and the whole project looks like "unfinished business". Documentation gaps, sloppy input filtering in a lot of classes and things like this which look like obvious bugs. One gets to wonder what in all the years till stable was done... Anyways, thanks for the tip with $.mage.translate.add. We wrote an ugly workaround which feeds our translation CSV of a whole language package to this function. – Jey DWork Dec 24 '15 at 5:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.