2015-11-02 53 views
1

在Magento中,在模塊中執行翻譯包括調用助手並調用其翻譯功能,例如E.G.如何創建包含變量內容的Magento翻譯字符串?

Mage::helper("core")->__("This is a string to translate"); 

不過,我還沒有發現關於翻譯包含變量字符串的所有資源聯機。根據以往的經驗,我知道這通常是通過在字符串中使用一個標記來處理的,使用額外的參數來定義標記替換。

例如,GNU的gettext手冊建議使用格式字符串翻譯:

sprintf (gettext ("Hello %s!"), username()); 

雖然Yii框架也有類似的,但微妙的不同格式:

Yii::t("default", "Hello {username}!", array("username"=>username())); 

從Magento的核心的一個快速的grep文件,它看起來像Magento使用C風格的格式字符串,例如在Mage_Adminhtml_Block_Api_User_Edit :: getHeaderText中可以找到以下內容:

return Mage::helper('adminhtml')->__("Edit User '%s'", $this->escapeHtml(Mage::registry('api_user')->getUsername())); 

但我想進一步確認或建議,因爲在線文檔相當稀少。

回答

1

Magento會用以下語法翻譯:

Mage::helper('catalog')->__("This is %s text %s", "First String", "Second String"); 

正如馬庫斯·哈里森的建議,我將這些文檔的格式字符串:

Formatting link 1

Formatting link 2 - wiki

+0

爲求完整性可能值得包括一些關於格式字符串的文檔的鏈接,例如[this tutoria l for format strings](http://www.cprogramming.com/tutorial/printf-format-strings.html),[printf的維基百科頁面](https://en.wikipedia.org/wiki/Printf_format_string#Format_placeholder_specification )或[printf函數的參考頁](http://www.cplusplus.com/reference/cstdio/printf/)。 –