🔥 Just Launched! Werra Premium Template for HyväSee it in Action

How To Translate JS Error Message Or Text In Magento 2

By Sanjay JethvaUpdated on May 22, 2025 1 min read

Magento 2 is a widely used CMS to develop E-commerce stores serving the global customer base.

As Magento 2 stores serve customers using a different language from different parts of the world, businesses serving around the globe develop different store views.

For example, a Magento 2 store has one English view and another is in French. For such cases, the text messages that are displayed from the JS files need to be translated in the respective language. Any error or success message or any text messages are to be displayed in the visitors’ native language for proper understanding, and better user experience.

One can use the below code to translate JS error message or text in Magento 2:

Method To Translate JS Error Message Or Text In Magento 2:

<?php
require(['jquery', // jquery Library
'mage/translate' // Magento text translate], function ($){
    $(window).load(function ()
    {
        alert($.mage . __('Enter Your message here'));
        // here $.mage.__() used to translate text
    });
});
ORrequire(['jquery', // jquery Library
    'mage/translate'], function ($,$t)
{
    $(window).load(function () 
    {
        alert($t('Enter Your message here'));
    });
});

That’s it to translate strings by Javascript in Magento 2.

Feel free to share the solution with fellow developers via social media profiles.

Thank you.

Sanjay Jethva Full Image
Article bySanjay Jethva

Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe. His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.