Variable% s does not work in Magento

I always use the% s variable as a placeholder if there is an error or success, such as this code:

Mage::getSingleton('customer/session')->addError(Mage::helper('module') ->__('Hi %s, there is an error', $name)); 

The problem is that sometimes it works, sometimes it doesn't. Of course, the name $ contains the correct string value (I have var_dump this many times). Do you know, why?

+8
magento
source share
5 answers

As others have noted, you are not limited to one argument in your translated string. Some examples:

One argument, in one place:

 Mage::helper('module')->__('Hi %s, there is an error', $name); 

Two arguments in two places:

 Mage::helper('module')->__('Hi %s, there is an error with code %s', $name, $code); 

One argument in two places:

 Mage::helper('module')->__('Hi %1$s, there is an error. Your name is %1$s', $name); 

Two arguments, places for exchange:

 // Template or block file Mage::helper('module')->__('Hi %1$s %2$s, there is an error', $firstname, $lastname); // CSV translation "Hi %1$s %2$s, there is an error", "%2$s, %1$s received an error" 

Documentation of the PHP function used to use it can be found here: vsprintf

(PS: The double quote that knase is talking about only applies to your CSV file, where the double quote is a single quote).

+12
source share

% s varible work in translate magento. You can write a .csv translate file, for example, in the example:

 "Hi %s, there is an error","An error is %s" 

Or, if you use "% s" for translation, you must write double salsa for the shield. Example example:

 "Hi ""%s"", there is an error.","An error is ""%s""." 

You write for this translation:

 Mage::helper('module')->__('Hi "%s", there is an error', $name); 
+3
source share

What version of Magento are you using? In the current version of Magento, variables are replaced here.

 #File: app/code/core/Mage/Core/Model/Translate.php ... //array_unshift($args, $translated); //$result = @call_user_func_array('sprintf', $args); $result = @vsprintf($translated, $args); ... 

I suggest that dropping some debugging statements may help to better understand the problem.

0
source share

Make sure the csv file uses double quotes instead of single quotes.

Wrong:

'Welcome %s', 'Hello %s'

Correctly:

"Welcome %s", "Hello %s"

0
source share

Wow, it looks like if there is more than one% s, the translation will not work. For the translation to work correctly, there must be only% s.

-3
source share

All Articles