Kornel Lesinski pisze:
On 8 Jan 2010, at 17:21, Marcin Głowacki wrote:

Thanks for your advice @phptal plural forms. It is working but i stuck on other problem - how to add plural translation in a tal variable?

for example:
<div i18n:translate="structure session/message"/>

and session/message for $n = 5 looks like:
'Added <tal:block i18n:name="n" tal:replace="string:5"/> pictures into queue';

Two rules to keep in mind:

1. PHPTAL isn't a dynamic/interpreted language. It's statically compiled language (as far as the TAL part goes), and it doesn't parse any tags at run time. 2. Translation strings are not allowed to contain any PHPTAL code. They're just dumb data.

<div i18n:translate="session/message">
    <tal:block i18n:name="n" tal:replace="string:5"/>

msgid: "Added ${n} pictures to the queue"

BTW: don't use structure keyword unless neccessary.

I solved all my problems with plurals with:

       if (!preg_match('/\b\d+\b/', $key)) {
           $value = gettext($key);
       else {
           preg_match('/\b\d+\b/', $key, $matches);
           $key = preg_replace('/\b\d+\b/', '${n}', $key);
           $this->setVar('n', $matches[0]);
           $value = ngettext($key, $key, $matches[0]);

It treats every string with number as plural. I don't have to use i18n:name="n" now ;-).

solution of problem with ${n} as a variable looks now:
<div i18n:translate="session/message"/>

and session/message can be just "Added 1 photos" ;-) regex will extract it, replace with ${n} and will be translated to "Added 1 photo"

the PO file:
msgid "Added ${n} photos"
msgid_plural "Added ${n} photos"
msgstr[0] "Added 1 photo"
msgstr[1] "Added ${n} photos"

msgid have to be same as msgid_plural because php ngettext($singular, $plural, $n).

Some can say that regexes are slow, but i don't need extreme performance.

Best regards and thanks for helping.

PHPTAL mailing list

Reply via email to