[Tkinter-discuss] Internationalising Tkinter
Fredrik Lundh
fredrik at pythonware.com
Fri May 6 14:41:24 CEST 2005
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.
>
> http://python.fyxm.net/workshops/1997-10/proceedings/loewis.html
>
> 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>
More information about the Tkinter-discuss
mailing list