Repository navigation
Languages
TurboFrame's own texts (dialog labels, buttons, messages) are in English, and in Spanish too. They come from TurboFrame's gettext domain, turboframe, separate from your application's. This page is about choosing the language; Translating Your Application is about making and checking the catalogues.
| Use it for | showing your application and TurboFrame's dialogs in the user's language; Options > Language |
| Import |
from turboframe.i18n import Domain, N_, choose_language; from turboframe.ui.language import CM_LANGUAGE, LanguageMixin
|
| Needs | nothing beyond TurboFrame (gettext is Python's); the i18n extra only to make catalogues |
| Extend | your own Domain; LANGUAGES, language_choices(), language_label(), language_saved()
|
TurboFrame speaks English until your application chooses:
from turboframe import i18n
i18n.use("es") # TurboFrame's texts in Spanish
i18n.use(i18n.system_language())
print(i18n.available()) # ['en', 'es']
print(i18n.current) # the language in useCall it when your application chooses its own language, so that both speak the same one. A dialog made after the call shows the new language. A language without a catalogue is English.
_(text) translates in TurboFrame's domain; N_(text) marks a text that is translated later (a table of labels, translated where it is shown, as the theme labels are); ngettext(singular, plural, n).
Your application keeps its own domain and catalogues. Domain(name, locale_dir, source="en") holds them, and its use() switches your texts and TurboFrame's together:
# myapp/i18n.py
from pathlib import Path
from turboframe.config import JsonStore
from turboframe.i18n import N_, Domain, choose_language
settings = JsonStore("myapp")
domain = Domain("myapp", Path(__file__).parent / "locale") # locale/<lang>/LC_MESSAGES/myapp.mo
_, ngettext = domain.gettext, domain.ngettext
domain.use(choose_language(env="MYAPP_LANGUAGE", program="myapp", setting=settings.get("language")))Your modules import _, N_ and ngettext from there. domain.available() lists the source language and those with a compiled catalogue; domain.current is the one in use.
choose_language(*, env=None, program=None, setting=None, argv=None) takes the first of:
- the environment variable
env(tests and screenshots set it); -
--language X(or--language=X) given to theprogramcommand (argv_language(program)). Your texts may be made when your modules are imported, before yourmainparses its arguments, so it readssys.argvitself; - the stored
setting(what Options > Language kept); - the system's language (
system_language():LANGUAGE,LC_ALL,LC_MESSAGES,LANG).
"auto", or nothing, passes to the next one.
Choose the language before your menus are built (before the application object is made), since init_menu_bar() translates its texts once.
LanguageMixin (turboframe.ui.language) gives your DesktopApp the dialog Turbo Vision applications had under Options: the languages as radio buttons, each named in itself (English, Español), and Automatic (the system's). OK keeps the choice in your store under "language", and a message says it applies from the next start: your texts were made when the application started.
from textual import work
from textual_vision.menus import Menu, MenuItem, SubMenu
from myapp.i18n import _, domain, settings
from turboframe.ui.app import DesktopApp
from turboframe.ui.language import CM_LANGUAGE, LanguageMixin
class MyApp(LanguageMixin, DesktopApp):
language_store = settings # where "language" is kept (None: for the session)
language_domain = domain # its languages are those with a catalogue (None: TurboFrame's)
LANGUAGES = {"en": "~E~nglish", "es": "E~s~pañol", "fr": "~F~rançais"} # auto: TurboFrame names it
def init_menu_bar(self) -> Menu:
return Menu([SubMenu(_("~O~ptions"), MenuItem(_("~L~anguage..."), CM_LANGUAGE))])
def on_command_message(self, message) -> None:
if message.command == CM_LANGUAGE:
message.stop()
self.language()
@work
async def language(self) -> None:
await self.language_command(CM_LANGUAGE) # True: it was CM_LANGUAGEAt the next start, choose_language(setting=settings.get("language")) applies it (above).
| Class attribute or hook | Default |
|---|---|
language_store |
None: the choice lasts for the session (a MemoryStore) |
language_domain |
None: TurboFrame's own languages |
LANGUAGES |
code to name, each in its own language, with a ~hotkey~: auto, en (English), es (Español). Without auto, TurboFrame names it, translated; a language it does not name shows its code |
language_choices() |
auto, then the domain's available() languages |
language_label(code) |
the name in LANGUAGES, with a hotkey (hotkey_label: its own ~X~, else its first letter) |
async language_saved(code) |
the message that the choice applies from the next start |
CM_LANGUAGE is FRAMEWORK_COMMANDS[50]. language_command(command) runs it from your command worker and returns False for any other command (see Application Mixins). await self.choose_language() runs the dialog directly. Do not mix up this method with the function turboframe.i18n.choose_language, which picks the language at start. language_setting() reads the stored choice ("auto" when there is none).
LanguageDialog(codes, labels, current) is the dialog alone: once it ends with OK, chosen is the code.
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links