On this page
gettext
Prev: getpass | Up: stdlib_module | Next: glob
Rel: wave, colorsys, locale, turtle, turtledemo
Ref: docs.python.org — gettext
Tags: #public #python3 #stdlib
Import
import gettext
Overview
GNU gettext internationalization (i18n) and localization (l10n) API for translating user-facing strings using .po/.mo message catalogs (translation(), GNUTranslations, NullTranslations). Use gettext to separate translatable UI text from code and handle language-specific plural rules cleanly.
Quick Start
import gettext
# Fallback NullTranslations passes strings through unchanged when no .mo catalog exists
t = gettext.translation("myapp", localedir="locale", languages=["fr"], fallback=True)
_ = t.gettext
ngettext = t.ngettext
assert _("Hello") == "Hello"
assert ngettext("%d file", "%d files", 2) % 2 == "2 files"
Implementation Walkthrough
1. Class-Based vs. Global (install()) Translation
Prefer the class-based API (t = gettext.translation(...)) over gettext.install()—it avoids mutating builtins._ and lets web servers or multi-tenant apps serve different languages per request:
import gettext
from pathlib import Path
def get_translator(locale_dir: Path, lang: str) -> gettext.NullTranslations:
return gettext.translation(
domain="messages",
localedir=str(locale_dir),
languages=[lang],
fallback=True,
)
# Standard workflow:
# 1. Extract strings with xgettext / pybabel into messages.pot -> <lang>/LC_MESSAGES/messages.po
# 2. Compile .po -> .mo with msgfmt
# 3. Load via get_translator(Path("locale"), "es").gettext("Save")
Gotchas & Boundaries
- Never translate pre-formatted f-strings:
_(f"Hello {name}")breaks catalog lookup because everynameproduces a new key. Translate the template first:_("Hello {name}").format(name=name). - Always pass
fallback=Trueunless missing catalogs should crash: Without it,gettext.translation()raisesFileNotFoundErrorwhen a locale has no compiled.mofile. - Avoid
gettext.install()in libraries: Injecting_intobuiltinscollides with the REPL's last-result_variable and prevents per-request language switching.
Common Functions
- gettext.bindtextdomain() —
bindtextdomain(domain, localedir=None)—gettext.bindtextdomain()function. - gettext.Catalog() —
Catalog(domain, localedir=None, languages=None, class_=None, fallback=False)—gettext.Catalog()function. - gettext.dgettext() —
dgettext(domain, message)—gettext.dgettext()function. - gettext.dngettext() —
dngettext(domain, msgid1, msgid2, n)—gettext.dngettext()function. - gettext.dnpgettext() —
dnpgettext(domain, context, msgid1, msgid2, n)—gettext.dnpgettext()function. - gettext.dpgettext() —
dpgettext(domain, context, message)—gettext.dpgettext()function. - gettext.find() —
find(domain, localedir=None, languages=None, all=False)—gettext.find()function. - gettext.gettext() —
gettext(message)—gettext.gettext()function. - gettext.install() —
install(domain, localedir=None, *, names=None)—gettext.install()function. - gettext.ngettext() —
ngettext(msgid1, msgid2, n)—gettext.ngettext()function. - gettext.npgettext() —
npgettext(context, msgid1, msgid2, n)—gettext.npgettext()function. - gettext.pgettext() —
pgettext(context, message)—gettext.pgettext()function. - gettext.textdomain() —
textdomain(domain=None)—gettext.textdomain()function. - gettext.translation() —
translation(domain, localedir=None, languages=None, class_=None, fallback=False)—gettext.translation()function.
Classes & Objects
- gettext.GNUTranslations —
gettext.GNUTranslationsclass. (Methods:.add_fallback(),.charset(),.gettext(),.info(),.install(),.ngettext()) - gettext.NullTranslations —
gettext.NullTranslationsclass. (Methods:.add_fallback(),.charset(),.gettext(),.info(),.install(),.ngettext())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/cmd
- tuts/python3/stdlib_module/colorsys
- tuts/python3/stdlib_module/getpass
- tuts/python3/stdlib_module/glob
- tuts/python3/stdlib_module/idlelib
- tuts/python3/stdlib_module/locale
- tuts/python3/stdlib_module/shlex
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tkinter
- tuts/python3/stdlib_module/turtle
- tuts/python3/stdlib_module/turtledemo
- tuts/python3/stdlib_module/wave
Related notes
- tuts/python3/stdlib_module/colorsys Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/getpass Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/glob Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/locale Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/turtle Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/turtledemo Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/wave Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/cmd links to this page; shared tags: #stdlib