To handle internationalization in Python, separate user-visible text from code with the standard gettext module, format dates, numbers and currencies with the Babel library, and handle time zones with the standard zoneinfo module. Store and process text as Unicode (str), and avoid changing process-wide settings with locale.setlocale() in multi-user programs.
Most real-world problems come from a handful of causes: hard-coded strings, concatenated sentences, naive date formatting, wrong encodings and plural rules that are not English-shaped. This guide covers each one with current Python 3 practice.
Internationalization (i18n) is designing software so it can support any language and region without code changes. Localization (l10n) is adapting it to one specific locale: translating text, and choosing date, number and currency formats. A locale identifier such as fr_FR or pt-BR combines language and region, and it controls sorting, formats and plural rules.
The gettext module reads compiled .mo catalogs and returns the translation for a message. Use gettext.translation() and get a translation object, instead of installing _ into builtins.
With fallback=True, a missing catalog returns the original English text instead of raising an error. The directory layout is locale/fr/LC_MESSAGES/myapp.mo.
Generate a template from your source with the tools that ship with Python's gettext ecosystem, then compile it.
Write full sentences with named placeholders, for example "Hello, %(name)s", and never build messages by concatenation, because word order differs between languages. Use ngettext for plurals: Polish, Arabic and Russian have more than two plural forms, which the catalog header describes.
Babel uses the Unicode CLDR locale data, so output matches what speakers expect. Install it with pip install babel.
The exact spacing characters and short forms can vary with the CLDR data version bundled in your Babel release, so avoid asserting on them in tests unless you pin the version.
The standard locale module calls the C library and changes process-wide state. locale.setlocale() is not thread-safe, requires the locale to be installed on the operating system, and affects every thread. In servers, pass an explicit locale to Babel for each request. Note also that locale.getdefaultlocale() is deprecated since Python 3.11 and slated for removal; use locale.getlocale() or locale.getencoding() instead.
Since Python 3.9 the standard library includes zoneinfo, which replaces pytz for new code. Store timestamps in UTC, and convert to the user's time zone only for display.
On Windows, zoneinfo needs the tzdata package (pip install tzdata) because the system has no IANA database.
Python 3 strings are Unicode, so most problems appear at the edges. Open files with an explicit encoding (open(path, encoding="utf-8")), decode HTTP and database input as UTF-8, and normalize text before comparing it, because the same visible character can have several representations.
For language-aware sorting, use a collation-aware tool such as Babel or ICU bindings, because plain sorted() compares code points and orders accented letters incorrectly for many languages. Use str.casefold() instead of lower() for case-insensitive matching.
, and . as number separators.datetime objects without a time zone.Use the standard gettext module for translation catalogs and Babel for locale-aware formatting. Web frameworks wrap them: Django has built-in support and Flask uses Flask-Babel.
Use zoneinfo in new code, since it is part of the standard library from Python 3.9 and works directly with datetime. Keep pytz only for legacy code that depends on it.
Use ngettext with a catalog that defines the plural rules for each language. Do not write if count == 1 yourself, since other languages have different rules.
On the web, read the Accept-Language header and let the user override it with a saved preference. In scripts, avoid process-wide locale detection and pass the locale explicitly.
Handle internationalization in Python by translating whole sentences with gettext, formatting values with Babel, using zoneinfo and UTC for time, and treating all text as Unicode. Pass the locale explicitly instead of relying on global locale state, and test plurals, long text and right-to-left layouts early.