Magento 使用一个系统来翻译模板文件中的文本,使用:
$this->__('要翻译的文本。');
或
Mage::helper('modulename')->__('text to be translated.');
.
这很好用。 但是当我将文本添加到 javascript 文件时,我不能使用这两种方法。
有没有一种方法可以对 javascript 文件的翻译做类似的事情?
最佳答案
您可以在模板文件 yourfile.phtml 中执行此操作。 JavaScript js/mage/translate.js
文件必须包含在您的 HTML header 中(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(...)
。
关于javascript - Magento::翻译 javascript 文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7751094/