[Tkinter-discuss] Internationalising Tkinter

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Fri May 6 15:41:45 CEST 2005

Fredrik Lundh wrote:
> Martin Franklin wrote:
>>As the subject says my boss in his infinite wisdom has asked me to
>>investigate "Internationalizing Tkinter"  By this I mean I have a Python
>>& Tkinter application that among other things produces reports (using
>>the Canvas postscript method)  What I would very much like to do is have
>>this application display widgets in different languages (just Spanish
>>for now but I would like to build / use a frame work where any language
>>python / Tk supports may be used)
>>I have googled for "Internationalizing python" and came up with this
>>excellent article (still reading through it as I write this) but it is
>>quite old and I wonder if it's a little out of date.
>>Any hints, docs, links etc would be much appreciated.
> minimalistic solution:
>     from translate import T
>     Label(w, text=T("my label")).pack()
>     b = Button(w, text=T("ok"))
>     etc
> where translate.py is
>     class Translator:
>         def __init__(self, language):
>             self.texts = {}
>             self.language = language
>             ... read translation file for given language and add relevant
>                 entries to texts ...
>         def __getitem__(self, text):
>             return self.texts.get(text, text)
>     ... get country/language code (e.g. via the locale module)
>     T = Translator(language)
> where the translation file can be a tab-separated text file (or CSV or any
> other format that is easy to maintain in e.g. Excel), a prefix-encoded text
> file:
>     ** Weight
>     DE Gewicht
>     DK Vægt
>     FI Paino
>     GB Weight
>     NO Vekt
>     SE Vikt
>     PL Ciezar
>     US Weight
>     ES Peso
>     MX Peso
>     FR Poids
>     CZ Vyska
>     ** Height
>     ...
> or some other easy-to-edit-easy-to-parse format.
> for maintenance, you can add a hook to the __getitem__ method that
> outputs all untranslated items to a file:
>     class DebugTranslator(Translator):
>         def __getitem__(self, text):
>             t = self.texts.get(text)
>             if t is None:
>                 print self.language, text
>             return t or text
> if you want a more complex way to do the same thing, look for "gettext"
> in the library reference.
> </F> 

Thanks Fredrik,

It's an obvious solution now I've seen it :-)

Although I think you mean __call__ since you are calling the instance
T() not slicing or indexing it T[]

I've started playing with the gettext module and I must say I quite like
the automatic features of the system - the generation of the pot files
from the source code, and the compiling of the mo files.

Whichever method I choose, I'm glad to say with python it's _almost_ too 


More information about the Tkinter-discuss mailing list