ngettext Command in Linux



The ngettext command is part of the GNU gettext package and is used in Unix and Linux systems to translate messages and choose the correct plural form based on a number. It's particularly useful for internationalization and localization of software.

The ngettext program handles translating messages that need to account for plural forms. It works by translating a natural language message into the user's preferred language.

The program looks up the original message in a translation catalog (also known as a message catalog). This catalog contains translations for various messages in different languages. Plural forms can vary significantly between languages. For example, in English, you have "one apple" and "two apples." In other languages, the rules for pluralization might be different.

  • ngettext uses the COUNT value (the number that indicates quantity) to determine which grammatical form to use. It ensures that the translated message uses the correct plural form based on the given number and the rules of the target language.
  • ngettext displays the translated message in the user's native language, adjusting the grammatical form of the message according to the provided number. For example, it will ensure that singular and plural forms are used correctly based on the count

The TEXTDOMAIN Parameter

When using the ngettext command, it needs to know which message catalog (translation file) to use for translating the messages. This is determined by a parameter called TEXTDOMAIN. The TEXTDOMAIN parameter specifies the domain of the message catalog.

If you don't provide the TEXTDOMAIN parameter when running the ngettext command, the program will look for the domain name in an environment variable called TEXTDOMAIN.

If the message catalog is not located in the standard directory, you can specify a different location using the TEXTDOMAINDIR environment variable. By default, ngettext looks for message catalogs in the /usr/share/locale directory.

Table of Contents

Here is a comprehensive guide to the options available with the ngettext command −

Syntax of ngettext Command

The following is the general syntax for the ngettext command −

ngettext [OPTION] [TEXTDOMAIN] MSGID MSGID-PLURAL COUNT

Where −

  • OPTION − Optional flags that modify the behavior of the command.
  • TEXTDOMAIN − The name of the message catalog (optional). If not provided, it will default to the environment variable TEXTDOMAIN.
  • MSGID − The singular form of the message to be translated.
  • MSGID-PLURAL − The plural form of the message to be translated.
  • COUNT − The number that determines whether to use the singular or plural form.

ngettext Command Options

The following are different options you can effectively combine with the ngettext command to handle translations and plural forms in your software localization efforts.

Tag Description
-d, --domain=TEXTDOMAIN This option specifies the text domain from which to retrieve the translated message.
-e Enables the expansion of certain escape sequences within the message strings.
-E This option is ignored and included only for compatibility reasons. It does not perform any function.
-h, --help Displays help information for the ngettext command and exits.
-V, --version Displays version information for the ngettext command and exits.
[TEXTDOMAIN] Retrieves the translated message from the specified text domain if no -d option is given.
MSGID MSGID-PLURAL These are the singular (MSGID) and plural (MSGID-PLURAL) forms of the message to be translated.
COUNT Choose singular/plural form based on this value.

Examples of ngettext Command in Linux

In this section, we'll look at various practial examples that should help you understand how to use the ngettext command effectively for translating messages and handling plural forms.

Basic Translation with Singular and Plural Forms

To perform a basic translation with singular and plural forms, you can simply run the following command −

sudo ngettext "apple" "apples" 1

This command translates "apple" if the count is 1. If the count is not 1, it translates "apples".

ngettext Command in Linux1

Translation Using a Specified Text Domain

To retrieve a translated message from a specific text domain, you can use the following command −

sudo ngettext -d mydomain "apple" "apples" 3

This command uses the message catalog for the mydomain text domain to translate "apple" or "apples" based on the count 3.

ngettext Command in Linux2

Enabling Escape Sequence Expansion

To enable the expansion of escape sequences within the message strings, simply run the following command −

sudo ngettext -e "line1\nline2" "lines\nline2" 2

The "-e" option allows escape sequences such as \n (newline) to be interpreted within the message strings.

ngettext Command in Linux3

Specifying Context for Translation

To specify a context for the translation, you can use the following command −

sudo ngettext -c fruit "apple" "apples" 2

This command specifies a context (fruit) for the translation and uses the count 2 to choose the appropriate plural form.

ngettext Command in Linux4

Displaying Help Information

To display help information for the ngettext command, simply run −

sudo ngettext -h

This command shows a summary of all available options and their usage.

ngettext Command in Linux5

Displaying Version Information

To display version information for the ngettext command, you can use the following command −

sudo ngettext -V

This command provides information about the version of the ngettext command installed on your system.

ngettext Command in Linux6

Using Environment Variable for Text Domain

To retrieve a translated message using the TEXTDOMAIN environment variable, you can simply run the following commands −

export TEXTDOMAIN=mydomain
ngettext "apple" "apples" 1

By setting TEXTDOMAIN to mydomain, you instruct ngettext to use the message catalog associated with mydomain. This is necessary when you do not provide the "-d" option directly in the ngettext command.

ngettext looks up the message "apple" (singular) and "apples" (plural) in the mydomain message catalog. It then uses the COUNT (which is 1 in this case) to determine that the singular form "apple" should be used in the translation.

ngettext Command in Linux7

Choosing the Plural Form Based on Count

To choose the singular or plural form of the message based on the count, simply run −

sudo ngettext "apple" "apples" 4

The number 4 indicates that the plural form ("apples") should be used in this translation.

ngettext Command in Linux8

Conclusion

These practical examples demonstrate its ease of use and applicability in real-world scenarios, making ngettext an essential component for building multilingual software.

Using message catalogs and adhering to the grammatical rules of the target language, ngettext ensures that messages are displayed correctly for different numbers and contexts.

Advertisements