gettext - Unix, Linux Command


gettext - translate message


gettext [OPTION] -s [MSGID]...


The gettext program translates a natural language message into the user’s language, by looking up the translation in a message catalog.

Display native language translation of a textual message.

-d, --domain=TEXTDOMAIN
  retrieve translated messages from TEXTDOMAIN
-e enable expansion of some escape sequences
-E (ignored for compatibility)
-h, --help display this help and exit
-n suppress trailing newline
-V, --version
  display version information and exit
  retrieve translated message corresponding to MSGID from TEXTDOMAIN
If the TEXTDOMAIN parameter is not given, the domain is determined from the environment variable TEXTDOMAIN. If the message catalog is not found in the regular directory, another location can be specified with the environment variable TEXTDOMAINDIR. When used with the -s option the program behaves like the ‘echo’ command. But it does not simply copy its arguments to stdout. Instead those messages found in the selected catalog are translated. Standard search directory: /usr/share/locale


gettext is an internationalization and localization system commonly used for writing multilingual programs. For gettext to work, the message catalogue is required.

Below are the steps in creating message catalogue:

1. Modify source code to use gettext calls

$ cat
gettext -s "This message to be translated"

2. run xgettext to generate .pot (Portable Object Template) file

$ xgettext -c /
This will create file which will look similar like below
msgid "This message to be translated"
msgstr ""

3. Translate to create .po (Portable object) file

$ msginit --locale=es --input=name.pot
The .po file has to be modified to ener the translated message.
msgid "This message to be translated"
msgstr "This is a translated message"

4. Create .mo (Machine Object) file

$ msginit --locale=es --input=es.pot

5. To see the translated message user has to change the LC_MESSAGE variable