duanlu7223
2015-11-02 13:32
浏览 27
已采纳

如何创建包含可变内容的Magento翻译字符串?

In Magento, performing a translation in modules involves invoking a helper and calling its translation function, E.G.

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

However, I haven't found any resources online regarding translating strings that contain variables. From past experience, I know that this is usually handled by using a token inside the string, with token replacements defined using additional arguments.

For example, GNU gettext's manual recommends using format strings for translation:

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

While Yii Framework has a similar, but subtly different format:

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

From a quick grep of Magento's core files, it looks like Magento uses C-style format strings, for example in Mage_Adminhtml_Block_Api_User_Edit::getHeaderText the following can be found:

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

But I would like further confirmation or advice since online documentation is quite sparse.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtdfj08626 2015-11-03 00:08
    已采纳

    Magento uses following syntax for translation:

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

    As suggested by Markus Harrison, I am adding these documentation for format strings:

    Formatting link 1

    Formatting link 2 - wiki

    点赞 打赏 评论

相关推荐 更多相似问题