Home

ngettext

ngettext is a function used in software localization to select the correct plural form of a translated string based on a numeric quantity. It is part of the GNU gettext framework and is available in C, C++, Python, and many other languages through bindings. The function takes three arguments: a singular form, a plural form, and a number n. It looks up the translation of the singular form in the active translation catalog for the current locale, applies the locale’s pluralization rules to n, and returns the corresponding translated string. If a translation is unavailable, ngettext falls back to the appropriate form of the original English text.

The selection of the plural form depends on the locale’s plural-forms rules, defined in the locale data.

Usage typically involves combining ngettext with the program’s formatting. For example, a line might format the

The
runtime
computes
a
plural
index
from
n
and
selects
the
corresponding
form
from
the
translated
message.
Some
locales
have
only
two
forms,
while
others,
such
as
Russian
or
Polish,
may
have
three
or
more
forms.
This
mechanism
allows
accurate
grammatical
agreement
for
quantities
across
many
languages.
returned
string
with
the
number
n
to
produce
a
message
like
“There
is
1
apple”
or
“There
are
3
apples,”
depending
on
the
value
of
n.
In
practice,
translations
are
stored
in
.po
files
and
compiled
to
.mo
catalogs,
which
are
loaded
by
the
application
at
runtime.
Many
languages
also
provide
higher-level
wrappers
or
aliases
for
ease
of
use.