GETTEXT(3)
π GETTEXT(3) β Library Functions Manual π
okay bestie, sit down, hydrate, and let me tell you about gettext, dgettext, and dcgettext β the trio that translates your message. this is a page from the Library Functions Manual and it's genuinely iconic. π
β¨ NAME β¨
gettext, dgettext, dcgettext β translate message. that's it, that's the whole job. πΈ
β¨ SYNOPSIS β¨
#include <libintl.h>
char * gettext (const char * msgid);
char * dgettext (const char * domainname, const char * msgid);
char * dcgettext (const char * domainname, const char * msgid,
int category);
look at them. three little functions. so much power. π
β¨ DESCRIPTION β¨
so the way that gettext, dgettext and dcgettext work is: they attempt to translate a text string into the user's native language, by looking up the translation in a message catalog. it's giving "your app speaks every language now". π
the msgid argument identifies the message to be translated. by convention, it's the English version of the message, with non-ASCII characters replaced by ASCII approximations. why tho? because that choice lets the translators work with message catalogs, called PO files, that contain both the English and the translated versions of each message, and those can be installed using the msgfmt utility. bestie teamwork fr fr. π«Ά
a message domain is a set of translatable msgid messages. usually every software package has its own message domain. the domain name is what determines the message catalog where the translation is looked up; it must be a non-empty string. for the gettext function, it's specified through a preceding textdomain call. for the dgettext and dcgettext functions, it's passed as the domainname argument; and if this argument is NULL, the domain name specified through a preceding textdomain call is used instead. sneaky, but valid. π
translation lookup operates in the context of the current locale. for the gettext and dgettext functions, the LC_MESSAGES locale facet is used. it's determined by a preceding call to the setlocale function. setlocale (LC_ALL, "") initializes the LC_MESSAGES locale based on the first nonempty value of the three environment variables LC_ALL, LC_MESSAGES, LANG; see setlocale(3). for the dcgettext function, the locale facet is determined by the category argument, which should be one of the LC_xxx constants defined in the <locale.h> header, excluding LC_ALL. in both cases, the functions also use the LC_CTYPE locale facet in order to convert the translated message from the translator's codeset to the current locale's codeset, unless overridden by a prior call to the bind_textdomain_codeset function. codeset conversion?? she does it all. π
β¨
the message catalog used by the functions is at the pathname dirname/locale/category/domainname.mo. okay let me break that down because it's literally a whole address:
dirnameis the directory specified throughbindtextdomain. its default is system and configuration dependent; typically it isprefix/share/locale, whereprefixis the installation prefix of the package.localeis the name of the current locale facet; the GNU implementation also tries generalizations, such as the language name without the territory name. so if it can't finden_USit's like "okay what about justen". resourceful queen. πcategoryisLC_MESSAGESfor thegettextanddgettextfunctions, or the argument passed to thedcgettextfunction.
if the LANGUAGE environment variable is set to a nonempty value, and the locale is not the "C" locale, the value of LANGUAGE is assumed to contain a colon separated list of locale names. the functions will attempt to look up a translation of msgid in each of the locales in turn. this is a GNU extension β special bonus feature, no cap. π₯
in the "C" locale, or if none of the used catalogs contain a translation for msgid, the gettext, dgettext and dcgettext functions return msgid. so worst case you get your own string back. it's giving "you're on your own bestie" but in a neutral way. π
β¨ RETURN VALUE β¨
if a translation was found in one of the specified catalogs, it is converted to the locale's codeset and returned. π
β οΈ the resulting string is statically allocated and must not be modified or freed. do NOT touch it. do NOT free it. hands off. it's not yours. π
otherwise msgid is returned.
β¨ ERRORS β¨
errno is not modified. no drama, no side quests, nothing. π
βοΈ
β¨ BUGS β¨
okay so, the return type ought to be const char *, but is char * to avoid warnings in C code predating ANSI C. so it's technically lying to you, but for a good cause. π
also: when an empty string is used for msgid, the functions may return a nonempty string. yes. empty in, maybe not-empty out. that's not a typo, that's just the vibe. π
β¨ SEE ALSO β¨
ngettext(3)dngettext(3)dcngettext(3)setlocale(3)textdomain(3)bindtextdomain(3)bind_textdomain_codeset(3)msgfmt(1)
and that's the whole page, bestie. GNU gettext 1.0, dated November 2024 β go translate something and slay. ππ¦β¨π
π¦ where this page comes from
- alpine 3.24: gettext-doc 1.0-r0 Β· GPL-3.0-or-later AND LGPL-2.1-or-later AND MIT
- arch rolling: gettext 1.0-2 Β· GFDL-1.2-only AND GPL-2.0-only AND GPL-2.0-or-later AND LGPL-2.0-only
- nixos 26.05: gettext 1.0
- nixpkgs unstable: gettext 1.0
- ubuntu stonking: gettext-base 1.0-3 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms