Tkinter app - Allowing for multiple languages
internationalization, python, tkinter
Solution
The GUI should construct its text with strings from a default language. So, for example, your code would look something like this:
import Tkinter as Tk
root = Tk.Tk()
root.title('Program')
menuButton = Tk.Button(root, text='Menu')
menuButton.grid(row=0, column=0)
root.mainloop()
Changing the language is as simple as modifying each Tk widget depending on the selected language. For example,
def change_language(lang):
if lang == 'English':
root.title('Program')
menuButton.config(text='Menu')
elif lang == 'Spanish':
root.title('Programa')
menuButton.config(text='Menú')
To make it easier to write the code, you could store your language data in a file (e.g. csv), parse it into lists or dictionaries, and have something like this:
english = ['Program', 'Menu']
spanish = ['Programa', 'Menú']
def change_language_2(lang):
root.title(lang[0])
menuButton.config(text=lang[1])
Problem
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?