views:

46

answers:

1

This particular piece of code works very well on Linux, but not on Windows:

locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain('exposong', LOCALE_PATH)
gettext.textdomain('exposong')

Code from here

Even if i specify the locale in locale.setlocale (I tried different formats) it doesn't work. One problem might be that the locale is not set in the environment variables (but I use a German Windows version; tested on XP and Vista). If I do "Set Lang=de_DE" on the command line, everything works as expected.

Any ideas?

+2  A: 

Standard gettext module in Python does not use startdard language settings from Windows settings, but instead relies on presence one of the environment variables: LANGUAGE, LC_MESSAGES, LC_ALL or LANG. (I'd say this is example of slack porting of Unix/Linux library to Windows.)

The environment variables mentioned above do not present on typical Windows machine, because OS Windows and native applications use settings from registry instead. So you need to get the language settings from Windows registry and put them into process environment.

You can use my helper module for this: https://launchpad.net/gettext-py-windows

This helper obtains language settings from Windows settings and set LANG variable for current process, so gettext can use this settings.

So, if the application in question is not yours you can do the following. Install my gettext helper as usual with python setup.py install. Then add these lines before locale.setlocale(locale.LC_ALL, ''):

import gettext_windows
gettext_windows.setup_env()

That's all.

bialix
Great work! I will try to integrate this into the project ASAP (the question is from my other developer).
bradlis7
@bradlis7 you are welcome :-) The license of my gettext_windows helper is very permissive (MIT) so you can just merge it to your codebase and mention it in your readme. Thus user won't be required to install one more separate library dependency.
bialix