
gettext Command in Linux
gettext is a Linux command that is used to translate program messages into different languages. With the help of this command, you can retrieve translations from a message catalog and display the translated text. It is pretty useful for creating multilingual software. You can specify the text domain and context to get the correct translation. This command can handle escape sequences and suppress training newlines if needed.
Table of Contents
Here is a comprehensive guide to the options available with the gettext command −
Syntax of gettext Command
The basic syntax to use the gettext command on Linux is as follows −
gettext [OPTION] MESSAGE
Here,
- MESSAGE is the text you want to translate.
- [OPTION] are several options you can use with gettext to customize its behavior.
gettext Command Options
The gettext command in Linux comes with several options to customize its behavior. Here are some of the most commonly used ones −
Option | Description |
---|---|
-c | Adds comments from the translation files. |
-d | Specifies the domain to use for message lookup. |
-e | Enables interpretation of backslash escapes. |
-E | Expands escape sequences, similar to the echo command. |
-n | Prevents the output of a trailing newline. |
[TEXTDOMAIN] MSGID | Specifies the text domain and the message ID you want to translate. |
Examples of gettext Command in Linux
Let explore a few examples gettext command in Linux −
- Basic Translation
- Specify a Text Domain
- Translate with Context
- Enable Backlash Escapes
- Expand Escape Sequences
- Prevent Trailing Newline
- Add Comments from Translation Files
Basic Translation
The basic function of the Linux gettext command is to retrieve the translated message for a given text. You can do this by simply using the command followed by the text you want to retrieve −
gettext "Hello, Linux Users!"
This command will output the translated message for âHello, Linux Users!â based on the current locale.

Specify a Text Domain
To specify a different text domain for the translation, you can set the TEXTDOMAIN environment variable and then use the gettext command. Follow the example below −
TEXTDOMAIN=mydomain gettext "Hello, Linux Users!"
This command will output the translation based on the text domain mydomain.

Translate with Context
To provide additional context for the translation, which can be helpful for ambiguous strings, use the -c option. For example −
TEXTDOMAIN=mydomain gettext -c "greeting" "Hello, Linux Users!"
This command provides the translation for âHello, Linux Users!â with the context of âgreetingâ.

Enable Backslash Escapes
If your message contains escape sequences like newlines or tabs, you can enable their interpretation by using the -e option. For example −
gettext -e "Hello\nLinux Users!"
This command will output âHelloâ followed by a newline and then âLinux Users!â.

Expand Escape Sequences
Similar to using the echo command, you can expand escape sequences in the message with the -E option using the gettext command. For example −
gettext -E "Hello\nLinux Users!"
This command outputs âHelloâ and a newline, followed by âLinux Users!â.

Prevent Trailing Newline
If you want to avoid a newline at the end of the translated message, simply use the -n option. Hereâs an example −
gettext -n "Hello, Linux Users!"
This command outputs the translation without appending a newline.

Add Comments from Translation Files
To include comments from the translation files in the output, use the -c option. For example −
gettext -c "Hello, Linux Users!"
This command will output the translated message along with any comments associated with it in the translation files.

Conclusion
The gettext is a crucial tool in Linux for translating program messages. It helps you fetch translations from a catalog and show them in different languages. This is great for making software that can be used by people worldwide. This tutorial has detailed the syntax, explained various options, and provided practical examples of the command gettext.
Understanding the gettext command and its various options is key to managing translations and building software that caters to a global audience.