Translating plural and singular forms in CakePHP

I don’t know how good you know the *.po translation file syntax, but for me it was a text file with “msgid” and “msgstr”, where msgid is the string, which could be found in a code of the app, wrapped with __() function and msgstr is the string which is the translation.

Following this convention in all of my projects I had not problems till now. But on this one I’ve noticed that the translation is not quite accurate especially when translating time. I am speaking about usage of timeAgoInWords() function from the TimeHelper.

I noticed that when there is a translation file for the specific language singular and plural words are not translated properly, I had results like “3 hour and 34 minute” instead of “3 hours and 34 minutes”.

I thought that I’ve found a bug, and I submitted a ticket in the trac, but I was wrong, because syntax of the PO files is a little bit extended than my understanding. 🙂

99% of the cases the standard approach could fit just fine, but when it’s used __n() function which is described as:

Returns correct plural form of message identified by $singular and $plural for count $count.

the translation file needed a specific syntax. Here is the example of it:

msgid "hour"
msgid_plural "hours"
msgstr[0] "hour"
msgstr[1] "hours"

As you can see instead of msgid<->msgstr pair, there is also msgid_plural and array of msgstr which holding the translations of singular and plural values. Note that in some languages plural could have 2 or more forms. In these cases the syntax will be (Czech):

msgid "%1 second"
msgid_plural "%1 seconds"
msgstr[0] "%1 sekunda"
msgstr[1] "%1 sekundy"
msgstr[2] "%1 sekund"

It’s good to set the plural forms definition in the project settings which are in the beggining of the file. This is very handy when you use PoEdit or similar translation tools, because if is your setting is correct, these tools will provide for each plural form separate tab.

The syntax of plural form definition is like:

"Plural-Forms: nplurals=1; plural=0;\n"

It hold 2 parameters the first one holding the number of plural forms in the specific language and the second one is an expression how to determine the plurals. If you don’t know how to set this correct for a specific language, check this good document of all available languages. 🙂

More information about the PO syntax I founded in that article. I’ve noticed in that the article there are shortcuts, which are not useful for me so far, but you never knows, in the future they could be handy.

Regarding to that I am wondering is there a POT file for the core lib translations which could be a big helper to anyone who need to translate the whole framework.

8 thoughts on “Translating plural and singular forms in CakePHP

  1. Nasko

    I guess the i18n console script would take care of proper rendering of the POT file when it encounters ‘__n’ keywords in the source. What about the standard gnu utils like xgettext? Is there an easy way (through configuration or by using specific command options) to generate the plural forms arrays in the POT files when the ‘__n’ keyword is encountered?

    (I’m asking this, because for a non-cake project I’m using a similar to CakePHP’s algorithm for parsing a language file – thus avoiding PHP’s native gettext extension, but I’m using xgettext to update my catalogs as sources evolve…)

    Offtopic: Haven’t had the chance to say hello yet, but anyways – thanks for the quality content you’re producing here and for the good CakePHP tips! I’ve been enjoying them for a while 🙂

  2. Pingback: Signets remarquables du 21/11/2008 au 24/11/2008 | Cherry on the...

  3. Jeff@Natural Beauty Tips

    Now this is a great article.

    I have spent, well in actual fact i spent ages and ages doing a custom php script to do this function.

    I never knew cake could do this. So many developer friends say i should wise up to the benefits of MVC and frameworks.

    I think its time for me to make the switch for sure now.

    Also, came aaccross this site in google, and can says its excellent, will be bookmarking and doing some frequent visits.

  4. Pingback: Translating Time helper in CakePHP « webdevelopment & opensource

  5. Pingback: Translating Time helper in CakePHP « V-TEK

  6. Pingback: Script installation service

Leave a Reply

Your email address will not be published. Required fields are marked *