views:

367

answers:

3

I have a application written in wxPython which I want to make multilingual. Our options are

  1. using gettext http://docs.python.org/library/gettext.html
  2. seprating out all UI text to a messages.py file, and using it to translate text

I am very much inclined towards 2nd and I see no benefit in going gettext way, using 2nd way i can have all my messages at one place not in code, so If i need to change a message, code need not be changed, in case of gettext i may have confusing msg-constants as I will be just wrapping the orginal msg instead of converting it to a constant in messages.py

basically instead of

wx.MessageBox(_("Hi stackoverflow!"))

I think

wx.MessageBox(messages.GREET_SO)

is better, so is there any advantage in gettext way and disadvantage 2nd way? and is there a 3rd way?

edit: also gettext languages files seems to be too tied to code, and what happens if i want two messages same in english but different in french e.g. suppose french has more subtle translation for different scnerarios for english one is ok

experience: I have already gone 2nd way, and i must say every application should try to extract UI text from code, it gives a chance to refactor, see where UI is creeping into model and where UI text can be improved, gettext in comparison is mechanic, doesn't gives any input for coder, and i think would be more difficult to maintain.

and while creating a name for text e.g. PRINT_PROGRESS_MSG, gives a chance to see that at many places, same msg is being used slightly differently and can be merged into a single name, which later on will help when i need to change msg only once.

Conclusion: I am still not sure of any advantage to use gettext and am using my own messages file. but I have selected the answer which at least explained few points why gettext can be beneficial. The final solution IMO is which takes the best from both ways i.e my own message identifier wrapped by gettext e.g

wx.MessageBox(_("GREET_SO"))
A: 

For the web (this is PHP but the idea's the same), I always create multiple language files in a specific directory. en.php, fr.php, et cetera. Those files contain definitions of all output text, in the given language. The user preference for language determines which of those files get included, thus, which language the output appears in. For example...

in en.php: TEXT_I_AM = "I am"

in fr.php: TEXT_I_AM = "Je suis"

Chris
+1  A: 

There are some advantages of gettext:

  1. One of the biggest advantages is: when using poedit to do the translations you can benefit from the translation database. Basically poedit ca scan your harddisk and find already translated files and will make suggestions when you translate your file.
  2. When you give the code to other people to translate they might already know the gettext way of translating, while you have to explain them your way of translating.
  3. You have the text in the context of the code, so it should be easier to translate, when you see the code around the translation
  4. Consider text like: print _('%d files of %d files selected') % (num, numTotal) and even more complicated situations. Here it really helps having the code around ...
RSabet
'1' i think doesn't matter in my case i will never rely on automatic translation, '2' is alsonot big worry as my way would be easier, just translates given strings , instead of cryptic gettext files, i don't understand '3' how you see code around translation, isn;t it reverse?4 seems to be valid case
Anurag Uniyal
well (4) is a special often used example for (3). Concerning (1): have you tried automatic translation of poedit - it really helps.
RSabet
Yeah, I forgot the automatic translation feature - it's really handy!
Alix Axel
@Anurag: gettext files are not cryptic, you have the PO (portable object file) which is pretty much a standard text file and MO (machine object) which is "compiled" for performance. In a way similar to .py and .pyc files.
Alix Axel
+1  A: 

Gettext is the way to go, in your example you can also use gettext to "avoid storing the translations in the code":

wx.MessageBox(messages.GREET_SO)

might be the same with gettext as:

wx.MessageBox(_("GREET_SO")) or wx.MessageBox(_("messages.GREET_SO"))

Gettext is pretty much the standard for multilingual applications, and I'm pretty sure you'll benefit from using it in the future. Example, you can use Poedit (or other similar app) to assign translations to your collaborators or contributors and later on flag one or several messages as not properly translated. Also if there are missing / extra entries poedit will warn you. Don't fool yourself, gettext is the only proven reliable way to maintain translations.

Alix Axel
yes this could be a nice way , merging the both , at least solves my main problem with gettext that messages is same as message identifier and if needed I can replace _ with my function just for fun :)
Anurag Uniyal