ngettext
Translate pluralized strings using gettext catalogs
SYNOPSIS
ngettext [OPTION]... DOMAIN MSGID MSGID_PLURAL COUNT
PARAMETERS
-d DOMAIN, --domain=DOMAIN
Specify the domain of the messages. If not specified, it defaults to the textdomain configured in the environment.
-e, --no-expand
Do not expand escape sequences in the output. Output bytes directly without interpretation.
-c CATEGORY, --context=CATEGORY
Specify the message context. This is typically used when the same message ID can have different translations depending on its specific usage context.
-h, --help
Display a help message and exit.
-V, --version
Display version information and exit.
DESCRIPTION
ngettext is a command-line utility from the GNU gettext tools, designed to retrieve the correct plural form of a translated message from a message catalog (typically a .mo file). It's crucial for internationalization (i18n) and localization (l10n) efforts, allowing applications to display grammatically correct messages depending on a numeric quantity.
The command takes three core arguments: the domain (usually the name of the software package), the singular form of the message ID (MSGID), the plural form of the message ID (MSGID_PLURAL), and a count. Based on the provided count and the plural rules defined for the target language in the message catalog, ngettext outputs the appropriate translated string. This ensures that an application can correctly display "1 file" versus "2 files" or "0 files" in multiple languages, where plural rules can be complex and vary significantly. It's an essential component for making software globally accessible and user-friendly.
CAVEATS
Requires a properly installed and accessible message catalog (.mo file) for the specified domain and locale.
The plural rules are determined by the MSGID_PLURAL and COUNT argument, along with the 'Plural-Forms' header defined in the MO file. Incorrect 'Plural-Forms' can lead to wrong outputs.
The behavior is highly dependent on the LANG, LC_ALL, and LC_MESSAGES environment variables, which determine the effective locale for translation.
ENVIRONMENT VARIABLES
ngettext's behavior is heavily influenced by standard GNU gettext environment variables such as LANG, LC_ALL, and LC_MESSAGES, which determine the desired locale for translation. The TEXTDOMAINDIR variable specifies where the .mo files (message catalogs) are located on the system.
EXIT STATUS
ngettext returns 0 on successful operation. It returns 1 if an error occurred, such as missing arguments, inability to find the specified domain, or if the message could not be retrieved.
HISTORY
ngettext is an integral part of the GNU gettext package, which was developed by the GNU Project to provide a standardized framework for internationalizing software. Its development began in the early 1990s, aiming to simplify the process of translating software by separating translatable strings from the source code. The concept of plural forms was a later, crucial addition to handle linguistic complexities, leading to the inclusion of ngettext to specifically address these varying plural rules across languages. It has been a stable and widely used utility in the GNU/Linux ecosystem for decades.