Thursday, July 21, 2011

Use python module gettext in windows

There are already a lot of great tutorials about how to use gettext to internationalize your python application or module under the Unix environment. 


For example: http://www.doughellmann.com/PyMOTW/gettext/.

However, if you are following these tutorials on Windows, gettext will be failed to load the .mo file even you correctly specified the 'localedir' parameter. 


For example:
import gettextt = gettext.translation('cn', 'C:\locale', 'en_US', fallback=True)
will fail to load "cn.mo" from "C:\local\en_US\LC_MESSAGES".[1]


The reason is that 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_ALLor LANG.


Therefore, you must add one of above environment variables into your windows settings. Just add:
LANG=en_US
into your system global environment variable. Then gettext will be able to load cn.mo file from "C:\local\en_US\LC_MESSAGES".

1 comment: