i18n in a library

Martin v. Löwis martin.vonloewis at hpi.uni-potsdam.de
Wed Apr 23 11:07:13 CEST 2003

> I think what you want is gettext.translation, which offers you
> finer-grained control than gettext.install.  I.e., within the
> modules of library L, you could use
>     import gettext
>     _ = gettext.translation('L').ugettext
> so that in those modules function _ will look things up in domain
> 'L', rather than in whatever domain is "installed" globally.

Alternatively, gettext.dgettext is the Python version of the
equivalent C API, so you could also do

def _(msg):
   return gettext.dgettext('L', msg)

This has the added advantage of returning the string itself if
no catalog can be found (instead of giving IOError as
gettext.translation does); with Python 2.x (for some x),
you can pass the fallback=True argument to translation
to achieve the same advantage.

The d in dgettext is for 'domain'; with 'L' being a textual domain.
The notion of a textual domain is somewhat independent from the
notion of a software package; different packages may share the same
domain, yet a single package may consist of several domains.


More information about the Python-list mailing list