How to do plurals in l10n and i18n
Tuesday, August 4th 2009, 3:09am
Topics: Tutorials, CakePHP
Tags: L10n, I18n, Plurals, Locale, Translation
Views: 701, Comments: 3
So this was a tricky one for me to figure out as it is not stated in the cookbook (that I could find). It took me having to edit the core CakePHP files and posting a Trac issue until the Cake team explained to me how its done.
First off you may ask, why would we need plurals in our locale files when we are defining everything our self? Well its quite simple, it is used within the Time helper (relativeTime(), etc) and can be used in conjunction with __n(). As with some developers, including myself, we would assume that plurals would be written as followed within our default.po file.
I was always under the assumption that Cake did some inflection and logic magic on the backend to determine the plural form, but that's not the case. You need to define the plural form manually using msgid_plural. The correct way to write plurals would be like so.
"Its a bit messier, cant understand why it wouldn't be msgstr_plural or something equivalent, but that is how you do it." (Has been answered below, its used for languages with multiple plural versions). For future reference, all CakePHP internal code that uses pluralization uses the strings in default.po, so you should place the locale strings there. If you place them in a separate locale file, you can then use __dn().
First off you may ask, why would we need plurals in our locale files when we are defining everything our self? Well its quite simple, it is used within the Time helper (relativeTime(), etc) and can be used in conjunction with __n(). As with some developers, including myself, we would assume that plurals would be written as followed within our default.po file.
msgid "minute" msgstr "Minute" msgid "minutes" msgstr "Minutes"
I was always under the assumption that Cake did some inflection and logic magic on the backend to determine the plural form, but that's not the case. You need to define the plural form manually using msgid_plural. The correct way to write plurals would be like so.
msgid "minute" msgid_plural "minutes" msgstr[0] "Minute" msgstr[1] "Minutes"
"Its a bit messier, cant understand why it wouldn't be msgstr_plural or something equivalent, but that is how you do it." (Has been answered below, its used for languages with multiple plural versions). For future reference, all CakePHP internal code that uses pluralization uses the strings in default.po, so you should place the locale strings there. If you place them in a separate locale file, you can then use __dn().
3 Comments
www.jmjjg.fr
Aug 4th 2009, 07:25
The short answer is: some languages have different plural forms depending on the number of instances, so that's gettext's way of clarifying.
The long answer is in gettext's manual [link] (see example about plural of "file" in polish).
Aug 4th 2009, 12:38
In some languages may be more than two plural forms. If so, msgstr[3], 4... etc are valid too.
www.milesj.me
Aug 4th 2009, 14:12