Marcus Harrison
Marcus Harrison

Reputation: 869

How do you create Magento translation strings including variable contents?

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.

Upvotes: 1

Views: 1402

Answers (1)

Bob
Bob

Reputation: 8714

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

Upvotes: 2

Related Questions