Re: python gettext example?

"Martin v. Loewis" <[email protected]>
Newsgroups gmane.comp.python.internationalization
Message-ID <[email protected]>
> 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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.