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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog