Skip to main content

Magento js validation message translation




Magento includes the translator library to translate the js messages that appear on our site.
The translator js is already included at the head.
To translate our messages, we just need to add the text to transator.
For eg:
This translator code needs to be added on our phtml file.
 Translator.add('Select a color.', "<?php echo $this->__('Select a color.');?>");

Then we need to add the text to translate at locale file i.e translate.csv at the locale folder.

However, this will not work for prototype js validation messages that appear on checkout pages.
e.g. "This is a required field." etc.

To translate these validation messages, we need to we’ll override Magento’s validate method.
For that, we need to create an external js file and place it under js folder.

For eg: js/lib/translatejs.js

Validation.addAllThese([
    ['validate-no-html-tags', Translator.translate('HTML tags are not allowed'), function(v) {
return !/<(\/)?\w+/.test(v);
}],
['validate-select', Translator.translate('Please select an option.'), function(v) {
                return ((v != "none") && (v != null) && (v.length != 0));
            }],
    ['required-entry', Translator.translate('This is a required field.'), function(v) {
                return !Validation.get('IsEmpty').test(v);
            }],
    ['validate-number', 'Please enter a valid number in this field.', function(v) {
                return Validation.get('IsEmpty').test(v)
                    || (!isNaN(parseNumber(v)) && /^\s*-?\d*(\.\d*)?\s*$/.test(v));
            }]
]);


For translation to work, we need to add the translations at header.phtml file.

Translator.add('This is a required field.', $this->__('This is a required field.'));
Translator.add('Please select an option.', $this->__('Please select an option.'));

These text should be in our translate csv file.


Finally, the translatejs.js file should be included. I have included it at footer.phtml because the translator should be initialized first.

<script type="text/javascript" src="<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);?>lib/translatejs.js"></script>

We can also modify the messages using the above method.






Comments

Popular posts from this blog

Magento 2.3 Admin panel blank issue

After the Installation of Magento from the composer, we cannot access the admin panel. Also, the deploy command doesn't work. (On Windows 10 OS). Note: Magento 2.3 supports Linux OS. Windows and Mac OS is not supported. This is a Magento bug. Wrong paths to Windows are generated. The fixed fix is Magento 2.3.0 - 2.3.3 #/vendor/magento/framework/View/Element/Template/File/Validator.php:140 the string if (0 === strpos($realPath, $directory)) {     return true; } to replace $realDirectory = $this->fileDriver->getRealPath($directory); if (0 === strpos($realPath, $realDirectory)) {    return true; } Magento 2.2.7 /vendor/magento/framework/View/Element/Template/File/Validator.php:113 code protected function isPathInDirectories($path, $directories) {     if (!is_array($directories)) {         $directories = (array)$directories;     }     foreach ($directories as $directory) {   ...

Error on payment method when placing order "No Such Entity With Cart ID"

No Such Entity With Cart ID Error on payment method when placing order. If you receive the error message " No such entity. ", " No such entity with " or "No such entity with  customerId,OrderId " in Magento 2, the issue usually occurred when you try to load not existing object via Magento 2 Repository Class. To debug this issue, please open the file vendor/magento/framework/Exception/NoSuchEntityException.php and at the beginning of the  __construct  method temporary add debug backtrace code: foreach ( debug_backtrace () as $_stack ) { echo ( $_stack [ "file" ] ? $_stack [ "file" ] : '' ) . ':' . ( $_stack [ "line" ] ? $_stack [ "line" ] : '' ) . ' - ' . ( $_stack [ "function" ] ? $_stack [ "function" ] : '' ); } exit (); example: public function __construct ( Phrase $phrase = null , \...

Cyber Security

Cybersecurity is the practice of protecting systems, networks, and programs from digital attacks. These cyberattacks are usually aimed at accessing, changing, or destroying sensitive information; extorting money from users; or interrupting normal business processes. Implementing effective cybersecurity measures is particularly challenging today because there are more devices than people, and attackers are becoming more innovative. What is cybersecurity all about? A successful cybersecurity approach has multiple layers of protection spread across the computers, networks, programs, or data that one intends to keep safe. In an organization, the people, processes, and technology must all complement one another to create an effective defense from cyber attacks. A unified threat management system can automate integrations across select Cisco Security products and accelerate key security operations functions: detection, investigation, and remediation. People Users must understand ...