LinuxCommandLibrary

xgettext

Extract translatable strings from source code

TLDR

Extract strings from C

$ xgettext -o [messages.pot] [*.c]
copy
Extract from Python
$ xgettext -L Python -o [messages.pot] [*.py]
copy
Extract with keyword
$ xgettext -k_ -o [messages.pot] [*.c]
copy
Join with existing
$ xgettext -j -o [messages.pot] [*.c]
copy
Add comments
$ xgettext --add-comments=TRANSLATORS -o [messages.pot] [*.c]
copy
Set package info
$ xgettext --package-name=[MyApp] --package-version=[1.0] -o [messages.pot] [*.c]
copy

SYNOPSIS

xgettext [-L language] [-k keyword] [-o output] [options] files

DESCRIPTION

xgettext extracts translatable strings from source code files and produces Portable Object Template (.pot) files used in the GNU gettext internationalization workflow. It scans source files for function calls that mark strings for translation, such as _(), gettext(), and N_(), and collects them into a template that translators can use as a starting point.
The tool supports a wide range of programming languages including C, C++, Python, Shell, PHP, Java, Perl, and many others. It can auto-detect the source language from file extensions or accept an explicit language specification via the -L option. Custom extraction keywords can be defined with -k to match project-specific translation functions.
The output .pot file preserves source file locations, translator comments (extracted with --add-comments), and message context. This template serves as the basis for creating language-specific .po files with msginit and keeping them updated with msgmerge as the source code evolves.

PARAMETERS

-o, --output FILE

Output file.
-L, --language LANG
Source language.
-k KEYWORD
Extraction keyword.
-j, --join-existing
Append to existing.
-c, --add-comments TAG
Extract comments.
--package-name NAME
Package name.
--from-code ENC
Source encoding.

CAVEATS

Part of gettext. Keywords must match code. Complex strings need care.

HISTORY

xgettext is part of GNU gettext, the standard internationalization system. It enables software translation workflow.

SEE ALSO

msgfmt(1), msginit(1), msgmerge(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community