Magento ::从javascript文件翻译文本

问题描述 投票:8回答:4

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

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

要么

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

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

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

php javascript jquery magento translation
4个回答
19
投票

您可以在模板文件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>

编辑:您可以从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>

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


4
投票

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

Translator.translate('Some phrase');

0
投票

这是使用.phtml文件翻译JavaScript字符串的正确方法

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

更新:修正错字。


-1
投票

在js文件中使用它:

Translator.translate('Some phrase');

但为了使它工作,你应该在phtml中定义这个翻译:

Translator.add('Some phrase', "<?php echo $this->__('Some phrase'); ?>");
© www.soinside.com 2019 - 2024. All rights reserved.