Agent Wranglers

Languages and regions Recommended

Get plurals right in every language

Choose plural forms from the language's plural rules instead of adding an "s".

How to do it

Pick the message for each count from the language's plural rules. Use Intl.PluralRules in JavaScript, Babel in Python, or ICU MessageFormat in PHP and Java. Some languages have up to six forms, such as Arabic, so give translators every form the language needs.

Example

const rules = new Intl.PluralRules('en');
const msg = { one: '# file', other: '# files' };
const label = n => msg[rules.select(n)].replace('#', n);
label(1); // "1 file"
label(3); // "3 files"
# Python with Babel
from babel import Locale
english = Locale("en")
messages = {"one": "{n} file", "other": "{n} files"}
label = lambda n: messages[english.plural_form(n)].format(n=n)
label(1)  # "1 file"
// PHP with ICU MessageFormat
$files = new MessageFormatter('en', '{n, plural, one {# file} other {# files}}');
echo $files->format(['n' => 3]); // "3 files"

How to check

A unit test runs the counts 0, 1, 2, 5 and 21 for each language. Each picks the right form.

For agents: if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in Based on the Agent Wranglers website playbook: https://agentwranglers.se/playbook/website/languages-and-regions/plural-rules.