LinuxCommandLibrary

gettext

Translate program messages into different languages

TLDR

Get the translation of a string as specified in the domain file (falls back to given msgid if no translation exists)

$ LANGUAGE=[locale] gettext [[-d|--domain]] [domain] "[msgid]"
copy

Display help
$ gettext [[-h|--help]]
copy

Display version
$ gettext [[-V|--version]]
copy

SYNOPSIS

gettext [OPTION]... [[TEXTDOMAIN:]MSGID] [CONTEXT:]MSGID...

PARAMETERS

-d, --domain=DOMAIN
    Use DOMAIN instead of default "messages"

-e, --exact
    Require exact MSGID match (disables context/plural handling)

-E, --no-wrap
    Omit trailing newline from output

--help
    Display help and exit

--no-translations
    Suppress translation output (for testing/debugging)

--version
    Output version information and exit

DESCRIPTION

The gettext command is a core utility in the GNU gettext framework for software internationalization (i18n). It dynamically retrieves translated versions of natural language messages from compiled message object (MO) files based on the current locale.

In internationalized programs, source code embeds calls to gettext (or its C library equivalent, like gettext(3)) with message IDs (MSGID). At runtime, gettext looks up the MSGID in the appropriate domain's catalog (typically under /usr/share/locale), falling back to the original MSGID if no translation exists.

It supports textual domains to organize translations per application or module, specified via TEXTDOMAIN environment variable or -d option. Plural forms and contexts enhance accuracy for complex strings.

Primarily used in shell scripts or command-line tools for on-the-fly translation, it enables multilingual support without recompiling code. Catalogs are generated from portable object (PO) files via msgfmt(1).

gettext respects locale settings (LC_MESSAGES, LANG) and searches standard paths, making it seamless for POSIX-compliant systems.

CAVEATS

Requires pre-installed MO catalogs in locale paths; falls back silently to MSGID on failure. Not for generating catalogs (use xgettext(1)). Sensitive to environment variables like TEXTDOMAIN, LC_ALL.

COMMON USAGE

eval_gettext "$Hello" or gettext "Hello, world!" retrieves translation.
TEXTDOMAIN=myapp gettext "Save file" specifies domain.

ENVIRONMENT

TEXTDOMAIN: sets default domain.
LC_MESSAGES/LANG: controls locale.
LOCALEDIR: overrides catalog search path.

HISTORY

Developed as part of GNU gettext package, first released in 1995 by Ulrich Drepper for glibc internationalization. Evolved into POSIX standard (XPG4, SUSv2+), now ubiquitous in Linux/Unix for i18n workflows.

SEE ALSO

xgettext(1), msgfmt(1), msgmerge(1), msgcat(1), gettext(3)

Copied to clipboard