Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

%s variable not working in Magento

Tags:

magento

I always use %s variable as a placeholder when there is 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: sometimes it works, sometimes it doesn't. Surely the $name does contain a valid string value (I've var_dump it many times). Do you know why?

like image 681
user2880076 Avatar asked Nov 16 '13 17:11

user2880076


2 Answers

As others have noted, you're 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, swapping places:

// 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 on the PHP function being used to power this can be found here: vsprintf

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

like image 76
Tyler V. Avatar answered Nov 11 '22 23:11

Tyler V.


%s varible work in translate magento. You can write in .csv translate file such as in example:

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

Or if you use "%s" for translate you must write double slashe twiсe for shield. Look example:

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

You write for this translate:

Mage::helper('module')->__('Hi "%s", there is an error', $name);
like image 44
Knase Avatar answered Nov 12 '22 00:11

Knase