How to specify ordered variables in gettext .po files?

醉酒当歌 提交于 2019-12-10 23:26:25

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/13979962/how-to-specify-ordered-variables-in-gettext-po-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!