Magento ::从javascript文件翻译文本

Chr*_*ris 9 javascript php translation magento magento-1.x

Magento使用系统翻译模板文件中的文本:

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

要么

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

这非常有效.但是当我向javascript文件添加文本时,我无法使用这两种方法.

有没有办法我可以用javascript文件的翻译做类似的事情?

Syl*_*ayé 20

您可以在模板文件yourfile.phtml中执行此操作.javascript脚本js/mage/translate.js必须包含在你的html头文件中(Magento默认使用它).

<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>
Run Code Online (Sandbox Code Playgroud)

编辑:您可以从Magento 1.7添加文件jstranslator.xml到您的模块etc /文件夹下,并设置如下字符串:

<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>
Run Code Online (Sandbox Code Playgroud)

感谢CSV文件,然后翻译字符串为PHP,这将把翻译添加到javascript代码,如下所示 var Translator = new Translate(...)


Rom*_*tko 5

只需在脚本中使用以下方法:

Translator.translate('Some phrase');
Run Code Online (Sandbox Code Playgroud)