[Tutor] Pay-at-Pump Use Case - Tkinter GUI

Alan Gauld alan.gauld at yahoo.co.uk
Sun Feb 23 13:52:37 EST 2020


On 23/02/2020 13:10, Jon Davies wrote:

> For translation, I am struggling to find the right way 

There is a well understood solution for this that is used
in real world programs regardless of language and is supported
in Python. Do not reinvent the wheel!

Search for terms like "internationalisation", "localisation",
"locale" "i18n".

The industry standard tool is called "gettext" and python
has a gettext module - check its documentation. It is non trivial
but it does mean that your code will be easily extendible to any language.

The basic process looks like this:

1) Write your python code with a gettext preamble and mark
the strings that need to be translated(ie the ones displayed
by the UI) for use with the gettext functions. In Python that usually
means things like:

print(_("Hello world"))  # _() marks string for gettext

2) Run the xgettext tool (non-python) to extract those strings into a
template file, usually "messages.po"

3) Create translation files providing the equivalent strings in the
languages you support. Google translate can give a helpful start!

4) Use another tool - msgfmt - from the gettext suite to translate
the .po files into the format used by gettext, usually ending in.mo

5) Ship the folder containing these files with your application.

(On Windows the tools are found in Tools/i18n of the python
distribution. On Linux the OS has the tools already.)
If you need a longer example then ask again.

<SHAMELESS PLUG>
This is described in Chapter 4 of my book "Python Projects"...
</SHAMELESS PLUG>

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list