DEV Community

Cover image for gettext Plural-Forms for 209 languages, checked against CLDR 47, and a checker for yours
AI Predictions Dev
AI Predictions Dev

Posted on Originally published at lightsoutfactory.ai

gettext Plural-Forms for 209 languages, checked against CLDR 47, and a checker for yours

A gettext catalogue picks the form of a plural message with one line in its header:

"Plural-Forms: nplurals=3; plural=(n % 10 == 1 && n % 100 != 11) ? 0 : (n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 12 || n % 100 > 14)) ? 1 : 2;\n"
Enter fullscreen mode Exit fullscreen mode

ngettext() puts the count into that C expression as n and shows msgstr[0], msgstr[1] or msgstr[2], whichever it returns. When the expression is wrong, nothing fails loudly: some counts just get the wrong word.

Where the headers go wrong

  • An English header in another language. nplurals=2; plural=(n != 1); in a Russian catalogue puts 2, 5 and 21 in the same slot. Russian says 2 файла, 5 файлов, 21 файл: no single translation of that slot is right for all three.
  • A header generated from CLDR as it is. Babel's to_gettext() gives Russian, Ukrainian, Polish, Czech and Slovak four forms, because CLDR has a form for fractions such as 1.5. gettext counts are whole numbers, so the fourth msgstr is never shown. For French and Portuguese it also writes CLDR's e operand into the expression, which gettext does not know.
  • Round millions. Recent CLDR gives exact millions a form of their own in French, Spanish, Italian, Portuguese, Catalan and a few others (un million de fichiers). Most catalogues keep the usual header; either works if you know which one you have.
  • Older headers. Some long-standing headers, the Welsh and Latvian ones for instance, split the numbers differently from CLDR.

A table and a checker

We needed correct headers for our own translations, so we built them from CLDR 47 (Babel 2.18.0) for every language with plural rules of its own: 209 of them, European Portuguese included, because 0 takes a different form there. Each header was evaluated on 22,017 counts (every number to 20,000, every thousand to 2,000,000 and round numbers to a billion) and had to put each one in the slot of its CLDR form.

The page has the whole table with copy buttons, and a checker: pick the language, paste your header (the value, the .po line or the whole header block) and it shows which numbers each msgstr gets, whether a slot mixes CLDR forms or is never used, and which "one" slots also serve other numbers (Russian 21, French 0), so their translation must show the number. It parses the expression itself instead of calling eval, and nothing leaves the browser.

Plural-Forms for every language →

For whole catalogues, translation-checks (MIT, one Python file, also a pre-commit hook) runs the same rules over every plural entry of a .po file, together with its placeholder, number and tag checks.


First published as a free tool on LightsOut Factory: what we learn running our own automation, written down as we go.

Top comments (0)