Skip to content

API turboframe.i18n

rafageist edited this page Dec 4, 2026 · 5 revisions

turboframe.i18n

Source: turboframe/i18n.py. Generated from the code of TurboFrame 0.3.0 by tools/wiki_api.py.

The languages of TurboFrame's own texts: gettext catalogues of the turboframe domain.

TurboFrame's texts are written in English and marked _("..."); locale/<lang>/LC_MESSAGES/turboframe.mo holds each translation. An application keeps its own domain for its own texts, and calls use() with the language it chose, so both speak the same one. Until it does, TurboFrame speaks English, so an English-only application never shows a translated framework text. A language without a catalogue is English.

An application's own texts are a Domain (its name and its locale folder): Domain.use() switches both, so the application and TurboFrame always speak the same language. choose_language picks it the way divide does: an environment variable, then --language on the command line, then the stored setting, then the system's.

turboframe.i18n_tools extracts, updates and compiles a domain's catalogues (with Babel, a development tool); turboframe.testing.i18n checks them in an application's tests.

Contents

DOMAIN, LOCALE_DIR, SOURCE, available, current, system_language, use, _, N_, ngettext, Domain, argv_language, choose_language

DOMAIN

'turboframe'

LOCALE_DIR

<the turboframe package>/locale (a Path)

SOURCE

'en'

available

def available() -> list[str]

English and the languages with a compiled catalogue.

current

'en'

system_language

def system_language() -> str

use

def use(language: str) -> str

Translate into language from now on (English when there is no catalogue). Returns it.

_

def _(text: str) -> str

text in the interface's language.

N_

def N_(text: str) -> str

Mark text for translation without translating it now: a text made at import, before the application chose its language, is translated with _() where it is shown.

ngettext

def ngettext(singular: str, plural: str, n: int) -> str

class Domain

class Domain
Domain(name: str, locale_dir: Path, source: str = 'en')

An application's own texts: the gettext domain name in locale_dir (<locale_dir>/<lang>/LC_MESSAGES/<name>.mo).

use() switches the application's texts and TurboFrame's to the same language. The application exposes the domain's _, N_ and ngettext:

_domain = Domain("myapp", Path(__file__).parent / "locale")
_, N_, ngettext = _domain.gettext, N_, _domain.ngettext
_domain.use(choose_language(env="MYAPP_LANGUAGE", program="myapp", setting=config.load().get("language")))
  • available() -> list[str]: The source language and those with a compiled catalogue.
  • use(language: str) -> str: Translate into language from now on (the source language when there is no catalogue), the application's texts and TurboFrame's. Returns the language in use.
  • gettext(text: str) -> str: text in the interface's language.
  • ngettext(singular: str, plural: str, n: int) -> str

argv_language

def argv_language(program: str, argv: list[str] | None = None) -> str | None

--language X (or --language=X) given to program's command. An application's texts may be made when its modules are imported, before its main parses the arguments, so the language is read here first; None when the command is not program's or has no such option.

choose_language

def choose_language(*, env: str | None = None, program: str | None = None, setting: str | None = None, argv: list[str] | None = None) -> str

The language to use, the first one of: the environment variable env (the tests and the screenshots set it), --language given to program (argv_language), the stored setting (an Options > Language), the system's. "auto" (or nothing) passes to the next one.

Clone this wiki locally