Home » Php » php – PoEdit keywords – plurals

php – PoEdit keywords – plurals

Posted by: admin July 12, 2020 Leave a comment


I incorporated a gettext-like localization system in my app, but my translation function looks like this:

 t($category, $string, [$plural_string, $number, $vprintf_arguments])

My PoEdit keywords:

  • t:2 tells PoEdit to parse $string, and it works apparently
  • t:2,3 should tell PoEdit to parse both $string and $plural_string – but it’s not 🙁

It only sees $string, so I don’t get the plural forms parsed… How can I fix that? I don’t want to switch my function to a different argument format because I like this one 🙁

Also this function acts like a sprintf replacement:

  • if 3rd argument ($plural_string) is a array, then the function will consider the values from the array as arguments to vsprintf
  • if 3rd argument is a string and $number is provided, the function will consider $vprintf_arguments as arguments to vsprintf (if they are provided), and $plural_string as the plural form of $string

Anyway PoEdit should not interfere with non quoted arguments, right? I mean it will only parse $plural_string as string if it looks like 'abc abc'

How to&Answers:

gettext only deals with one string at a time.
What you may be after is ngettext. (look it up for syntax)

There, you’ll put in one text for singular, another for plural, but also the number.

The smart thing about this is, that many languages have a totally different plural structure, than English.
Russian, for instance, uses three different forms.
One is for where the count ends in a pronounced ‘one’:
1, 21, 31, 41 etc.
The second form is for counts, which end in a pronounced 2, 3 or 4.
The third form is for the rest…

And, with ngettext, this can be done.
Yeah, in poedit, one has to correctly define the target language’s plural structure, but then, it just works.



Like Tor-Bjorn Fjellner has answered, this actually should just work.

If you tell poedit that the language actually has plural forms (Translating Plural Forms with Poedit) and taken your keywords into account, poedit offers them for translation.


Plural Forms: nplurals=2; plural=n != 1;


  • t:2
  • t:2,3

PHP Code:


t("cat", "strA");

t("cat", "strB1", "strB2", 2, array());

t("cat", "strC1", "strC2", 3, array());

Poedit UI:

Poedit Plural Forms

What I didn’t understand in your question was the point about the arrays and such. I could not decipher what you’re intending to in the poedit/gettext domain, I think your question would benefit if you add some concrete code examples and how the outcome should be for them.