Skip to main content

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:

KeywordDescription
_()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

TargetDescription
moCompiles all .po files to .mo (part of ALL)
po-updateUpdates every .po file from the .pot template
po-update-<lang>Updates a single language's .po file
pot-updateExtracts translatable strings into po/<name>.pot

These targets are only available for top-level projects.

Cache variables

VariableDescriptionDefault
I18N_SOURCE_GLOBSGlob patterns for translatable source filessrc/*.c
MSGFMTPath to the msgfmt executable
MSGMERGEPath to the msgmerge executable
XGETTEXTPath 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