开发者

Magento :: Translate text from javascript files

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

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

or

Mage::helper('modulename')->__('text to be translated.');.

开发者_Python百科This works quite well. But when I add text to a javascript file I can't use these two methods.

Is there a way I could do a similar thing with the translations for javascript files?


You can do it in a template file yourfile.phtml. The JavaScript js/mage/translate.js file 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>

Since Magento 1.7, you can 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 the CSV file. This will add the translation to the JavaScript code like the following var Translator = new Translate(...).


Just use the following method in your scripts:

Translator.translate('Some phrase');


I just made the simplest way:

let sometext = '<?php echo $this->__('text to be translated.'); ?>' + someVarData;


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

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜