“Internationalization” (often referred to as i18n) is the process to change an application so that all user visible texts are translated to the user selected language and that things like dates, money amounts and numbers in general are shown in a format the user is familiar with/or used to.
The easiest way to show what is needed is by using a little code sample.
Text translation in Python is done using gettext [1] , to ensure that all
wxPython labels are also translated we will use wx.Locale
and
wx.GetTranslation
.
How to prepare your source code to enable translation of texts:
aString = _(u"This is a string which will be translated")
As you can see it is very simple, you just enclose the text with the translation function “_()”, obviously there is a bit more to it, see below.
Enabling I18N for a whole application you would do some setup in the application file along the following lines:
import builtins
builtins.__dict__['_'] = wx.GetTranslation
Here we setup the “_” translation function and making it available application by adding it to builtin.
The code required to change to a different language is as follows:
def updateLanguage(self, lang):
"""
Update the language to the requested one.
Make *sure* any existing locale is deleted before the new
one is created. The old C++ object needs to be deleted
before the new one is created, and if we just assign a new
instance to the old Python variable, the old C++ locale will
not be destroyed soon enough, likely causing a crash.
:param string `lang`: one of the supported language codes
"""
# if an unsupported language is requested default to English
if lang in appC.supLang:
selLang = appC.supLang[lang]
else:
selLang = wx.LANGUAGE_ENGLISH
if self.locale:
assert sys.getrefcount(self.locale) <= 2
del self.locale
# create a locale object for this language
self.locale = wx.Locale(selLang)
if self.locale.IsOk():
self.locale.AddCatalog(appC.langDomain)
else:
self.locale = None
You need to extract all the text strings marked by the “_” function, a little
script geni18n.py is in the downloadable zip file
,
it will extract all the strings and generate a .pot
file, which is put to
the locale folder. The geni18n.py script will also generate the .mo
files
for defined languages.
The .pot
file is then provided to the translators and they use it to
generate a .po
file for the language they translate too or they can also use
the .pot
file to merge new/changed text strings to an existing .po
file.
To do the actual translation we recommend poEdit [2] , it allows you to
create or update a translation catalog (.po
file) from the .pot
file.
In the downloadable zip file
we included a small sample application showing the above in action.
app_base.py contains the initialization code
sampleapp.py is the main frame/application, just run this to see things in action
geni18n.py is the script to generate the .pot
file and it also generates the .mo
files.
Note
The application has a button which displays a file dialog, as wxPython uses a native widget for this the text are shown in the operating system language and not the language which is selected in app_base.py.
“Localization”, often referred to as “L10n”, is the process to adapt the display of dates and numbers to local custom.
E.g. “4/5/2012” would for an American mean April 5. 2012, but for most Europeans it would be 4. May 2012.
Todo
to be written
Todo
to be written
Footnotes