gettext, Python, and Windows: a simple demo

Last time, I discussed some of the issues about localizing a Python app in Windows. This time, I’m providing a simple demo app which shows how localization can be accomplished.

To avoid an overly verbose explanation, I’ve set up a GitHub repo with the code, a sample .po file, plus some scripts for easily creating .po/.mo files in the correct hierarchies. (Consider its contents as public domain.)

Basically: MinGW/Msys provide the necessary utilities for extracting strings and compiling .mo files. If you’re in a non-English environment, things may act odd if LANG is not set appropriately, but other than that the tools work fine. So, it’s just a matter of a few minor tweaks, and maybe some scripts or Makefiles to automate things.

The demo app in the above repo contains a minimum of magic: it will check if gettext-compatible environment settings are set (LANG, LC_MESSAGES, etc.), and if not, will create a LANG value based on locale.getdefaultlocale().

Anyway, test output on my system (Japanese Windows with English set via Regional Settings dialog) looks like follows:

LANG/LC_* unspecified:

$ ./simple_test.py
Current locale:
LANGUAGE None
LC_ALL None
LC_MESSAGES None
LANG None

Adjusted locale:
LANGUAGE None
LC_ALL None
LC_MESSAGES None
LANG en_US

Hello world!

LANG=ja:

$ LANG=ja ./simple_test.py
Current locale:
LANGUAGE None
LC_ALL None
LC_MESSAGES None
LANG ja

こんにちは、世界!

And a final note: last time I made comments about encodings, but it seems that LANG values including encodings (“ja_JP.euc-jp”) don’t have any effect – at least in this demo and on Windows. Just the language/country portion is sufficient.

Author: Paul Goins

Paul Goins has been writing software professionally since 2005, minus a two-and-a-half year break to teach English in Japan. He has been working primarily in Python since 2008, is proficient in many other languages, and most recently is turning his focus towards Erlang. Paul has worked both in Japan and the U.S., and presently resides in the Greater Portland area of Oregon state in the U.S.

Leave a Reply

Your email address will not be published. Required fields are marked *