Magento的應用系統使用翻譯模板文件中的文本:的Magento ::從JavaScript文件翻譯文本
$this->__('text to be translated.');
或
Mage::helper('modulename')->__('text to be translated.');
。
這工作得很好。 但是,當我將文本添加到JavaScript文件時,我無法使用這兩種方法。
有沒有一種方法可以對JavaScript文件進行類似的翻譯?
Magento的應用系統使用翻譯模板文件中的文本:的Magento ::從JavaScript文件翻譯文本
$this->__('text to be translated.');
或
Mage::helper('modulename')->__('text to be translated.');
。
這工作得很好。 但是,當我將文本添加到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添加文件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>
然後翻譯字符串因爲你這樣做的PHP感謝CSV文件 這樣,將翻譯添加到JavaScript代碼如下var Translator = new Translate(...)
只需使用以下方法在你的腳本:
Translator.translate('Some phrase');
這是翻譯的JavaScript字符串withing一個.phtml文件的正確方式
Translator-add({"To be translated":"<?php echo $this->_('To be translated'); ?>"});
這就是答案:http://stackoverflow.com/questions/1450294/magento- translate-validation-error-messages#answer-3839423 – wormhit