Magento :: Translate text from javascript files

后端 未结 4 2045
予麋鹿
予麋鹿 2021-02-08 18:16

Magento uses a system for translating text in the template files using:

$this->__(\'text to be translated.\');

or

Mage::helper(\

相关标签:
4条回答
  • 2021-02-08 18:49

    You can do it in a template file yourfile.phtml. The javascript script js/mage/translate.js must be included in your html header (Magento does it by default).

    <script type="text/javascript">
    Translator.add('You should take care of this confirmation message!','<?php echo Mage::helper('yourmodule')->__('You should take care of this confirmation message!')?>');
    </script>
    

    EDIT: You can since Magento 1.7 add a file jstranslator.xml into your module under the etc/ folder and set the following string like that:

    <jstranslator>
        <!-- validation.js -->
        <validate-no-html-tags translate="message" module="core">
            <message>HTML tags are not allowed</message>
        </validate-no-html-tags>
        <validate-select translate="message" module="core">
            <message>Please select an option.</message>
        </validate-select>
    </jstranslator>
    

    Then translate the string as you do it for PHP thanks to CSV file This, will add the translation to the javascript code like the following var Translator = new Translate(...)

    0 讨论(0)
  • 2021-02-08 18:50

    Just use the following method in your scripts:

    Translator.translate('Some phrase');
    
    0 讨论(0)
  • 2021-02-08 19:06

    Use this is in js file:

    Translator.translate('Some phrase');
    

    But to make it work you should define this translation in phtml:

    Translator.add('Some phrase', "<?php echo $this->__('Some phrase'); ?>");
    
    0 讨论(0)
  • 2021-02-08 19:10

    This is the correct way for translating JavaScript strings withing .phtml file

    Translator.add({"To be translated":"<?php echo $this->_('To be translated'); ?>"});
    

    Update: fixed typo.

    0 讨论(0)
提交回复
热议问题