Skip to content

Python ​

Chooser for Python applications. Extraction, catalogs, and call shapes live in Python applications. The step-by-step for an existing single-locale app is Add i18n to a Python app, with an i18n.py prompt and a Django / gettext prompt.

Streamlit, FastAPI, Flask, Pyramid, Tkinter, PyQt, PySide, and PyWebView call the generated i18n.py helper (Python 3.10 or newer). Django keeps Django's gettext. Four runnable copies ship in the repository: examples/python-streamlit, examples/python-flask, examples/python-fastapi, and examples/python-gettext. Pyramid, Django, Tkinter, Qt, and PyWebView have no separate example directory.

Which template ​

FrameworkInit templateCatalogExample
Streamlitui-pythonFlat JSON keyed by the source stringpython-streamlit
FastAPIui-pythonFlat JSON keyed by the source stringpython-fastapi
Flask + Jinja, flat JSONui-pythonFlat JSON keyed by the source stringpython-flask
Flask / Jinja gettextui-python-flask.po / .mo under translations/Use the Flask notes below
Pyramidui-python or ui-python-flaskSame as FlaskSame wiring as Flask
Djangoui-python-djangolocale/<lang>/LC_MESSAGES/django.poNo example directory
Existing GNU gettextui-python-gettextFlat JSON and .po / .mo, or gettext onlypython-gettext
Tkinterui-pythonFlat JSON keyed by the source stringNo example directory
PyQt / PySideui-python or a gettext templateFlat JSON and/or .poNo example directory
PyWebView / PyWebKitA Python template for the shellShell catalogs, plus HTML markers or one HTML file per localeNo example directory

ui-python, ui-python-gettext, and ui-python-flask write i18n.py when that file is missing. ui-python-django does not. Add --with-readme to also translate the repository README: Add i18n to your README and Markdown docs.

Framework notes ​

Streamlit ​

streamlit_language_selector() writes the locale into session state and the query string. Give every widget a fixed key. Details: Locale — Streamlit.

FastAPI ​

Read ?lang= or Accept-Language in middleware, call set_locale, and reset_locale in finally so concurrent requests keep their own locale. Details: Locale — FastAPI.

Flask and Pyramid ​

The python-flask example uses flat JSON. init -t ui-python-flask is the gettext layout under translations/ for an app that already uses Flask-Babel-style catalogs. Either way, set the locale from the request and inject _, t, current_locale, ui_languages, and direction into the template context. Pyramid uses the same request-scoped pattern. Details: Locale — Flask and Pyramid.

Django ​

Keep gettext, ngettext, and {% blocktrans %}. init -t ui-python-django matches Django's locale/ directory and the django domain, and leaves Django's runtime in place. Details: Locale — Django.

Existing gettext ​

Leave _(), gettext(), and ngettext() in place. Point ui.gettext at the current catalog tree and run extract, then translate-ui. The python-gettext example is gettext-only (flatOutput: false). init -t ui-python-gettext also writes flat JSON. Details: Locale — Existing gettext.

Tkinter, Qt, and PyWebView ​

Call t() or _() when creating each Tkinter widget, then rebuild the visible text after set_locale. PyQt and PySide self.tr("...") calls are extracted, and Qt .ui files contribute <string> elements unless notr="true". For PyWebView or PyWebKit, extract the Python shell with a template above and extract the HTML page with data-i18n markers or with one HTML document per locale. Details: Locale — Tkinter, PyQt / PySide, and PyWebView.

Released under the MIT License.