10

I have two view English and Arabic default js validation error message needs to translate in Arabic so how to do it? and also I have few custom text how to do it

I created a i18n for Arabic is working fine if I do echo in phtml or php file , same way how to brig it into js also in Magento 2

any one help me out

26

You can translate js error message like this

require([
'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 – Pradeep Kumar Feb 2 '16 at 6:05
  • Did you clear your cache and static-content deploy ? @PradeepKumar – Keyur Shah Feb 2 '16 at 7:23
  • Just the answer I was looking for! +1 – thdoan Aug 1 '16 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 '18 at 3:00
  • This will work but is not the that clean of a code; you are requiring mage/translate but are not using it. . – oliver de Cramer Nov 6 '18 at 12:38
8

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

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

This worked for me:

define(
    [
        'mage/translate'
    ],
    function (
        $tr
    ) {

        $tr('<your text>'),
    }
);
5

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 ? – Sunil Verma Feb 3 '17 at 8:11
2

it throw error if you use __ so follow below code

  require([
    'jquery', // jquery Library
    'mage/translate'
   ], function($,$t){ 
        $(window).load(function() {
            alert($t('Enter Your message here'));
        });
    });
0

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.

0

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

done

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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