blob: a295c0eb672bcc619cad79070dc3530468721c87 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`gettext` --- Multilingual internationalization services
3=============================================================
4
5.. module:: gettext
6 :synopsis: Multilingual internationalization services.
7.. moduleauthor:: Barry A. Warsaw <barry@zope.com>
8.. sectionauthor:: Barry A. Warsaw <barry@zope.com>
9
10
11The :mod:`gettext` module provides internationalization (I18N) and localization
12(L10N) services for your Python modules and applications. It supports both the
13GNU ``gettext`` message catalog API and a higher level, class-based API that may
14be more appropriate for Python files. The interface described below allows you
15to write your module and application messages in one natural language, and
16provide a catalog of translated messages for running under different natural
17languages.
18
19Some hints on localizing your Python modules and applications are also given.
20
21
22GNU :program:`gettext` API
23--------------------------
24
25The :mod:`gettext` module defines the following API, which is very similar to
26the GNU :program:`gettext` API. If you use this API you will affect the
27translation of your entire application globally. Often this is what you want if
28your application is monolingual, with the choice of language dependent on the
29locale of your user. If you are localizing a Python module, or if your
30application needs to switch languages on the fly, you probably want to use the
31class-based API instead.
32
33
34.. function:: bindtextdomain(domain[, localedir])
35
36 Bind the *domain* to the locale directory *localedir*. More concretely,
37 :mod:`gettext` will look for binary :file:`.mo` files for the given domain using
38 the path (on Unix): :file:`localedir/language/LC_MESSAGES/domain.mo`, where
39 *languages* is searched for in the environment variables :envvar:`LANGUAGE`,
40 :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and :envvar:`LANG` respectively.
41
42 If *localedir* is omitted or ``None``, then the current binding for *domain* is
43 returned. [#]_
44
45
46.. function:: bind_textdomain_codeset(domain[, codeset])
47
48 Bind the *domain* to *codeset*, changing the encoding of strings returned by the
49 :func:`gettext` family of functions. If *codeset* is omitted, then the current
50 binding is returned.
51
Georg Brandl116aa622007-08-15 14:28:22 +000052
53.. function:: textdomain([domain])
54
55 Change or query the current global domain. If *domain* is ``None``, then the
56 current global domain is returned, otherwise the global domain is set to
57 *domain*, which is returned.
58
59
60.. function:: gettext(message)
61
62 Return the localized translation of *message*, based on the current global
63 domain, language, and locale directory. This function is usually aliased as
64 :func:`_` in the local namespace (see examples below).
65
66
67.. function:: lgettext(message)
68
Georg Brandlbded4d32008-07-17 18:15:35 +000069 Equivalent to :func:`gettext`, but the translation is returned in the
70 preferred system encoding, if no other encoding was explicitly set with
Georg Brandl116aa622007-08-15 14:28:22 +000071 :func:`bind_textdomain_codeset`.
72
Georg Brandl116aa622007-08-15 14:28:22 +000073
74.. function:: dgettext(domain, message)
75
76 Like :func:`gettext`, but look the message up in the specified *domain*.
77
78
79.. function:: ldgettext(domain, message)
80
Georg Brandlbded4d32008-07-17 18:15:35 +000081 Equivalent to :func:`dgettext`, but the translation is returned in the
82 preferred system encoding, if no other encoding was explicitly set with
Georg Brandl116aa622007-08-15 14:28:22 +000083 :func:`bind_textdomain_codeset`.
84
Georg Brandl116aa622007-08-15 14:28:22 +000085
86.. function:: ngettext(singular, plural, n)
87
88 Like :func:`gettext`, but consider plural forms. If a translation is found,
89 apply the plural formula to *n*, and return the resulting message (some
90 languages have more than two plural forms). If no translation is found, return
91 *singular* if *n* is 1; return *plural* otherwise.
92
93 The Plural formula is taken from the catalog header. It is a C or Python
94 expression that has a free variable *n*; the expression evaluates to the index
95 of the plural in the catalog. See the GNU gettext documentation for the precise
96 syntax to be used in :file:`.po` files and the formulas for a variety of
97 languages.
98
Georg Brandl116aa622007-08-15 14:28:22 +000099
100.. function:: lngettext(singular, plural, n)
101
Georg Brandlbded4d32008-07-17 18:15:35 +0000102 Equivalent to :func:`ngettext`, but the translation is returned in the
103 preferred system encoding, if no other encoding was explicitly set with
Georg Brandl116aa622007-08-15 14:28:22 +0000104 :func:`bind_textdomain_codeset`.
105
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107.. function:: dngettext(domain, singular, plural, n)
108
109 Like :func:`ngettext`, but look the message up in the specified *domain*.
110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112.. function:: ldngettext(domain, singular, plural, n)
113
114 Equivalent to :func:`dngettext`, but the translation is returned in the
115 preferred system encoding, if no other encoding was explicitly set with
116 :func:`bind_textdomain_codeset`.
117
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but
120this was deemed not useful and so it is currently unimplemented.
121
122Here's an example of typical usage for this API::
123
124 import gettext
125 gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
126 gettext.textdomain('myapplication')
127 _ = gettext.gettext
128 # ...
Georg Brandl6911e3c2007-09-04 07:15:32 +0000129 print(_('This is a translatable string.'))
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131
132Class-based API
133---------------
134
135The class-based API of the :mod:`gettext` module gives you more flexibility and
136greater convenience than the GNU :program:`gettext` API. It is the recommended
137way of localizing your Python applications and modules. :mod:`gettext` defines
138a "translations" class which implements the parsing of GNU :file:`.mo` format
Georg Brandlf6945182008-02-01 11:56:49 +0000139files, and has methods for returning strings. Instances of this "translations"
140class can also install themselves in the built-in namespace as the function
141:func:`_`.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
144.. function:: find(domain[, localedir[, languages[, all]]])
145
146 This function implements the standard :file:`.mo` file search algorithm. It
147 takes a *domain*, identical to what :func:`textdomain` takes. Optional
148 *localedir* is as in :func:`bindtextdomain` Optional *languages* is a list of
149 strings, where each string is a language code.
150
151 If *localedir* is not given, then the default system locale directory is used.
152 [#]_ If *languages* is not given, then the following environment variables are
153 searched: :envvar:`LANGUAGE`, :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
154 :envvar:`LANG`. The first one returning a non-empty value is used for the
155 *languages* variable. The environment variables should contain a colon separated
156 list of languages, which will be split on the colon to produce the expected list
157 of language code strings.
158
159 :func:`find` then expands and normalizes the languages, and then iterates
160 through them, searching for an existing file built of these components:
161
162 :file:`localedir/language/LC_MESSAGES/domain.mo`
163
164 The first such file name that exists is returned by :func:`find`. If no such
165 file is found, then ``None`` is returned. If *all* is given, it returns a list
166 of all file names, in the order in which they appear in the languages list or
167 the environment variables.
168
169
170.. function:: translation(domain[, localedir[, languages[, class_[, fallback[, codeset]]]]])
171
Georg Brandlbded4d32008-07-17 18:15:35 +0000172 Return a :class:`Translations` instance based on the *domain*, *localedir*,
173 and *languages*, which are first passed to :func:`find` to get a list of the
Georg Brandl116aa622007-08-15 14:28:22 +0000174 associated :file:`.mo` file paths. Instances with identical :file:`.mo` file
Georg Brandlbded4d32008-07-17 18:15:35 +0000175 names are cached. The actual class instantiated is either *class_* if
176 provided, otherwise :class:`GNUTranslations`. The class's constructor must
177 take a single file object argument. If provided, *codeset* will change the
178 charset used to encode translated strings in the :meth:`lgettext` and
179 :meth:`lngettext` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181 If multiple files are found, later files are used as fallbacks for earlier ones.
182 To allow setting the fallback, :func:`copy.copy` is used to clone each
183 translation object from the cache; the actual instance data is still shared with
184 the cache.
185
186 If no :file:`.mo` file is found, this function raises :exc:`IOError` if
187 *fallback* is false (which is the default), and returns a
188 :class:`NullTranslations` instance if *fallback* is true.
189
Georg Brandl116aa622007-08-15 14:28:22 +0000190
Georg Brandlbded4d32008-07-17 18:15:35 +0000191.. function:: install(domain[, localedir[, codeset[, names]]]])
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193 This installs the function :func:`_` in Python's builtin namespace, based on
194 *domain*, *localedir*, and *codeset* which are passed to the function
Benjamin Peterson801844d2008-07-14 14:32:15 +0000195 :func:`translation`.
Georg Brandl116aa622007-08-15 14:28:22 +0000196
197 For the *names* parameter, please see the description of the translation
198 object's :meth:`install` method.
199
200 As seen below, you usually mark the strings in your application that are
201 candidates for translation, by wrapping them in a call to the :func:`_`
202 function, like this::
203
Georg Brandl6911e3c2007-09-04 07:15:32 +0000204 print(_('This string will be translated.'))
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206 For convenience, you want the :func:`_` function to be installed in Python's
207 builtin namespace, so it is easily accessible in all modules of your
208 application.
209
Georg Brandl116aa622007-08-15 14:28:22 +0000210
211The :class:`NullTranslations` class
212^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
213
214Translation classes are what actually implement the translation of original
215source file message strings to translated message strings. The base class used
216by all translation classes is :class:`NullTranslations`; this provides the basic
217interface you can use to write your own specialized translation classes. Here
218are the methods of :class:`NullTranslations`:
219
220
Benjamin Petersone41251e2008-04-25 01:59:09 +0000221.. class:: NullTranslations([fp])
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223 Takes an optional file object *fp*, which is ignored by the base class.
224 Initializes "protected" instance variables *_info* and *_charset* which are set
225 by derived classes, as well as *_fallback*, which is set through
226 :meth:`add_fallback`. It then calls ``self._parse(fp)`` if *fp* is not
227 ``None``.
228
Georg Brandlbded4d32008-07-17 18:15:35 +0000229 .. method:: _parse(fp)
Georg Brandl116aa622007-08-15 14:28:22 +0000230
Georg Brandlbded4d32008-07-17 18:15:35 +0000231 No-op'd in the base class, this method takes file object *fp*, and reads
232 the data from the file, initializing its message catalog. If you have an
233 unsupported message catalog file format, you should override this method
234 to parse your format.
Georg Brandl116aa622007-08-15 14:28:22 +0000235
236
Georg Brandlbded4d32008-07-17 18:15:35 +0000237 .. method:: add_fallback(fallback)
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Georg Brandlbded4d32008-07-17 18:15:35 +0000239 Add *fallback* as the fallback object for the current translation object.
240 A translation object should consult the fallback if it cannot provide a
241 translation for a given message.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243
Georg Brandlbded4d32008-07-17 18:15:35 +0000244 .. method:: gettext(message)
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Georg Brandlbded4d32008-07-17 18:15:35 +0000246 If a fallback has been set, forward :meth:`gettext` to the fallback.
247 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000248
249
Georg Brandlbded4d32008-07-17 18:15:35 +0000250 .. method:: lgettext(message)
Georg Brandl116aa622007-08-15 14:28:22 +0000251
Georg Brandlbded4d32008-07-17 18:15:35 +0000252 If a fallback has been set, forward :meth:`lgettext` to the fallback.
253 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000254
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Georg Brandlbded4d32008-07-17 18:15:35 +0000256 .. method:: ngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000257
Georg Brandlbded4d32008-07-17 18:15:35 +0000258 If a fallback has been set, forward :meth:`ngettext` to the fallback.
259 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
261
Georg Brandlbded4d32008-07-17 18:15:35 +0000262 .. method:: lngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000263
Georg Brandlbded4d32008-07-17 18:15:35 +0000264 If a fallback has been set, forward :meth:`ngettext` to the fallback.
265 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Georg Brandl116aa622007-08-15 14:28:22 +0000267
Georg Brandlbded4d32008-07-17 18:15:35 +0000268 .. method:: info()
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Georg Brandlbded4d32008-07-17 18:15:35 +0000270 Return the "protected" :attr:`_info` variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Georg Brandl116aa622007-08-15 14:28:22 +0000272
Georg Brandlbded4d32008-07-17 18:15:35 +0000273 .. method:: charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Georg Brandlbded4d32008-07-17 18:15:35 +0000275 Return the "protected" :attr:`_charset` variable, which is the encoding of
276 the message catalog file.
Georg Brandl116aa622007-08-15 14:28:22 +0000277
Georg Brandl116aa622007-08-15 14:28:22 +0000278
Georg Brandlbded4d32008-07-17 18:15:35 +0000279 .. method:: output_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000280
Georg Brandlbded4d32008-07-17 18:15:35 +0000281 Return the "protected" :attr:`_output_charset` variable, which defines the
282 encoding used to return translated messages in :meth:`lgettext` and
283 :meth:`lngettext`.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285
Georg Brandlbded4d32008-07-17 18:15:35 +0000286 .. method:: set_output_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Georg Brandlbded4d32008-07-17 18:15:35 +0000288 Change the "protected" :attr:`_output_charset` variable, which defines the
289 encoding used to return translated messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Benjamin Peterson801844d2008-07-14 14:32:15 +0000291
Georg Brandlbded4d32008-07-17 18:15:35 +0000292 .. method:: install([names])
Benjamin Peterson801844d2008-07-14 14:32:15 +0000293
Georg Brandlbded4d32008-07-17 18:15:35 +0000294 This method installs :meth:`self.gettext` into the built-in namespace,
295 binding it to ``_``.
Benjamin Peterson801844d2008-07-14 14:32:15 +0000296
Georg Brandlbded4d32008-07-17 18:15:35 +0000297 If the *names* parameter is given, it must be a sequence containing the
298 names of functions you want to install in the builtin namespace in
299 addition to :func:`_`. Supported names are ``'gettext'`` (bound to
300 :meth:`self.gettext`), ``'ngettext'`` (bound to :meth:`self.ngettext`),
301 ``'lgettext'`` and ``'lngettext'``.
Benjamin Peterson801844d2008-07-14 14:32:15 +0000302
Georg Brandlbded4d32008-07-17 18:15:35 +0000303 Note that this is only one way, albeit the most convenient way, to make
304 the :func:`_` function available to your application. Because it affects
305 the entire application globally, and specifically the built-in namespace,
306 localized modules should never install :func:`_`. Instead, they should use
307 this code to make :func:`_` available to their module::
Benjamin Peterson801844d2008-07-14 14:32:15 +0000308
Georg Brandlbded4d32008-07-17 18:15:35 +0000309 import gettext
310 t = gettext.translation('mymodule', ...)
311 _ = t.gettext
Benjamin Peterson801844d2008-07-14 14:32:15 +0000312
Georg Brandlbded4d32008-07-17 18:15:35 +0000313 This puts :func:`_` only in the module's global namespace and so only
314 affects calls within this module.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Georg Brandl116aa622007-08-15 14:28:22 +0000316
317The :class:`GNUTranslations` class
318^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
319
320The :mod:`gettext` module provides one additional class derived from
321:class:`NullTranslations`: :class:`GNUTranslations`. This class overrides
322:meth:`_parse` to enable reading GNU :program:`gettext` format :file:`.mo` files
Benjamin Peterson801844d2008-07-14 14:32:15 +0000323in both big-endian and little-endian format.
Georg Brandl116aa622007-08-15 14:28:22 +0000324
325:class:`GNUTranslations` parses optional meta-data out of the translation
326catalog. It is convention with GNU :program:`gettext` to include meta-data as
327the translation for the empty string. This meta-data is in :rfc:`822`\ -style
328``key: value`` pairs, and should contain the ``Project-Id-Version`` key. If the
329key ``Content-Type`` is found, then the ``charset`` property is used to
330initialize the "protected" :attr:`_charset` instance variable, defaulting to
331``None`` if not found. If the charset encoding is specified, then all message
332ids and message strings read from the catalog are converted to Unicode using
Georg Brandlbded4d32008-07-17 18:15:35 +0000333this encoding, else ASCII encoding is assumed.
334
335Since message ids are read as Unicode strings too, all :meth:`*gettext` methods
336will assume message ids as Unicode strings, not byte strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338The entire set of key/value pairs are placed into a dictionary and set as the
339"protected" :attr:`_info` instance variable.
340
341If the :file:`.mo` file's magic number is invalid, or if other problems occur
342while reading the file, instantiating a :class:`GNUTranslations` class can raise
343:exc:`IOError`.
344
345The following methods are overridden from the base class implementation:
346
347
348.. method:: GNUTranslations.gettext(message)
349
350 Look up the *message* id in the catalog and return the corresponding message
Georg Brandlbded4d32008-07-17 18:15:35 +0000351 string, as a Unicode string. If there is no entry in the catalog for the
352 *message* id, and a fallback has been set, the look up is forwarded to the
353 fallback's :meth:`gettext` method. Otherwise, the *message* id is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000354
355
356.. method:: GNUTranslations.lgettext(message)
357
Georg Brandlbded4d32008-07-17 18:15:35 +0000358 Equivalent to :meth:`gettext`, but the translation is returned as a
359 bytestring encoded in the selected output charset, or in the preferred system
360 encoding if no encoding was explicitly set with :meth:`set_output_charset`.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Georg Brandl116aa622007-08-15 14:28:22 +0000363.. method:: GNUTranslations.ngettext(singular, plural, n)
364
365 Do a plural-forms lookup of a message id. *singular* is used as the message id
366 for purposes of lookup in the catalog, while *n* is used to determine which
Georg Brandlbded4d32008-07-17 18:15:35 +0000367 plural form to use. The returned message string is a Unicode string.
Georg Brandl116aa622007-08-15 14:28:22 +0000368
369 If the message id is not found in the catalog, and a fallback is specified, the
370 request is forwarded to the fallback's :meth:`ngettext` method. Otherwise, when
371 *n* is 1 *singular* is returned, and *plural* is returned in all other cases.
Georg Brandl48310cd2009-01-03 21:18:54 +0000372
Benjamin Peterson801844d2008-07-14 14:32:15 +0000373 Here is an example::
374
375 n = len(os.listdir('.'))
376 cat = GNUTranslations(somefile)
377 message = cat.ngettext(
378 'There is %(num)d file in this directory',
379 'There are %(num)d files in this directory',
380 n) % {'num': n}
Georg Brandl116aa622007-08-15 14:28:22 +0000381
Georg Brandl116aa622007-08-15 14:28:22 +0000382
383.. method:: GNUTranslations.lngettext(singular, plural, n)
384
Georg Brandlbded4d32008-07-17 18:15:35 +0000385 Equivalent to :meth:`gettext`, but the translation is returned as a
386 bytestring encoded in the selected output charset, or in the preferred system
387 encoding if no encoding was explicitly set with :meth:`set_output_charset`.
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Georg Brandl116aa622007-08-15 14:28:22 +0000389
Georg Brandl116aa622007-08-15 14:28:22 +0000390Solaris message catalog support
391^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
392
393The Solaris operating system defines its own binary :file:`.mo` file format, but
394since no documentation can be found on this format, it is not supported at this
395time.
396
397
398The Catalog constructor
399^^^^^^^^^^^^^^^^^^^^^^^
400
401.. index:: single: GNOME
402
403GNOME uses a version of the :mod:`gettext` module by James Henstridge, but this
404version has a slightly different API. Its documented usage was::
405
406 import gettext
407 cat = gettext.Catalog(domain, localedir)
408 _ = cat.gettext
Georg Brandl6911e3c2007-09-04 07:15:32 +0000409 print(_('hello world'))
Georg Brandl116aa622007-08-15 14:28:22 +0000410
411For compatibility with this older module, the function :func:`Catalog` is an
412alias for the :func:`translation` function described above.
413
414One difference between this module and Henstridge's: his catalog objects
415supported access through a mapping API, but this appears to be unused and so is
416not currently supported.
417
418
419Internationalizing your programs and modules
420--------------------------------------------
421
422Internationalization (I18N) refers to the operation by which a program is made
423aware of multiple languages. Localization (L10N) refers to the adaptation of
424your program, once internationalized, to the local language and cultural habits.
425In order to provide multilingual messages for your Python programs, you need to
426take the following steps:
427
428#. prepare your program or module by specially marking translatable strings
429
430#. run a suite of tools over your marked files to generate raw messages catalogs
431
432#. create language specific translations of the message catalogs
433
434#. use the :mod:`gettext` module so that message strings are properly translated
435
436In order to prepare your code for I18N, you need to look at all the strings in
437your files. Any string that needs to be translated should be marked by wrapping
438it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
439
440 filename = 'mylog.txt'
441 message = _('writing a log message')
442 fp = open(filename, 'w')
443 fp.write(message)
444 fp.close()
445
446In this example, the string ``'writing a log message'`` is marked as a candidate
447for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not.
448
449The Python distribution comes with two tools which help you generate the message
450catalogs once you've prepared your source code. These may or may not be
451available from a binary distribution, but they can be found in a source
452distribution, in the :file:`Tools/i18n` directory.
453
454The :program:`pygettext` [#]_ program scans all your Python source code looking
455for the strings you previously marked as translatable. It is similar to the GNU
456:program:`gettext` program except that it understands all the intricacies of
457Python source code, but knows nothing about C or C++ source code. You don't
458need GNU ``gettext`` unless you're also going to be translating C code (such as
459C extension modules).
460
461:program:`pygettext` generates textual Uniforum-style human readable message
462catalog :file:`.pot` files, essentially structured human readable files which
463contain every marked string in the source code, along with a placeholder for the
464translation strings. :program:`pygettext` is a command line script that supports
465a similar command line interface as :program:`xgettext`; for details on its use,
466run::
467
468 pygettext.py --help
469
470Copies of these :file:`.pot` files are then handed over to the individual human
471translators who write language-specific versions for every supported natural
472language. They send you back the filled in language-specific versions as a
473:file:`.po` file. Using the :program:`msgfmt.py` [#]_ program (in the
474:file:`Tools/i18n` directory), you take the :file:`.po` files from your
475translators and generate the machine-readable :file:`.mo` binary catalog files.
476The :file:`.mo` files are what the :mod:`gettext` module uses for the actual
477translation processing during run-time.
478
479How you use the :mod:`gettext` module in your code depends on whether you are
480internationalizing a single module or your entire application. The next two
481sections will discuss each case.
482
483
484Localizing your module
485^^^^^^^^^^^^^^^^^^^^^^
486
487If you are localizing your module, you must take care not to make global
488changes, e.g. to the built-in namespace. You should not use the GNU ``gettext``
489API but instead the class-based API.
490
491Let's say your module is called "spam" and the module's various natural language
492translation :file:`.mo` files reside in :file:`/usr/share/locale` in GNU
493:program:`gettext` format. Here's what you would put at the top of your
494module::
495
496 import gettext
497 t = gettext.translation('spam', '/usr/share/locale')
498 _ = t.lgettext
499
Georg Brandl116aa622007-08-15 14:28:22 +0000500
501Localizing your application
502^^^^^^^^^^^^^^^^^^^^^^^^^^^
503
504If you are localizing your application, you can install the :func:`_` function
505globally into the built-in namespace, usually in the main driver file of your
506application. This will let all your application-specific files just use
507``_('...')`` without having to explicitly install it in each file.
508
509In the simple case then, you need only add the following bit of code to the main
510driver file of your application::
511
512 import gettext
513 gettext.install('myapplication')
514
Benjamin Peterson801844d2008-07-14 14:32:15 +0000515If you need to set the locale directory, you can pass these into the
516:func:`install` function::
Georg Brandl116aa622007-08-15 14:28:22 +0000517
518 import gettext
Benjamin Peterson801844d2008-07-14 14:32:15 +0000519 gettext.install('myapplication', '/usr/share/locale')
Georg Brandl116aa622007-08-15 14:28:22 +0000520
521
522Changing languages on the fly
523^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
524
525If your program needs to support many languages at the same time, you may want
526to create multiple translation instances and then switch between them
527explicitly, like so::
528
529 import gettext
530
531 lang1 = gettext.translation('myapplication', languages=['en'])
532 lang2 = gettext.translation('myapplication', languages=['fr'])
533 lang3 = gettext.translation('myapplication', languages=['de'])
534
535 # start by using language1
536 lang1.install()
537
538 # ... time goes by, user selects language 2
539 lang2.install()
540
541 # ... more time goes by, user selects language 3
542 lang3.install()
543
544
545Deferred translations
546^^^^^^^^^^^^^^^^^^^^^
547
548In most coding situations, strings are translated where they are coded.
549Occasionally however, you need to mark strings for translation, but defer actual
550translation until later. A classic example is::
551
552 animals = ['mollusk',
553 'albatross',
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000554 'rat',
555 'penguin',
556 'python', ]
Georg Brandl116aa622007-08-15 14:28:22 +0000557 # ...
558 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000559 print(a)
Georg Brandl116aa622007-08-15 14:28:22 +0000560
561Here, you want to mark the strings in the ``animals`` list as being
562translatable, but you don't actually want to translate them until they are
563printed.
564
565Here is one way you can handle this situation::
566
567 def _(message): return message
568
569 animals = [_('mollusk'),
570 _('albatross'),
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000571 _('rat'),
572 _('penguin'),
573 _('python'), ]
Georg Brandl116aa622007-08-15 14:28:22 +0000574
575 del _
576
577 # ...
578 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000579 print(_(a))
Georg Brandl116aa622007-08-15 14:28:22 +0000580
581This works because the dummy definition of :func:`_` simply returns the string
582unchanged. And this dummy definition will temporarily override any definition
583of :func:`_` in the built-in namespace (until the :keyword:`del` command). Take
584care, though if you have a previous definition of :func:`_` in the local
585namespace.
586
587Note that the second use of :func:`_` will not identify "a" as being
588translatable to the :program:`pygettext` program, since it is not a string.
589
590Another way to handle this is with the following example::
591
592 def N_(message): return message
593
594 animals = [N_('mollusk'),
595 N_('albatross'),
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000596 N_('rat'),
597 N_('penguin'),
598 N_('python'), ]
Georg Brandl116aa622007-08-15 14:28:22 +0000599
600 # ...
601 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000602 print(_(a))
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604In this case, you are marking translatable strings with the function :func:`N_`,
605[#]_ which won't conflict with any definition of :func:`_`. However, you will
606need to teach your message extraction program to look for translatable strings
607marked with :func:`N_`. :program:`pygettext` and :program:`xpot` both support
608this through the use of command line switches.
609
610
Georg Brandl116aa622007-08-15 14:28:22 +0000611Acknowledgements
612----------------
613
614The following people contributed code, feedback, design suggestions, previous
615implementations, and valuable experience to the creation of this module:
616
617* Peter Funk
618
619* James Henstridge
620
621* Juan David Ibáñez Palomar
622
623* Marc-André Lemburg
624
625* Martin von Löwis
626
627* François Pinard
628
629* Barry Warsaw
630
631* Gustavo Niemeyer
632
633.. rubric:: Footnotes
634
635.. [#] The default locale directory is system dependent; for example, on RedHat Linux
636 it is :file:`/usr/share/locale`, but on Solaris it is :file:`/usr/lib/locale`.
637 The :mod:`gettext` module does not try to support these system dependent
638 defaults; instead its default is :file:`sys.prefix/share/locale`. For this
639 reason, it is always best to call :func:`bindtextdomain` with an explicit
640 absolute path at the start of your application.
641
642.. [#] See the footnote for :func:`bindtextdomain` above.
643
644.. [#] François Pinard has written a program called :program:`xpot` which does a
645 similar job. It is available as part of his :program:`po-utils` package at http
646 ://po-utils.progiciels-bpi.ca/.
647
648.. [#] :program:`msgfmt.py` is binary compatible with GNU :program:`msgfmt` except that
649 it provides a simpler, all-Python implementation. With this and
650 :program:`pygettext.py`, you generally won't need to install the GNU
651 :program:`gettext` package to internationalize your Python applications.
652
653.. [#] The choice of :func:`N_` here is totally arbitrary; it could have just as easily
654 been :func:`MarkThisStringForTranslation`.
655