Skip to content
Advertisement

How to specify ordered variables in gettext .po files?

I have a multi-lingual site that uses Zend_Translate PHP arrays to handle the translations. It works fine, but I’m trying to convert to using gettext since that offers extra features.

However, I haven’t yet discovered the gettext equivalent of a feature that I liked in PHP array translations: n$ position specifier (example #4 of PHP sprintf).

I found a good example of what I want (notice how the order of the variables is different in English and Chinese):

#: wp-admin/upload.php:96
#, php-format
msgid "File %1$s of type %2$s is not allowed."
msgstr "类型为%2$s的文件%1$s不允许被上传。"

But I haven’t gotten it working for me. My en/messages.po file has this:

#, php-format
msgid "Earn X cash"
msgstr "%1$sEarn 1-30%% cash back%2$s, get money-saving coupons, and find the best price on every purchase at %3$s2,500+ stores%4$s."

and the PHP is this (which had worked for the array style of translations, and I don’t think it is supposed to change):

<?php echo $this->translate('Earn X cash', '<span class="earnCashBack">', '</span>', '<a href="/stores" class="numStores">', '</a>'); ?>

The HTML comes out wrong, like this:

$sEarn 1-30% cash back$s, get money-saving coupons, and find the best price on every purchase at $s2,500+ stores$s.

How must I edit the .po file for this to work? Or should I not be using php-mo for compiling the .po into the .mo file?

Advertisement

Answer

My .po and .mo files worked (and my webpage looks good) after I tried commenting out this line of php-mo.php:

$x = str_replace('$', '\$', $x);

I’m actually not sure why that line was there at all, but removing it makes me nervous that I might be introducing bugs that I haven’t discovered yet. But at least it fixed the problems that I could see!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement