[I18n-sig] python gettext example?

Martin v. Loewis martin@v.loewis.de
Tue, 1 Jan 2002 22:29:30 +0100


> Are there an examples of Python programs using gettext. 

mailman does.

> I put this at the top of files:
> 
> from gettext import gettext as _
> from gettext import bindtextdomain, textdomain
> from os import sep
> from locale import setlocale, LC_ALL
> 
> LOCALE_PREFIX = "%susr" % (sep)
> LOCALE_DIR = "%s%sshare%slocale" % ( LOCALE_PREFIX, sep, sep )

If you install into the system locale dir (i.e. the one of the python
prefix), you don't need to bind the text domain; the default search
path should be fine.

> PACKAGE = "deodas"
> 
> setlocale( LC_ALL )

This has no effect. To set the locale, use

setlocale(LC_ALL, "")

OTOH, gettext.py will work even without a setlocale call.

> The source code looks like 
> module/__init__.py 
> module/etc...
> po/POTFILES.in
> po/es.po
> po/es.mo
[...]
> How do I run the program using a translation file?

You should install the files, into
/usr/share/locale/es/LC_MESSAGES/deodas.mo. Alternatively, you could 
install them anywhere else, e.g.

/tmp/LC_MESSAGES/deodas.mo

then you should set LOCALE_DIR to /tmp. If you want to use the locale
files right in their soure location, you should do

trans = gettext.GNUTranslation(open("po/es.mo"))
_ = trans.gettext

HTH,
Martin