Textual Bindings with gettext - for multiple languages #6516
Replies: 2 comments
|
You don't actually need to translate the bindings, just the descriptions. The key and the action name are internal plumbing ( The catch is when that string gets evaluated. Which is fine if you pick the language at startup. Install the translation before the app module gets imported, then use # main.py (entry point)
import gettext
gettext.translation("myapp", localedir="locale", languages=["de"]).install()
from app import MyApp # import AFTER install(), so BINDINGS sees the German strings
MyApp().run()# app.py
class MyApp(App):
BINDINGS = [
("q", "quit", _("Quit")),
("s", "save", _("Save")),
]The important bit is the import order. If Switching language live while the app is running is the harder case, since Textual doesn't re-read |
|
Wonderful - thanks for taking the time to reply.
I had a much messier fix - which I will now replace with your version.
Much nicer, and easier
…On Thu, 2 Jul 2026 at 04:20, Apoorva Verma ***@***.***> wrote:
You don't actually need to translate the bindings, just the descriptions.
The key and the action name are internal plumbing ("q" -> "quit"), they
never change between English and German. The only thing the user ever sees
is the third value, the description that shows up in the Footer, and that's
just a plain string.
The catch is *when* that string gets evaluated. BINDINGS is a class
variable, so it's built once when the class is defined (i.e. at import
time), not every time the Footer draws. So if you wrap the descriptions in
_(), gettext resolves them at import and that's the language you're stuck
with for the run.
Which is fine if you pick the language at startup. Install the translation
*before* the app module gets imported, then use _() normally:
# main.py (entry point)import gettextgettext.translation("myapp", localedir="locale", languages=["de"]).install()
from app import MyApp # import AFTER install(), so BINDINGS sees the German stringsMyApp().run()
# app.pyclass MyApp(App):
BINDINGS = [
("q", "quit", _("Quit")),
("s", "save", _("Save")),
]
The important bit is the import order. If app.py gets imported before
install() runs, the descriptions are already baked in English and it's
too late.
Switching language live while the app is running is the harder case, since
Textual doesn't re-read BINDINGS on the fly. If you need that, you're
into rebuilding bindings at runtime rather than translating strings, which
is a different (and messier) problem. But for shipping an app in English or
German picked at launch, the install-before-import trick is all you need.
—
Reply to this email directly, view it on GitHub
<#6516?email_source=notifications&email_token=CCW5DRJKKCNX2WQ7XWEW4R35CXBGBA5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZVGA2DAOJQUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVRTG633UMVZF6Y3MNFRWW#discussioncomment-17504090>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/CCW5DRIOFBRPZ25L4EOEUVT5CXBGBAVCNFSNUABHKJSXA33TNF2G64TZHMZTKNJZGU4TKOJXHNCGS43DOVZXG2LPNY5TSOJVHE4DMMNBOYBA>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/CCW5DRLHFH25WCQXINM2N7T5CXBGBA5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZVGA2DAOJQUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVJTG633UMVZF62LPOM>
and Android
<https://github.com/notifications/mobile/android/CCW5DRJ7M45MQRPX443VYN35CXBGBA5CNFSNUABIM5UWIORPF5TWS5BNNB2WEL2ENFZWG5LTONUW63SDN5WW2ZLOOQXTCNZVGA2DAOJQUZZGKYLTN5XKMYLVORUG64VFMV3GK3TUVZTG633UMVZF6YLOMRZG62LE>.
Download it today!
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Uh oh!
There was an error while loading. Please reload this page.
Hi - my apologies if this is already answered or obvious - but I can't see the solution anywhere.
I am creating an app in English and German using Textual (wonderful, and fun), and I can't see an obvious (easy) way to update the bindings to match the language.
"Bindings are not supposed to be dynamic" - this is fine, but I need the German version somehow.
Is this possible?
Many thanks for any suggestions
All reactions