blob: 3a87bf591776c45ee1471b7f518a23b4ca45ebd8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`gettext` --- Multilingual internationalization services
2=============================================================
3
4.. module:: gettext
5 :synopsis: Multilingual internationalization services.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Andrew Kuchling587e9702013-11-12 10:02:35 -05007.. moduleauthor:: Barry A. Warsaw <barry@python.org>
8.. sectionauthor:: Barry A. Warsaw <barry@python.org>
Georg Brandl116aa622007-08-15 14:28:22 +00009
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/gettext.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
14The :mod:`gettext` module provides internationalization (I18N) and localization
15(L10N) services for your Python modules and applications. It supports both the
16GNU ``gettext`` message catalog API and a higher level, class-based API that may
17be more appropriate for Python files. The interface described below allows you
18to write your module and application messages in one natural language, and
19provide a catalog of translated messages for running under different natural
20languages.
21
22Some hints on localizing your Python modules and applications are also given.
23
24
25GNU :program:`gettext` API
26--------------------------
27
28The :mod:`gettext` module defines the following API, which is very similar to
29the GNU :program:`gettext` API. If you use this API you will affect the
30translation of your entire application globally. Often this is what you want if
31your application is monolingual, with the choice of language dependent on the
32locale of your user. If you are localizing a Python module, or if your
33application needs to switch languages on the fly, you probably want to use the
34class-based API instead.
35
36
Georg Brandl036490d2009-05-17 13:00:36 +000037.. function:: bindtextdomain(domain, localedir=None)
Georg Brandl116aa622007-08-15 14:28:22 +000038
39 Bind the *domain* to the locale directory *localedir*. More concretely,
40 :mod:`gettext` will look for binary :file:`.mo` files for the given domain using
41 the path (on Unix): :file:`localedir/language/LC_MESSAGES/domain.mo`, where
42 *languages* is searched for in the environment variables :envvar:`LANGUAGE`,
43 :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and :envvar:`LANG` respectively.
44
45 If *localedir* is omitted or ``None``, then the current binding for *domain* is
46 returned. [#]_
47
48
Georg Brandl036490d2009-05-17 13:00:36 +000049.. function:: bind_textdomain_codeset(domain, codeset=None)
Georg Brandl116aa622007-08-15 14:28:22 +000050
51 Bind the *domain* to *codeset*, changing the encoding of strings returned by the
52 :func:`gettext` family of functions. If *codeset* is omitted, then the current
53 binding is returned.
54
Georg Brandl116aa622007-08-15 14:28:22 +000055
Georg Brandl036490d2009-05-17 13:00:36 +000056.. function:: textdomain(domain=None)
Georg Brandl116aa622007-08-15 14:28:22 +000057
58 Change or query the current global domain. If *domain* is ``None``, then the
59 current global domain is returned, otherwise the global domain is set to
60 *domain*, which is returned.
61
62
63.. function:: gettext(message)
64
65 Return the localized translation of *message*, based on the current global
66 domain, language, and locale directory. This function is usually aliased as
67 :func:`_` in the local namespace (see examples below).
68
69
70.. function:: lgettext(message)
71
Georg Brandlbded4d32008-07-17 18:15:35 +000072 Equivalent to :func:`gettext`, but the translation is returned in the
73 preferred system encoding, if no other encoding was explicitly set with
Georg Brandl116aa622007-08-15 14:28:22 +000074 :func:`bind_textdomain_codeset`.
75
Georg Brandl116aa622007-08-15 14:28:22 +000076
77.. function:: dgettext(domain, message)
78
79 Like :func:`gettext`, but look the message up in the specified *domain*.
80
81
82.. function:: ldgettext(domain, message)
83
Georg Brandlbded4d32008-07-17 18:15:35 +000084 Equivalent to :func:`dgettext`, but the translation is returned in the
85 preferred system encoding, if no other encoding was explicitly set with
Georg Brandl116aa622007-08-15 14:28:22 +000086 :func:`bind_textdomain_codeset`.
87
Georg Brandl116aa622007-08-15 14:28:22 +000088
89.. function:: ngettext(singular, plural, n)
90
91 Like :func:`gettext`, but consider plural forms. If a translation is found,
92 apply the plural formula to *n*, and return the resulting message (some
93 languages have more than two plural forms). If no translation is found, return
94 *singular* if *n* is 1; return *plural* otherwise.
95
96 The Plural formula is taken from the catalog header. It is a C or Python
97 expression that has a free variable *n*; the expression evaluates to the index
Andrew Kuchling30c5ad22013-11-19 11:05:20 -050098 of the plural in the catalog. See
99 `the GNU gettext documentation <https://www.gnu.org/software/gettext/manual/gettext.html>`__
100 for the precise syntax to be used in :file:`.po` files and the
101 formulas for a variety of languages.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104.. function:: lngettext(singular, plural, n)
105
Georg Brandlbded4d32008-07-17 18:15:35 +0000106 Equivalent to :func:`ngettext`, but the translation is returned in the
107 preferred system encoding, if no other encoding was explicitly set with
Georg Brandl116aa622007-08-15 14:28:22 +0000108 :func:`bind_textdomain_codeset`.
109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111.. function:: dngettext(domain, singular, plural, n)
112
113 Like :func:`ngettext`, but look the message up in the specified *domain*.
114
Georg Brandl116aa622007-08-15 14:28:22 +0000115
116.. function:: ldngettext(domain, singular, plural, n)
117
118 Equivalent to :func:`dngettext`, but the translation is returned in the
119 preferred system encoding, if no other encoding was explicitly set with
120 :func:`bind_textdomain_codeset`.
121
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but
124this was deemed not useful and so it is currently unimplemented.
125
126Here's an example of typical usage for this API::
127
128 import gettext
129 gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
130 gettext.textdomain('myapplication')
131 _ = gettext.gettext
132 # ...
Georg Brandl6911e3c2007-09-04 07:15:32 +0000133 print(_('This is a translatable string.'))
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
136Class-based API
137---------------
138
139The class-based API of the :mod:`gettext` module gives you more flexibility and
140greater convenience than the GNU :program:`gettext` API. It is the recommended
141way of localizing your Python applications and modules. :mod:`gettext` defines
142a "translations" class which implements the parsing of GNU :file:`.mo` format
Georg Brandlf6945182008-02-01 11:56:49 +0000143files, and has methods for returning strings. Instances of this "translations"
144class can also install themselves in the built-in namespace as the function
145:func:`_`.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147
Georg Brandl036490d2009-05-17 13:00:36 +0000148.. function:: find(domain, localedir=None, languages=None, all=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000149
150 This function implements the standard :file:`.mo` file search algorithm. It
151 takes a *domain*, identical to what :func:`textdomain` takes. Optional
152 *localedir* is as in :func:`bindtextdomain` Optional *languages* is a list of
153 strings, where each string is a language code.
154
155 If *localedir* is not given, then the default system locale directory is used.
156 [#]_ If *languages* is not given, then the following environment variables are
157 searched: :envvar:`LANGUAGE`, :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
158 :envvar:`LANG`. The first one returning a non-empty value is used for the
159 *languages* variable. The environment variables should contain a colon separated
160 list of languages, which will be split on the colon to produce the expected list
161 of language code strings.
162
163 :func:`find` then expands and normalizes the languages, and then iterates
164 through them, searching for an existing file built of these components:
165
Georg Brandl036490d2009-05-17 13:00:36 +0000166 :file:`{localedir}/{language}/LC_MESSAGES/{domain}.mo`
Georg Brandl116aa622007-08-15 14:28:22 +0000167
168 The first such file name that exists is returned by :func:`find`. If no such
169 file is found, then ``None`` is returned. If *all* is given, it returns a list
170 of all file names, in the order in which they appear in the languages list or
171 the environment variables.
172
173
Georg Brandl036490d2009-05-17 13:00:36 +0000174.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000175
Georg Brandlbded4d32008-07-17 18:15:35 +0000176 Return a :class:`Translations` instance based on the *domain*, *localedir*,
177 and *languages*, which are first passed to :func:`find` to get a list of the
Georg Brandl116aa622007-08-15 14:28:22 +0000178 associated :file:`.mo` file paths. Instances with identical :file:`.mo` file
Georg Brandlbded4d32008-07-17 18:15:35 +0000179 names are cached. The actual class instantiated is either *class_* if
180 provided, otherwise :class:`GNUTranslations`. The class's constructor must
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000181 take a single :term:`file object` argument. If provided, *codeset* will change
182 the charset used to encode translated strings in the :meth:`lgettext` and
Georg Brandlbded4d32008-07-17 18:15:35 +0000183 :meth:`lngettext` methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000184
185 If multiple files are found, later files are used as fallbacks for earlier ones.
186 To allow setting the fallback, :func:`copy.copy` is used to clone each
187 translation object from the cache; the actual instance data is still shared with
188 the cache.
189
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200190 If no :file:`.mo` file is found, this function raises :exc:`OSError` if
Georg Brandl116aa622007-08-15 14:28:22 +0000191 *fallback* is false (which is the default), and returns a
192 :class:`NullTranslations` instance if *fallback* is true.
193
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200194 .. versionchanged:: 3.3
195 :exc:`IOError` used to be raised instead of :exc:`OSError`.
196
Georg Brandl116aa622007-08-15 14:28:22 +0000197
Georg Brandl036490d2009-05-17 13:00:36 +0000198.. function:: install(domain, localedir=None, codeset=None, names=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Georg Brandl22b34312009-07-26 14:54:51 +0000200 This installs the function :func:`_` in Python's builtins namespace, based on
Georg Brandl116aa622007-08-15 14:28:22 +0000201 *domain*, *localedir*, and *codeset* which are passed to the function
Benjamin Peterson801844d2008-07-14 14:32:15 +0000202 :func:`translation`.
Georg Brandl116aa622007-08-15 14:28:22 +0000203
204 For the *names* parameter, please see the description of the translation
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000205 object's :meth:`~NullTranslations.install` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207 As seen below, you usually mark the strings in your application that are
208 candidates for translation, by wrapping them in a call to the :func:`_`
209 function, like this::
210
Georg Brandl6911e3c2007-09-04 07:15:32 +0000211 print(_('This string will be translated.'))
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213 For convenience, you want the :func:`_` function to be installed in Python's
Georg Brandl22b34312009-07-26 14:54:51 +0000214 builtins namespace, so it is easily accessible in all modules of your
Georg Brandl116aa622007-08-15 14:28:22 +0000215 application.
216
Georg Brandl116aa622007-08-15 14:28:22 +0000217
218The :class:`NullTranslations` class
219^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
220
221Translation classes are what actually implement the translation of original
222source file message strings to translated message strings. The base class used
223by all translation classes is :class:`NullTranslations`; this provides the basic
224interface you can use to write your own specialized translation classes. Here
225are the methods of :class:`NullTranslations`:
226
227
Georg Brandl036490d2009-05-17 13:00:36 +0000228.. class:: NullTranslations(fp=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000229
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000230 Takes an optional :term:`file object` *fp*, which is ignored by the base class.
Georg Brandl116aa622007-08-15 14:28:22 +0000231 Initializes "protected" instance variables *_info* and *_charset* which are set
232 by derived classes, as well as *_fallback*, which is set through
233 :meth:`add_fallback`. It then calls ``self._parse(fp)`` if *fp* is not
234 ``None``.
235
Georg Brandlbded4d32008-07-17 18:15:35 +0000236 .. method:: _parse(fp)
Georg Brandl116aa622007-08-15 14:28:22 +0000237
Georg Brandlbded4d32008-07-17 18:15:35 +0000238 No-op'd in the base class, this method takes file object *fp*, and reads
239 the data from the file, initializing its message catalog. If you have an
240 unsupported message catalog file format, you should override this method
241 to parse your format.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243
Georg Brandlbded4d32008-07-17 18:15:35 +0000244 .. method:: add_fallback(fallback)
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Georg Brandlbded4d32008-07-17 18:15:35 +0000246 Add *fallback* as the fallback object for the current translation object.
247 A translation object should consult the fallback if it cannot provide a
248 translation for a given message.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250
Georg Brandlbded4d32008-07-17 18:15:35 +0000251 .. method:: gettext(message)
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Georg Brandlbded4d32008-07-17 18:15:35 +0000253 If a fallback has been set, forward :meth:`gettext` to the fallback.
254 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256
Georg Brandlbded4d32008-07-17 18:15:35 +0000257 .. method:: lgettext(message)
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Georg Brandlbded4d32008-07-17 18:15:35 +0000259 If a fallback has been set, forward :meth:`lgettext` to the fallback.
260 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Georg Brandl116aa622007-08-15 14:28:22 +0000262
Georg Brandlbded4d32008-07-17 18:15:35 +0000263 .. method:: ngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Georg Brandlbded4d32008-07-17 18:15:35 +0000265 If a fallback has been set, forward :meth:`ngettext` to the fallback.
266 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
268
Georg Brandlbded4d32008-07-17 18:15:35 +0000269 .. method:: lngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000270
Éric Araujo35a502b2011-10-07 22:02:58 +0200271 If a fallback has been set, forward :meth:`lngettext` to the fallback.
Georg Brandlbded4d32008-07-17 18:15:35 +0000272 Otherwise, return the translated message. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000273
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Georg Brandlbded4d32008-07-17 18:15:35 +0000275 .. method:: info()
Georg Brandl116aa622007-08-15 14:28:22 +0000276
Georg Brandlbded4d32008-07-17 18:15:35 +0000277 Return the "protected" :attr:`_info` variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000278
Georg Brandl116aa622007-08-15 14:28:22 +0000279
Georg Brandlbded4d32008-07-17 18:15:35 +0000280 .. method:: charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Georg Brandlbded4d32008-07-17 18:15:35 +0000282 Return the "protected" :attr:`_charset` variable, which is the encoding of
283 the message catalog file.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
Georg Brandl116aa622007-08-15 14:28:22 +0000285
Georg Brandlbded4d32008-07-17 18:15:35 +0000286 .. method:: output_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Georg Brandlbded4d32008-07-17 18:15:35 +0000288 Return the "protected" :attr:`_output_charset` variable, which defines the
289 encoding used to return translated messages in :meth:`lgettext` and
290 :meth:`lngettext`.
Georg Brandl116aa622007-08-15 14:28:22 +0000291
292
Georg Brandlbded4d32008-07-17 18:15:35 +0000293 .. method:: set_output_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000294
Georg Brandlbded4d32008-07-17 18:15:35 +0000295 Change the "protected" :attr:`_output_charset` variable, which defines the
296 encoding used to return translated messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Benjamin Peterson801844d2008-07-14 14:32:15 +0000298
Georg Brandl036490d2009-05-17 13:00:36 +0000299 .. method:: install(names=None)
Benjamin Peterson801844d2008-07-14 14:32:15 +0000300
Georg Brandlbded4d32008-07-17 18:15:35 +0000301 This method installs :meth:`self.gettext` into the built-in namespace,
302 binding it to ``_``.
Benjamin Peterson801844d2008-07-14 14:32:15 +0000303
Georg Brandlbded4d32008-07-17 18:15:35 +0000304 If the *names* parameter is given, it must be a sequence containing the
Georg Brandl22b34312009-07-26 14:54:51 +0000305 names of functions you want to install in the builtins namespace in
Georg Brandlbded4d32008-07-17 18:15:35 +0000306 addition to :func:`_`. Supported names are ``'gettext'`` (bound to
307 :meth:`self.gettext`), ``'ngettext'`` (bound to :meth:`self.ngettext`),
308 ``'lgettext'`` and ``'lngettext'``.
Benjamin Peterson801844d2008-07-14 14:32:15 +0000309
Georg Brandlbded4d32008-07-17 18:15:35 +0000310 Note that this is only one way, albeit the most convenient way, to make
311 the :func:`_` function available to your application. Because it affects
312 the entire application globally, and specifically the built-in namespace,
313 localized modules should never install :func:`_`. Instead, they should use
314 this code to make :func:`_` available to their module::
Benjamin Peterson801844d2008-07-14 14:32:15 +0000315
Georg Brandlbded4d32008-07-17 18:15:35 +0000316 import gettext
317 t = gettext.translation('mymodule', ...)
318 _ = t.gettext
Benjamin Peterson801844d2008-07-14 14:32:15 +0000319
Georg Brandlbded4d32008-07-17 18:15:35 +0000320 This puts :func:`_` only in the module's global namespace and so only
321 affects calls within this module.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
Georg Brandl116aa622007-08-15 14:28:22 +0000323
324The :class:`GNUTranslations` class
325^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
326
327The :mod:`gettext` module provides one additional class derived from
328:class:`NullTranslations`: :class:`GNUTranslations`. This class overrides
329:meth:`_parse` to enable reading GNU :program:`gettext` format :file:`.mo` files
Benjamin Peterson801844d2008-07-14 14:32:15 +0000330in both big-endian and little-endian format.
Georg Brandl116aa622007-08-15 14:28:22 +0000331
332:class:`GNUTranslations` parses optional meta-data out of the translation
333catalog. It is convention with GNU :program:`gettext` to include meta-data as
334the translation for the empty string. This meta-data is in :rfc:`822`\ -style
335``key: value`` pairs, and should contain the ``Project-Id-Version`` key. If the
336key ``Content-Type`` is found, then the ``charset`` property is used to
337initialize the "protected" :attr:`_charset` instance variable, defaulting to
338``None`` if not found. If the charset encoding is specified, then all message
339ids and message strings read from the catalog are converted to Unicode using
Georg Brandlbded4d32008-07-17 18:15:35 +0000340this encoding, else ASCII encoding is assumed.
341
342Since message ids are read as Unicode strings too, all :meth:`*gettext` methods
343will assume message ids as Unicode strings, not byte strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000344
345The entire set of key/value pairs are placed into a dictionary and set as the
346"protected" :attr:`_info` instance variable.
347
Antoine Pitroube8d06f2014-10-28 20:17:51 +0100348If the :file:`.mo` file's magic number is invalid, the major version number is
349unexpected, or if other problems occur while reading the file, instantiating a
350:class:`GNUTranslations` class can raise :exc:`OSError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352The following methods are overridden from the base class implementation:
353
354
355.. method:: GNUTranslations.gettext(message)
356
357 Look up the *message* id in the catalog and return the corresponding message
Georg Brandlbded4d32008-07-17 18:15:35 +0000358 string, as a Unicode string. If there is no entry in the catalog for the
359 *message* id, and a fallback has been set, the look up is forwarded to the
360 fallback's :meth:`gettext` method. Otherwise, the *message* id is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362
363.. method:: GNUTranslations.lgettext(message)
364
Georg Brandlbded4d32008-07-17 18:15:35 +0000365 Equivalent to :meth:`gettext`, but the translation is returned as a
366 bytestring encoded in the selected output charset, or in the preferred system
367 encoding if no encoding was explicitly set with :meth:`set_output_charset`.
Georg Brandl116aa622007-08-15 14:28:22 +0000368
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Georg Brandl116aa622007-08-15 14:28:22 +0000370.. method:: GNUTranslations.ngettext(singular, plural, n)
371
372 Do a plural-forms lookup of a message id. *singular* is used as the message id
373 for purposes of lookup in the catalog, while *n* is used to determine which
Georg Brandlbded4d32008-07-17 18:15:35 +0000374 plural form to use. The returned message string is a Unicode string.
Georg Brandl116aa622007-08-15 14:28:22 +0000375
376 If the message id is not found in the catalog, and a fallback is specified, the
377 request is forwarded to the fallback's :meth:`ngettext` method. Otherwise, when
378 *n* is 1 *singular* is returned, and *plural* is returned in all other cases.
Georg Brandl48310cd2009-01-03 21:18:54 +0000379
Benjamin Peterson801844d2008-07-14 14:32:15 +0000380 Here is an example::
381
382 n = len(os.listdir('.'))
383 cat = GNUTranslations(somefile)
384 message = cat.ngettext(
385 'There is %(num)d file in this directory',
386 'There are %(num)d files in this directory',
387 n) % {'num': n}
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Georg Brandl116aa622007-08-15 14:28:22 +0000389
390.. method:: GNUTranslations.lngettext(singular, plural, n)
391
Georg Brandlbded4d32008-07-17 18:15:35 +0000392 Equivalent to :meth:`gettext`, but the translation is returned as a
393 bytestring encoded in the selected output charset, or in the preferred system
394 encoding if no encoding was explicitly set with :meth:`set_output_charset`.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Georg Brandl116aa622007-08-15 14:28:22 +0000396
Georg Brandl116aa622007-08-15 14:28:22 +0000397Solaris message catalog support
398^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
399
400The Solaris operating system defines its own binary :file:`.mo` file format, but
401since no documentation can be found on this format, it is not supported at this
402time.
403
404
405The Catalog constructor
406^^^^^^^^^^^^^^^^^^^^^^^
407
408.. index:: single: GNOME
409
410GNOME uses a version of the :mod:`gettext` module by James Henstridge, but this
411version has a slightly different API. Its documented usage was::
412
413 import gettext
414 cat = gettext.Catalog(domain, localedir)
415 _ = cat.gettext
Georg Brandl6911e3c2007-09-04 07:15:32 +0000416 print(_('hello world'))
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418For compatibility with this older module, the function :func:`Catalog` is an
419alias for the :func:`translation` function described above.
420
421One difference between this module and Henstridge's: his catalog objects
422supported access through a mapping API, but this appears to be unused and so is
423not currently supported.
424
425
426Internationalizing your programs and modules
427--------------------------------------------
428
429Internationalization (I18N) refers to the operation by which a program is made
430aware of multiple languages. Localization (L10N) refers to the adaptation of
431your program, once internationalized, to the local language and cultural habits.
432In order to provide multilingual messages for your Python programs, you need to
433take the following steps:
434
435#. prepare your program or module by specially marking translatable strings
436
437#. run a suite of tools over your marked files to generate raw messages catalogs
438
439#. create language specific translations of the message catalogs
440
441#. use the :mod:`gettext` module so that message strings are properly translated
442
443In order to prepare your code for I18N, you need to look at all the strings in
444your files. Any string that needs to be translated should be marked by wrapping
445it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
446
447 filename = 'mylog.txt'
448 message = _('writing a log message')
449 fp = open(filename, 'w')
450 fp.write(message)
451 fp.close()
452
453In this example, the string ``'writing a log message'`` is marked as a candidate
454for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not.
455
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500456There are a few tools to extract the strings meant for translation.
457The original GNU :program:`gettext` only supported C or C++ source
458code but its extended version :program:`xgettext` scans code written
459in a number of languages, including Python, to find strings marked as
460translatable. `Babel <http://babel.pocoo.org/>`__ is a Python
461internationalization library that includes a :file:`pybabel` script to
462extract and compile message catalogs. François Pinard's program
463called :program:`xpot` does a similar job and is available as part of
Georg Brandl525d3552014-10-29 10:26:56 +0100464his `po-utils package <https://github.com/pinard/po-utils>`__.
Georg Brandl116aa622007-08-15 14:28:22 +0000465
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500466(Python also includes pure-Python versions of these programs, called
467:program:`pygettext.py` and :program:`msgfmt.py`; some Python distributions
468will install them for you. :program:`pygettext.py` is similar to
469:program:`xgettext`, but only understands Python source code and
470cannot handle other programming languages such as C or C++.
471:program:`pygettext.py` supports a command-line interface similar to
472:program:`xgettext`; for details on its use, run ``pygettext.py
473--help``. :program:`msgfmt.py` is binary compatible with GNU
474:program:`msgfmt`. With these two programs, you may not need the GNU
475:program:`gettext` package to internationalize your Python
476applications.)
Georg Brandl116aa622007-08-15 14:28:22 +0000477
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500478:program:`xgettext`, :program:`pygettext`, and similar tools generate
479:file:`.po` files that are message catalogs. They are structured
Georg Brandled007d52013-11-24 16:09:26 +0100480human-readable files that contain every marked string in the source
481code, along with a placeholder for the translated versions of these
482strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000483
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500484Copies of these :file:`.po` files are then handed over to the
485individual human translators who write translations for every
486supported natural language. They send back the completed
487language-specific versions as a :file:`<language-name>.po` file that's
488compiled into a machine-readable :file:`.mo` binary catalog file using
489the :program:`msgfmt` program. The :file:`.mo` files are used by the
490:mod:`gettext` module for the actual translation processing at
491run-time.
Georg Brandl116aa622007-08-15 14:28:22 +0000492
493How you use the :mod:`gettext` module in your code depends on whether you are
494internationalizing a single module or your entire application. The next two
495sections will discuss each case.
496
497
498Localizing your module
499^^^^^^^^^^^^^^^^^^^^^^
500
501If you are localizing your module, you must take care not to make global
502changes, e.g. to the built-in namespace. You should not use the GNU ``gettext``
503API but instead the class-based API.
504
505Let's say your module is called "spam" and the module's various natural language
506translation :file:`.mo` files reside in :file:`/usr/share/locale` in GNU
507:program:`gettext` format. Here's what you would put at the top of your
508module::
509
510 import gettext
511 t = gettext.translation('spam', '/usr/share/locale')
512 _ = t.lgettext
513
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515Localizing your application
516^^^^^^^^^^^^^^^^^^^^^^^^^^^
517
518If you are localizing your application, you can install the :func:`_` function
519globally into the built-in namespace, usually in the main driver file of your
520application. This will let all your application-specific files just use
521``_('...')`` without having to explicitly install it in each file.
522
523In the simple case then, you need only add the following bit of code to the main
524driver file of your application::
525
526 import gettext
527 gettext.install('myapplication')
528
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500529If you need to set the locale directory, you can pass it into the
Benjamin Peterson801844d2008-07-14 14:32:15 +0000530:func:`install` function::
Georg Brandl116aa622007-08-15 14:28:22 +0000531
532 import gettext
Benjamin Peterson801844d2008-07-14 14:32:15 +0000533 gettext.install('myapplication', '/usr/share/locale')
Georg Brandl116aa622007-08-15 14:28:22 +0000534
535
536Changing languages on the fly
537^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
538
539If your program needs to support many languages at the same time, you may want
540to create multiple translation instances and then switch between them
541explicitly, like so::
542
543 import gettext
544
545 lang1 = gettext.translation('myapplication', languages=['en'])
546 lang2 = gettext.translation('myapplication', languages=['fr'])
547 lang3 = gettext.translation('myapplication', languages=['de'])
548
549 # start by using language1
550 lang1.install()
551
552 # ... time goes by, user selects language 2
553 lang2.install()
554
555 # ... more time goes by, user selects language 3
556 lang3.install()
557
558
559Deferred translations
560^^^^^^^^^^^^^^^^^^^^^
561
562In most coding situations, strings are translated where they are coded.
563Occasionally however, you need to mark strings for translation, but defer actual
564translation until later. A classic example is::
565
566 animals = ['mollusk',
567 'albatross',
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000568 'rat',
569 'penguin',
570 'python', ]
Georg Brandl116aa622007-08-15 14:28:22 +0000571 # ...
572 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000573 print(a)
Georg Brandl116aa622007-08-15 14:28:22 +0000574
575Here, you want to mark the strings in the ``animals`` list as being
576translatable, but you don't actually want to translate them until they are
577printed.
578
579Here is one way you can handle this situation::
580
581 def _(message): return message
582
583 animals = [_('mollusk'),
584 _('albatross'),
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000585 _('rat'),
586 _('penguin'),
587 _('python'), ]
Georg Brandl116aa622007-08-15 14:28:22 +0000588
589 del _
590
591 # ...
592 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000593 print(_(a))
Georg Brandl116aa622007-08-15 14:28:22 +0000594
595This works because the dummy definition of :func:`_` simply returns the string
596unchanged. And this dummy definition will temporarily override any definition
597of :func:`_` in the built-in namespace (until the :keyword:`del` command). Take
598care, though if you have a previous definition of :func:`_` in the local
599namespace.
600
601Note that the second use of :func:`_` will not identify "a" as being
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500602translatable to the :program:`gettext` program, because the parameter
603is not a string literal.
Georg Brandl116aa622007-08-15 14:28:22 +0000604
605Another way to handle this is with the following example::
606
607 def N_(message): return message
608
609 animals = [N_('mollusk'),
610 N_('albatross'),
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000611 N_('rat'),
612 N_('penguin'),
613 N_('python'), ]
Georg Brandl116aa622007-08-15 14:28:22 +0000614
615 # ...
616 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000617 print(_(a))
Georg Brandl116aa622007-08-15 14:28:22 +0000618
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500619In this case, you are marking translatable strings with the function
620:func:`N_`, which won't conflict with any definition of :func:`_`.
621However, you will need to teach your message extraction program to
622look for translatable strings marked with :func:`N_`. :program:`xgettext`,
623:program:`pygettext`, ``pybabel extract``, and :program:`xpot` all
Martin Panter5c679332016-10-30 04:20:17 +0000624support this through the use of the :option:`!-k` command-line switch.
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500625The choice of :func:`N_` here is totally arbitrary; it could have just
626as easily been :func:`MarkThisStringForTranslation`.
Georg Brandl116aa622007-08-15 14:28:22 +0000627
628
Georg Brandl116aa622007-08-15 14:28:22 +0000629Acknowledgements
630----------------
631
632The following people contributed code, feedback, design suggestions, previous
633implementations, and valuable experience to the creation of this module:
634
635* Peter Funk
636
637* James Henstridge
638
639* Juan David Ibáñez Palomar
640
641* Marc-André Lemburg
642
643* Martin von Löwis
644
645* François Pinard
646
647* Barry Warsaw
648
649* Gustavo Niemeyer
650
651.. rubric:: Footnotes
652
653.. [#] The default locale directory is system dependent; for example, on RedHat Linux
654 it is :file:`/usr/share/locale`, but on Solaris it is :file:`/usr/lib/locale`.
655 The :mod:`gettext` module does not try to support these system dependent
656 defaults; instead its default is :file:`sys.prefix/share/locale`. For this
657 reason, it is always best to call :func:`bindtextdomain` with an explicit
658 absolute path at the start of your application.
659
660.. [#] See the footnote for :func:`bindtextdomain` above.