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 等差数列中的素数个数
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证