i18n and GUI under Windows

A

Andr? Roberge

In short: I'm looking for a *simple* example of how to write a program
that can have its GUI in at least two languages under Windows ...
using only Python, of course!

I've downloaded Guido van Robot (great program!) so that my kids could
learn the basics of programming. Apparently it is set up to work in
French under *nix, but not under Windows. I've looked at the source
code, found where this is noted, scratched my head, looked at the web
to find solution, to no avail. GvR uses wxPython.

On the wxWidget website, it is said that there is a "internat" sample
program, that would seem to do what I'm looking for, but it doesn't
seem to be in the wxPython distribution.

I haven't found any simple demonstration with Tkinter either (or
Pythoncard, anygui, easygui, etc...). Sigh....

I *think* I understand the basics of unicode encoding (at the very
least, enough to have been able to modify Leo [another great program!]
to work properly on my machine [actually, it was well explained on the
web site]).

Any pointer, or sample program (like those found in useless Python)
demonstrating how this can be done would be appreciated. So, how do
you write a program that can display on a label (or menu item) either
"Guido" or "André" (Andre´) depending on the user's choice.
[Ok, that's not a good translation, but still :)]

I am not a real programmer, just a hobbyist. However, if I can get
enough pointers to answer my question within the next week, I commit
myself to:
1) write a detailed tutorial in both French and English, explaining
how to do this in a way even I can understand :)
2) find a way to modify GvR so that it works in languages other than
English on Windows.

Hey, I've already installed Poedit in anticipation of all the
translations I am going to do ;-)

André Roberge
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Andr? Roberge said:
In short: I'm looking for a *simple* example of how to write a program
that can have its GUI in at least two languages under Windows ...
using only Python, of course!

I think the most simple approach is using a dictionary. Make one
Python file per language, and have that file contain only a single
dictionary named "translations". For keys, you have three options:

- use the English messages as keys (e.g. "Please enter your name")
- use short identifiers as keys (e.g. "EnterName")
- use numbers as keys (e.g. 116)

Regards,
Martin
 
F

Fred Pacquier

(e-mail address removed) (Andr? Roberge) said :
In short: I'm looking for a *simple* example of how to write a program
that can have its GUI in at least two languages under Windows ...
using only Python, of course!

Well, André, if you really want *simple* (as in *simplistic*, *simple-
minded* or *can't-get-no-simpler* :) I have something that does just that
(english/french under wxPython)... Note that most of the other
suggestions that were given in the rest of this thread are probably
better for "serious" work ; but going all the way to GNU gettext is a bit
involved, and quick-n-dirty hacks such as this can be enough for simple
apps and a handful of languages...

It goes something like this (purists please avert your eyes :),


* at the beginning of the main module I have these statements :

#---------------------------------------------------------------------
# determine locale and set global french/english flag
# import localized strings and define gettext-like '_' function

import locale
lang, cp = locale.getdefaultlocale()
lang = lang[:2]
if not lang == 'fr' : lang = 'en'

import l10n

def _(msg) :
if lang == 'fr' :
return msg
else :
return l10n.trans[lang].get(msg, msg)
# if no translation found return the original (fr) string


* afterwards in your code, everywhere a string appears that needs to be
localized, wrap it in a _() function call (this is the convention set by
GNU gettext that everyone generally uses even if they don't use gettext
itself :)
i.e.: "André" becomes _("André")


* module l10n.py is just one big dictionary with language locales as
strings and dictionaries of original/translated strings as values, as in:

# MyApp localized strings
# -*- coding: iso-8859-1 -*-

trans = {}

trans['en'] = {
"André" : "Andrew",
"Bonjour" : "Hello",
"Dictateur Bienveillant A Vie" : "BDFL" }


* and that's it. You'll notice that in my example the original version is
in French and English is a translation. That's because it was grafted as
an afterthought/experiment onto already finished code. Forward-thinking
authors may want to do it the other way around, especially if they expect
to get help with other languages :)

Then it's not too hard to add, say, a trans['de'] dict to l10n.py and
change the locale test above to use it. If automatic detection is not
adequate, then the GUI needs to provide a widget or dialog for the user
to set the desired language...

HTH,
fp
 
A

Andr? Roberge

Fred Pacquier said:
Well, André, if you really want *simple* (as in *simplistic*, *simple-
minded* or *can't-get-no-simpler* :) I have something that does just that
(english/french under wxPython)... ......
Then it's not too hard to add, say, a trans['de'] dict to l10n.py and
change the locale test above to use it. If automatic detection is not
adequate, then the GUI needs to provide a widget or dialog for the user
to set the desired language...

Thanks for the suggestion. Actually I will NOT use the automatic
detection - and I would encourage others to do the same. The reason
is as follows:

Suppose that a French user is fairly fluent in Spanish, and very
little in English. Her computer's locale indicates that French should
be the default. The application she chose is available both with a
Spanish and an English interface - but not a French one. The
automatic detection scheme will chose English whereas, as she had
known it was available, she would have preferred to use the Spanish
one. But that can't be known by a program using the locale test.

Anyway, I'll try something along the lines of what is in the wxPython
wiki (suggested by another reader). Thanks to all those who helped.

André
 
F

Fred Pacquier

(e-mail address removed) (Andr? Roberge) said :
Thanks for the suggestion. Actually I will NOT use the automatic
detection - and I would encourage others to do the same. The reason
is as follows:

Quite, those are the sorts of reasons I had in mind when making that
remark. As I said, this was just a quick hack to verify things worked as
intended, not an end-user-friendly product :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top