Internationalization
The i18n module provides build targets to extract translatable strings and compile message catalogs using the GNU gettext toolchain. It automates the standard gettext workflow: extracting marked strings from source code into a template, updating per-language translation files from that template, and compiling translations into binary catalogs.
Prerequisites
The module requires three gettext tools: xgettext, msgmerge, and msgfmt. If any of them is missing, the
module prints a status message and disables all i18n targets. On Alpine Linux, these are provided by the
gettext-dev package.
Source discovery
Source files to scan for translatable strings are found using the glob patterns in the I18N_SOURCE_GLOBS cache
variable. By default, only C files under src/ are scanned. The patterns are expanded recursively relative to
the project source directory. To scan additional directories, override the variable:
set(I18N_SOURCE_GLOBS "src/*.c;lib/*.c" CACHE STRING "" FORCE)
Extracted keywords
The module extracts the following gettext keywords from source files:
| Keyword | Description |
|---|---|
_() | Simple translation |
C_(:1c,2) | Translation with context |
N_() | Deferred translation |
NC_(:1c,2) | Deferred with context |
Directory structure
Translations are kept in a po/ directory at the project root:
po/
LINGUAS # List of language codes, one per line
myproject.pot # Template (generated by pot-update)
es.po # Spanish translation
de.po # German translation
The LINGUAS file lists each supported language code on a separate line. For every language listed, the module
creates a po-update-<lang> target to update that translation from the template, and registers a build command
to compile the .po file to a binary .mo catalog. The .mo files are built automatically as part of the
default ALL target, so a normal make build compiles all translations.
Build targets
| Target | Description |
|---|---|
mo | Compiles all .po files to .mo (part of ALL) |
po-update | Updates every .po file from the .pot template |
po-update-<lang> | Updates a single language's .po file |
pot-update | Extracts translatable strings into po/<name>.pot |
These targets are only available for top-level projects.
Cache variables
| Variable | Description | Default |
|---|---|---|
I18N_SOURCE_GLOBS | Glob patterns for translatable source files | src/*.c |
MSGFMT | Path to the msgfmt executable | |
MSGMERGE | Path to the msgmerge executable | |
XGETTEXT | Path to the xgettext executable |
Usage
# Extract strings from source code
make pot-update
# Update all language files from the template
make po-update
# Binary catalogs are compiled automatically during a normal build
make build