This is outdated. The translation is not done anymore as described below
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'));
  • 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
    Commented Oct 21, 2015 at 8:21

2 Answers 2


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"/>

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

$_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) ?>)

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


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
    Commented Oct 6, 2014 at 14:41
  • I'm really hoping this is unfinished business and it will be fixed. Thanks.
    – Marius
    Commented Oct 6, 2014 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
    Commented Dec 24, 2015 at 5:06

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