Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple plural forms in gettext()

See this example of _n(); function (http://codex.wordpress.org/Function_Reference/_n):

sprintf( _n('%d comment.', '%d comments.', $number, 'text-domain'), $number );

in English:

1 Comment
2 Comments

in languages such as Polish there is a different pattern and multiple plural forms:

1 Komentarz
2 Komentarze
3 Komentarze
4 Komentarze
5 Komentarzy
6 Komentarzy
...
21 Komentarzy
22 Komentarze
23 Komentarze
24 Komentarze
25 Komentarzy
...
31 Komentarzy
32 Komentarze
...
91 Komentarzy
92 Komentarze
...
111 Komentarzy
112 Komentarzy (!)
...
121 Komentarzy
122 Komentarze

I'm looking for some way to enable translators to set their own pattern if their language supports multiple plural forms. Can you think of any creative PHP approach to do this?

Some solution I can think of (but still translators will not be able to set any pattern):

if($number == 1){
    $message = __(‘1 Komentarz’ , ‘text-domain’);
}else if($number == 2){
    $message = __(‘2 Komentarze’ , ‘text-domain’);
}else if($number == 3){
    $message = __(‘3 Komentarze’ , ‘text-domain’);
}

EDIT: I found this in PO file for Polish: "Plural-Forms: nplurals=3; plural=n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n" but I still don't get how to prepare _n(); function to support that.

like image 537
Atadj Avatar asked Aug 25 '12 11:08

Atadj


1 Answers

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.

like image 75
akky Avatar answered Sep 18 '22 17:09

akky