gettext
gettext(string $message): string
GNU gettext message translation: looks the msgid up in the current domain's .mo catalog, returning the translation when found and the msgid unchanged when not — this entry deliberately ships no catalog and demos the zero-dependency, genuinely runnable flow via the fallback behaviour (bindtextdomain + textdomain + the _() alias).
C C23
#include <stdio.h>
/* the C standard library has no gettext: this models its contract — a catalog
lookup that degrades to the msgid when no translation exists (exactly what
gettext(3) returns when the .mo file for the current locale is missing) */
static const char *translate(const char *msgid) {
/* an empty catalog: every lookup misses */
return msgid;
}
int main(void) {
printf("%s\n", translate("Hello World"));
printf("%s\n", translate("Hello World")); /* the _() alias behaves identically */
return 0;
}
Hello World Hello World
C++ C++26
#include <iostream>
#include <map>
#include <string>
// C++ has no message catalog in its standard library: this models the gettext
// contract — a lookup that degrades to the msgid when no translation exists
int main() {
const std::map<std::string, std::string> catalog; // empty: every lookup misses
auto _ = [&catalog](const std::string &msgid) -> std::string {
auto it = catalog.find(msgid);
return it == catalog.end() ? msgid : it->second;
};
std::cout << _("Hello World") << "\n";
return 0;
}
Hello World
C# v11.0.100
// ResourceManager is the BCL-native catalog; a missing resource degrades to the msgid
using System.Resources;
var rm = new ResourceManager("messages", typeof(Program).Assembly);
try {
Console.WriteLine(rm.GetString("Hello World"));
} catch (MissingManifestResourceException) {
Console.WriteLine("Hello World"); // no catalog on the classpath -> msgid fallback
}
Hello World
Go v1.27.4
package main
import "fmt"
// Go's stdlib has no message catalog: a lookup map models the gettext contract —
// a miss degrades to the msgid (what gettext does with no .mo file)
var catalog = map[string]string{} // empty: every lookup misses
func gettext(msgid string) string {
if msg, ok := catalog[msgid]; ok {
return msg
}
return msgid
}
func main() {
fmt.Println(gettext("Hello World"))
}
Hello World
Java v27.0.1
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
public class Main {
public static void main(String[] args) {
// ResourceBundle is the JDK-native catalog; with no bundle on the
// classpath the lookup degrades to the msgid — the gettext contract
try {
ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.GERMAN);
System.out.println(bundle.getString("hello"));
} catch (MissingResourceException e) {
System.out.println("Hello World"); // no catalog -> msgid fallback
}
}
}
Hello World
JavaScript v26.13.1
// JS has no catalog in stdlib: the gettext contract is a lookup that degrades
// to the msgid (exactly what gettext() does when no .mo translation exists)
const catalog = new Map(); // empty: every lookup misses
const _ = (msgid) => catalog.get(msgid) ?? msgid;
console.log(_('Hello World'));
Hello World
PHP v8.6.0
// bind the domain to a directory with no .mo catalog anywhere
bindtextdomain('demo', sys_get_temp_dir());
textdomain('demo');
echo gettext('Hello World'), "\n"; // lookup misses -> the msgid comes back as-is
echo _('Hello World'), "\n"; // _() is the ubiquitous alias
Hello World Hello World
Python v3.15.2
import gettext
# bind the domain to a directory with no .mo catalog anywhere
gettext.bindtextdomain('demo', '/nonexistent-locale-dir')
gettext.textdomain('demo')
print(gettext.gettext('Hello World')) # lookup misses -> the msgid comes back as-is
Hello World
Rust v1.99.1
use std::collections::HashMap;
// Rust's std has no message catalog: a lookup map models the gettext contract —
// a miss degrades to the msgid (what gettext does with no .mo file)
fn gettext<'a>(catalog: &'a HashMap<&'a str, &'a str>, msgid: &'a str) -> &'a str {
catalog.get(msgid).map_or(msgid, |v| v)
}
fn main() {
let catalog = HashMap::new(); // empty: every lookup misses
println!("{}", gettext(&catalog, "Hello World"));
}
Hello World
TypeScript v26.13.1
// TS has no catalog in stdlib either: the gettext contract degrades to the msgid
const catalog: Map<string, string> = new Map(); // empty: every lookup misses
const _ = (msgid: string): string => catalog.get(msgid) ?? msgid;
console.log(_('Hello World'));
Hello World
Language differences
PHP: the gettext extension binds GNU gettext; the flow has three steps — bindtextdomain(domain, dir) points at the dir/<locale>/LC_MESSAGES/<domain>.mo search root, textdomain(domain) selects the current domain, and gettext()/_() look words up (_() is the alias; the 8.6 deprecation list explicitly leaves the function _() untouched — only the constant name _ is banned). Plurals go through ngettext(). When a translation is missing the msgid comes back unchanged — this entry deliberately uses that fallback for a deterministic demo: no .mo is generated and no OS locale is required, so output is identical in every environment (textbook "honest degradation"). Engineering notes (from the manual's user notes): .mo files are cached per process — restart or rotate the domain after editing translations; set both putenv('LC_ALL=...') and setlocale; on Windows use full names like de_DE without setlocale; extract strings with xgettext --from-code=UTF-8.
C/C++: the gettext(3) family (bundled with glibc; macOS needs -lintl); the example re-implements the contract with an empty catalog because of linking constraints. Real projects: bindtextdomain + textdomain + _(), then compile .po → .mo with msgfmt.
C#: .resx + ResourceManager is the resources-school counterpart; a missing manifest throws MissingManifestResourceException (caught in the example), while a missing key returns null — both degradation levels must be handled.
Java: ResourceBundle + .properties (the native counterpart!); getBundle walks the locale fallback chain for messages[_de].properties and throws MissingResourceException when nothing is found (caught in the example).
Python: the gettext module (same family!) — bindtextdomain/textdomain/gettext share names and semantics with the C API, and a missing .mo returns the msgid unchanged; the more Pythonic spelling is gettext.translation('demo', fallback=True).gettext.
JS/TS: no built-in — the ecosystem lives at the framework/library layer (i18next, react-intl, vue-i18n; node-gettext on the server); the example reproduces the contract with a Map.
Go: no i18n in stdlib — go-i18n (.toml/.json catalogs + localizer.MustLocalize) or gotext.
Rust: fluent (Mozilla's l10n format) / i18n-embed (compile-time-embedded .ftl); the example reproduces the contract with a HashMap.
Ecosystem side-by-side: C/PHP/Python share the GNU gettext lineage; Java/.NET follow the resource-file school; JS/Go/Rust have no official standard and a blooming ecosystem — the real选型分水岭 is "who maintains the translation files" and "how complex are plural/gender rules", not API shape.