I'm writing my first Tk app with Python. I can't find an example of how to create a UI which supports multiple languages for the on screen text/dialogs. Initially I'd support English, but would like the ability to add others (from a file, perhaps XML) and have users select which language they want from an applications preferences menu.
Does anyone have advice towards the best approach?
There are a multiple parts to this.
The Tk framework uses the Tcl msgcat package for its internal messages, so you might have to use parts of that for the system provided locale things. The available messages are stored inside the $::tk_library/msgs
folder, which is usually located inside your Tcl/Tk installation.
Python usually uses the GNU gettext system for localization, see the descriptions of the gettext python module for an introduction.
So to make your UI localized, you would run all your user visible texts through gettext.gettext()
or its alias _()
before using it in labels or texts. Once done you can than provide message catalogues in the PO/POT/MO format (see polib for some nice python lib to handle those) for your translated texts.
You could also use the Tcl msgcat system instead, if you provide a small Python wrapper around the Tcl msgcat::mc
commands. Or any other msg catalogue system you like, pybabel might also be worth a look.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With