Magento::从javascript文件翻译文本


Magento :: Translate text from javascript files

Magento使用一个系统来翻译模板文件中的文本,该系统使用:

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

CCD_ 2。

这个效果很好。但是当我向javascript文件添加文本时,我不能使用这两种方法。

有没有一种方法可以对javascript文件的翻译做类似的事情?

您可以在模板文件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开始,您可以在etc/文件夹下的模块中添加一个jstranslator.xml文件,并设置如下字符串:

<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做的那样翻译字符串。这将像下面的var Translator = new Translate(...)一样向JavaScript代码添加翻译。

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

Translator.translate('Some phrase');

我刚刚做了一个最简单的方法:

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

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

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

js文件中使用这个:

Translator.translate('Some phrase');

但要使其发挥作用,您应该在phtml:中定义此翻译

Translator.add('Some phrase', "<?php echo $this->__('Some phrase'); ?>");