Php 如何创建包含可变内容的Magento翻译字符串?
在Magento中,在模块中执行翻译涉及调用助手并调用其翻译函数,例如Php 如何创建包含可变内容的Magento翻译字符串?,php,magento,internationalization,Php,Magento,Internationalization,在Magento中,在模块中执行翻译涉及调用助手并调用其翻译函数,例如 Mage::helper("core")->__("This is a string to translate"); 然而,我还没有在网上找到任何关于翻译包含变量的字符串的资源。根据过去的经验,我知道这通常是通过在字符串中使用标记来处理的,使用附加参数定义标记替换 例如,GNU gettext手册建议使用格式字符串进行翻译: sprintf (gettext ("Hello %s!"), username ());
Mage::helper("core")->__("This is a string to translate");
然而,我还没有在网上找到任何关于翻译包含变量的字符串的资源。根据过去的经验,我知道这通常是通过在字符串中使用标记来处理的,使用附加参数定义标记替换
例如,GNU gettext手册建议使用格式字符串进行翻译:
sprintf (gettext ("Hello %s!"), username ());
Mage::helper('catalog')->__("This is %s text %s", "First String", "Second String");
虽然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()));
但我希望得到进一步的确认或建议,因为在线文档非常稀少。Magento使用以下语法进行翻译:
sprintf (gettext ("Hello %s!"), username ());
Mage::helper('catalog')->__("This is %s text %s", "First String", "Second String");
根据Markus Harrison的建议,我正在为格式字符串添加以下文档:
为了完整起见,可能值得添加一些关于格式字符串的文档链接,例如,或。