Multiple plural forms in gettext() Multiple plural forms in gettext() wordpress wordpress

Multiple plural forms in gettext()


First, your locale file needs to have the definition of plural. As you added on the question, in Polish case you may see

"Plural-Forms: nplurals=3; plural=n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"

or similar definition in (domain_name)-pl.po file.

Then, you need to prepare the translation for "%d Comment"/"%d Comments" in the .po file. For example,

msgid "%d Comment"msgid_plural "%d Comments"msgstr[0] "%d Komentarz"msgstr[1] "%d Komentarze"msgstr[2] "%d Komentarzy"

Please compile .po file into .mo file and place to an appropriate folder. (e.g. languages/(domain_name)-pl.mo

In your Wordpress (plugin/theme I assume) code, you may call it like this,

for ($i=1;$i<15;$i++) {  printf(_n("%d Comment", "%d Comments", $i, "(domain_name)"), $i);echo "<br />";}printf(_n("%d Comment", "%d Comments", 112, "(domain_name)"), 112);echo "<br />";

then of course set the WordPress's locale to Polish, in wp-config.php,

define ('WPLANG', 'pl');

you should see the results with correct plural forms.


I think I found a way to do what I want. POEdit application allows you to create multiple plural forms using the pattern you can specify in settings.

Translator have to code pattern on his own (see pattern for Polish in my question). I'm still researching that subject but if anyone has similar question then it's a good place to start: http://www.poedit.net/trac/wiki/Doc/PluralForms