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

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

报告相同问题?

悬赏问题

  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接
  • ¥15 MATLAB中使用parfor,矩阵Removal的有效索引在parfor循环中受限制
  • ¥20 Win 10 LTSC 1809版本如何无损提升到20H1版本
  • ¥50 win10 LTSC 虚拟键盘不弹出
  • ¥30 微信小程序请求失败,网页能正常带锁访问
  • ¥15 Matlab求解微分方程,如何用fish2d进行预优?