Magento, translate validation error messages

2020-08-22 03:39发布

问题:

I have successfully created new rules for the prototype validation, now I need to translate the error messages (Location: String in Javascript). However, I can only translate all the messages, my new custom ones don't appear to be translatable. How do I change this?

回答1:

Maybe you need an jstranslator.xml file inside etc folder:

<?xml version="1.0" encoding="UTF-8"?>
<jstranslator>
  <some-message-name translate="message" module="mymodule">
    <message>This is the text in my own js validation</message>
  </some-message-name>
</jstranslator>

With the following structure and meanings:

  • <jstranslator> - [one] XML root node.
    • <some-message-name> - [zero or more] root node child elements with a unique XML element name across all jstranslator.xml files (otherwise last in loading order based on module listing wins).
      • Attributes:
      • translate="message" - (optional) a hint that the child element(s) that is being translated is named "message", however this is hardencoded for the js translation XML files (Magento CE 1.9, search for "*/message") and this attribute does not need to be used.
      • module="mymodule" - (optional) name of the module, if left out the value is "core". It will be used to instantiate the data-helper later on (from that module) which then is reponsible to load the translations (e.g. from the CSV files).
    • <message> - [zero or one per parent] message to translate. the text value of this elements node-value is taken to be added to the javascript Translator object data.

All jstranslator.xml files of activated modules are processed.

Then put your translation line into the Something_Mymodule.csv file:

"This is the text in my own js validation", "(translated in a different language or speech)"

Then in your js scripts you can use your own translations via the Translator:

Translator.translate('This is the text in my own js validation');

Further References

  • Correct usage of jstranslator.xml


回答2:

To translate custom javascript error messages you need also to add them to the following file:

\app\code\core\Mage\Core\Helper\Js.php

find a function _getTranslateData() and you'll see a bunch of messages already in there.

just add your message somewhere in the array like this:

'This is my validation message' => $this->__('This is my validation message')

Don't forget a comma (,).

And then put translation in some translate file.

In the file where you use this message (I use it in opcheckout.js file) you need to wrap text in Translator.translate('This is my validation message').

I haven't figured out yet if it's important which translate file that is. You can try Mage_Core.csv .

I needed it in Mage_Checkout.csv and it works in there.


Anyway, for those who are interested in more, I noticed that these javascript messages are printed in header of every html page and some worrie that it messes with the SEO. Anyway this is printed in file \app\design\frontend\bmled\default\template\page\html\head.phtml with the code.

<?php echo $this->helper('core/js')->getTranslatorScript() ?>

Check for more here:


I hope this helps, I just hope this works everywhere, so far I tested it on Onepage Checkout only.



回答3:

You can edit app/local/ur_language/Mage_Core.csv file, adding original string in the first Column the translated one in the second. All the javascript translations are stored in this file.



回答4:

What's helped me (Magento EE 1.6) - I added new translation object:

<script>
var _data = {
    'This is a required field.':
        "<?php echo $this->__('This is a required field.'); ?>",
    'Please make sure your passwords match.':
        "<?php echo $this->__('Please make sure your passwords match');?>",
    'validate telephone error':
        "<?php echo $this->__('validate telephone error');?>"
};
var Translator = new Translate(_data);
</script>

if it is defined VarienForm uses it in js validation



回答5:

We had exactly the same problem with one of our magento projects. We found that the function in app/design/frontend/default/default/template/page/htmlhead.phtml had been commented out:

<?php echo $this->helper('core/js')->getTranslatorScript() ?>

After putting this in, it still did not work, because translations had not been inserted into translate file. Check out those two things and it should start working.



回答6:

To expand on this, you must add the translation strings to Mage/Core/Helper/Js.php.