Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter app - Allowing for multiple languages

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?

like image 552
Dr.Avalanche Avatar asked Dec 19 '22 19:12

Dr.Avalanche


1 Answers

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.

like image 59
schlenk Avatar answered Dec 27 '22 11:12

schlenk