Skip to content

Translating Your Application

rafageist edited this page Dec 4, 2026 · 4 revisions

Translating Your Application

Your application's texts live in gettext catalogues of its own domain (see Languages for the Domain and how the language is chosen). TurboFrame gives you two tools for them: a command that makes the catalogues, and checks for your tests.

Use it for extracting your texts, starting and updating each language's .po, compiling the .mo files, finding what is wrong
Import python -m turboframe.i18n_tools ...; from turboframe.testing.i18n import catalogue_problems, unique_hotkeys, local_underscore_problems
Needs i18n_tools: the i18n extra (Babel), a development tool. testing.i18n: nothing more
Extend --locale, --project, --bugs; Catalogues from Python; keywords= for your own marking functions

Marking the texts

Mark every text the user reads with your domain's functions:

from myapp.i18n import N_, _, ngettext

MenuItem(_("~O~pen..."), Command.OPEN, "F3")
self.notify(ngettext("{n} file saved", "{n} files saved", n).format(n=n))
LABELS = {"draft": N_("Draft"), "done": N_("Done")}   # made at import: translated where shown, _(LABELS[k])
  • Keep the ~hotkey~ marks: one letter, unique in its menu or dialog (or a key: ~F2~, ~Alt+X~).
  • Keep the {placeholders}, and use .format() after translating, never inside _().
  • Keep to characters one cell wide (see Glyphs).
  • A TurboFrame class that shows a text of its own translates it with TurboFrame's catalogue. When your subclass replaces such a text, translate it through the class's hook, in your domain: ListWindow.empty_text() for EMPTY (see Debugger Windows).

Making the catalogues

turboframe.i18n_tools extracts the texts marked _(), N_() and ngettext(), updates each language's .po, and compiles the .mo files you ship. It uses Babel, from the i18n extra, as a development tool; your application needs only gettext.

pip install "turboframe[i18n] @ git+https://github.com/divengine/turboframe@v0.3.0"
python -m turboframe.i18n_tools --domain myapp --src src/myapp extract    # src/myapp/locale/myapp.pot
python -m turboframe.i18n_tools --domain myapp --src src/myapp init es    # a new language
python -m turboframe.i18n_tools --domain myapp --src src/myapp update     # the .pot merged into every .po
python -m turboframe.i18n_tools --domain myapp --src src/myapp compile    # .po -> .mo
python -m turboframe.i18n_tools --domain myapp --src src/myapp all        # extract, update, compile
python -m turboframe.i18n_tools --domain myapp --src src/myapp check      # what is wrong, if anything
Option
--domain the gettext domain: the .mo file's name, your Domain's name (required)
--src the package whose texts are marked (required)
--locale the locale folder; <src>/locale by default
--project the project's name in the .pot; the domain by default
--bugs where translators report a wrong text (msgid-bugs-address)

The catalogues are <locale>/<domain>.pot and <locale>/<lang>/LC_MESSAGES/<domain>.po, next to the .mo each compiles into. The header lines Babel fills with placeholders or dates are left out, so a catalogue changes only when a text does. check prints no problems and exits with 0, or lists each problem and exits with 1: put it in your CI.

Translate the .po files with any gettext editor (Poedit, Weblate). Ship the .mo files with your package, as package data:

[tool.setuptools.package-data]
myapp = ["locale/*/LC_MESSAGES/*.mo"]

The same from Python: Catalogues(domain, src, locale=None, *, project=None, bugs=None) with extract(), update(), init(language), compile() and check().

A complete example

The workbench (examples/workbench/) has its own catalogue, workbench, with a Spanish translation. Its texts are marked with its domain's _, it chooses its language in main() before the menus are made, and Options > Language keeps the choice. Its catalogue was made, and is kept, with these commands from the repository's root:

python -m turboframe.i18n_tools --domain workbench --src examples/workbench --project workbench \
    --bugs https://github.com/divengine/turboframe/issues extract
python -m turboframe.i18n_tools --domain workbench --src examples/workbench init es     # once
python -m turboframe.i18n_tools --domain workbench --src examples/workbench update
python -m turboframe.i18n_tools --domain workbench --src examples/workbench compile
python -m turboframe.i18n_tools --domain workbench --src examples/workbench check

and a test keeps it complete:

from turboframe.testing.i18n import catalogue_problems

def test_the_workbench_catalogue_is_complete():
    po = EXAMPLES / "workbench" / "locale" / "es" / "LC_MESSAGES" / "workbench.po"
    assert catalogue_problems(po, src=EXAMPLES / "workbench") == []

The workbench in Spanish

Checking them in your tests

turboframe.testing.i18n checks a catalogue without Babel. Each function returns what is wrong, so a test asserts an empty list and a failure names every problem:

from pathlib import Path

from turboframe.testing.i18n import catalogue_problems, local_underscore_problems, unique_hotkeys

from myapp.app import MyApp

SRC = Path(__file__).parents[1] / "src" / "myapp"


def test_the_spanish_catalogue():
    assert catalogue_problems(SRC / "locale/es/LC_MESSAGES/myapp.po", src=SRC) == []


def test_menu_hotkeys_are_unique(isolated_config):
    for menu in MyApp().init_menu_bar().items:
        assert unique_hotkeys(i.name for i in menu.sub_menu.items if not i.is_separator) == []


def test_no_local_underscore():
    assert local_underscore_problems(SRC) == []    # `x, _ = ...` beside `_("...")` breaks gettext

catalogue_problems(po, *, src=None, keywords=KEYWORDS, compiled=True) finds:

  • texts marked in src but missing from the catalogue, and texts the code no longer marks;
  • entries that are untranslated or fuzzy;
  • placeholders or hotkey marks that differ between the text and its translation, and a hotkey that is neither one letter nor a key (F2, Alt+X);
  • characters wider than one cell;
  • with compiled, a .mo missing, or older than its .po.

The other checks:

Function Returns
unique_hotkeys(labels) the hotkeys used more than once among a menu's or a dialog's labels
hotkey_problems(msgid, msgstr) what is wrong with one translation's hotkey
local_underscore_problems(src) functions that assign _ (x, _ = pair) and also call _("..."): the assignment hides gettext in the whole function
marked_msgids(src, keywords=KEYWORDS) every text src marks
read_po(po) a .po file's Entrys (plurals included), read without Babel

TurboFrame's own Spanish catalogue is checked with these same functions.

API: turboframe.i18n_tools, turboframe.testing.i18n.

Clone this wiki locally