I have two views English and Arabic default js validation error message needs to translate in Arabic so how to do it? And also I have a few custom texts on how to do it.

I created an i18n for Arabic is working fine if I do echo in PHTML or PHP file, the same way how to brig it into js also in Magento 2

anyone help me out

7 Answers 7


You can translate js error message like this

'jquery', // jquery Library
'jquery/ui', // Jquery UI Library
'mage/translate' // Magento text translate (Validation message translte as per language)
], function($){ 
    $(window).load(function() {
        alert($.mage.__('Enter Your message here'));

Magento2 use mage/translate.js for translation

  • created ar_KW.csv in my module i18n folder in that translated "Enter Your message here" in my j i tried it not working but in phtml if i do echo my translated text i displaying Feb 2, 2016 at 6:05
  • Did you clear your cache and static-content deploy ? @PradeepKumar
    – Keyur Shah
    Feb 2, 2016 at 7:23
  • Just the answer I was looking for! +1
    – thdoan
    Aug 1, 2016 at 10:22
  • Be aware that this will not work if you use it directly in phtml files. Magento will only parse and translate from js and html files.
    – eInyzant
    Feb 9, 2018 at 3:00
  • 1
    This will work but is not the that clean of a code; you are requiring mage/translate but are not using it. . Nov 6, 2018 at 12:38

This worked for me:

    function (
    ) {

        $tr('<your text>'),

Additional note: if you need to include some variable to translation (in js), you can use this:

$.mage.__('Hello %1').replace('%1', yourVariable);

after digging deep i come to know the concept of localization in magento2

below are thing you need to follow , in my website we have two view en and ar_kw Arabic

  1. Create language package http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-i18n.html#config-cli-subcommands-xlate-example2

  2. to get all phrases you need to run command ex

    php D:\xampp\htdocs\magento2\bin\magento i18n:collect-phrases -o "D:\xampp\htdocs\magento2\app\code\Sugarcode\Test\i18n\ar_KW.csv" D:\xampp\htdocs\magento2\app\code\Sugarcode\Test

    it ill read my test module and create a csv Sugarcode\Test\i18n\ar_KW.csv

it will get all __('Some Text') and $.mage.__('Some Text') in js and prepare a csv

  1. now translate the newly generated file to Arabic which you can keep in name of ar_KW.csv locale code (it may be inside your module or it may be inside you language module )
  2. remove var folder and run bin\magento setup:static-content:deploy en_US ar_KW

  3. it will create all js along with js-translation.json file in each theme/ ex luma/ar_KW, js-translation.json will have all js side translated data in json format when ever you see the error msg it ill shown from this file only

  • Hello, i have done all steps as per your answer, the code i added for ex : alert($.mage.__('js test')); is correctly coming in the language file generating but when i change the translatiion the js message still comes the same. Am i missing something ? Feb 3, 2017 at 8:11

it throw error if you use __ so follow below code

    'jquery', // jquery Library
   ], function($,$t){ 
        $(window).load(function() {
            alert($t('Enter Your message here'));

I had the same problem with js translations so i created a new language pack within app/i18n/... folder and refresh the cache.

See translating problem in JS templates.


Add in a template .phtml your error message like :
<?php echo __('This is a required field.'); ?>
- active translate online
- translate


  • Could you add some more details for the answer as to how and why it works?
    – tjons
    Jan 4, 2017 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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