2011-10-13 59 views
7

Magento的應用系統使用翻譯模板文件中的文本:的Magento ::從JavaScript文件翻譯文本

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

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

這工作得很好。 但是,當我將文本添加到JavaScript文件時,我無法使用這兩種方法。

有沒有一種方法可以對JavaScript文件進行類似的翻譯?

+0

這就是答案:http://stackoverflow.com/questions/1450294/magento- translate-validation-error-messages#answer-3839423 – wormhit

回答

17

你可以在模板文件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(...)

+0

我曾嘗試過,但沒有奏效。 我會再試一次,但要確保。 – Chris

+0

不起作用...不幸的是。 我爲我的本地文件夾中的一個自定義模塊創建了一個新的助手。並在我的.phtml文件的頂部添加了Js。但它只是沒有用。 – Chris

+0

譯者是一個Magento的JavaScript變量。也許它失蹤了。嘗試使用Javascript標記:new Translate({'您應該處理此確認消息!','<?php echo Mage :: helper('yourmodule') - > __('您應該處理此確認消息!' )?>'}) –

4

只需使用以下方法在你的腳本:

Translator.translate('Some phrase'); 
0

這是翻譯的JavaScript字符串withing一個.phtml文件的正確方式

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