blob: 38515ebdf591071edca2ec3019ff0b245f47ae56 [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
Stéphane Wirtel55f33172018-11-04 23:24:41 +010016GNU :program:`gettext` message catalog API and a higher level, class-based API that may
Georg Brandl116aa622007-08-15 14:28:22 +000017be 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
Stéphane Wirtel55f33172018-11-04 23:24:41 +010041 the path (on Unix): :file:`{localedir}/{language}/LC_MESSAGES/{domain}.mo`, where
Georg Brandl116aa622007-08-15 14:28:22 +000042 *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
Serhiy Storchaka26cb4652017-06-20 17:13:29 +030051 Bind the *domain* to *codeset*, changing the encoding of byte strings
52 returned by the :func:`lgettext`, :func:`ldgettext`, :func:`lngettext`
53 and :func:`ldngettext` functions.
54 If *codeset* is omitted, then the current binding is returned.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Serhiy Storchakafec35c92018-10-27 08:00:41 +030056 .. deprecated-removed:: 3.8 3.10
57
Georg Brandl116aa622007-08-15 14:28:22 +000058
Georg Brandl036490d2009-05-17 13:00:36 +000059.. function:: textdomain(domain=None)
Georg Brandl116aa622007-08-15 14:28:22 +000060
61 Change or query the current global domain. If *domain* is ``None``, then the
62 current global domain is returned, otherwise the global domain is set to
63 *domain*, which is returned.
64
65
Serhiy Storchaka913876d2018-10-28 13:41:26 +020066.. index:: single: _ (underscore); gettext
Georg Brandl116aa622007-08-15 14:28:22 +000067.. function:: gettext(message)
68
69 Return the localized translation of *message*, based on the current global
70 domain, language, and locale directory. This function is usually aliased as
71 :func:`_` in the local namespace (see examples below).
72
73
Georg Brandl116aa622007-08-15 14:28:22 +000074.. function:: dgettext(domain, message)
75
Serhiy Storchaka26cb4652017-06-20 17:13:29 +030076 Like :func:`.gettext`, but look the message up in the specified *domain*.
Georg Brandl116aa622007-08-15 14:28:22 +000077
Georg Brandl116aa622007-08-15 14:28:22 +000078
79.. function:: ngettext(singular, plural, n)
80
Serhiy Storchaka26cb4652017-06-20 17:13:29 +030081 Like :func:`.gettext`, but consider plural forms. If a translation is found,
Georg Brandl116aa622007-08-15 14:28:22 +000082 apply the plural formula to *n*, and return the resulting message (some
83 languages have more than two plural forms). If no translation is found, return
84 *singular* if *n* is 1; return *plural* otherwise.
85
86 The Plural formula is taken from the catalog header. It is a C or Python
87 expression that has a free variable *n*; the expression evaluates to the index
Andrew Kuchling30c5ad22013-11-19 11:05:20 -050088 of the plural in the catalog. See
89 `the GNU gettext documentation <https://www.gnu.org/software/gettext/manual/gettext.html>`__
90 for the precise syntax to be used in :file:`.po` files and the
91 formulas for a variety of languages.
Georg Brandl116aa622007-08-15 14:28:22 +000092
Georg Brandl116aa622007-08-15 14:28:22 +000093
Georg Brandl116aa622007-08-15 14:28:22 +000094.. function:: dngettext(domain, singular, plural, n)
95
96 Like :func:`ngettext`, but look the message up in the specified *domain*.
97
Georg Brandl116aa622007-08-15 14:28:22 +000098
Serhiy Storchaka26cb4652017-06-20 17:13:29 +030099.. function:: lgettext(message)
100.. function:: ldgettext(domain, message)
101.. function:: lngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000102.. function:: ldngettext(domain, singular, plural, n)
103
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300104 Equivalent to the corresponding functions without the ``l`` prefix
105 (:func:`.gettext`, :func:`dgettext`, :func:`ngettext` and :func:`dngettext`),
106 but the translation is returned as a byte string encoded in the preferred
107 system encoding if no other encoding was explicitly set with
Georg Brandl116aa622007-08-15 14:28:22 +0000108 :func:`bind_textdomain_codeset`.
109
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300110 .. warning::
111
112 These functions should be avoided in Python 3, because they return
113 encoded bytes. It's much better to use alternatives which return
114 Unicode strings instead, since most Python applications will want to
115 manipulate human readable text as strings instead of bytes. Further,
116 it's possible that you may get unexpected Unicode-related exceptions
Serhiy Storchakafec35c92018-10-27 08:00:41 +0300117 if there are encoding problems with the translated strings.
118
119 .. deprecated-removed:: 3.8 3.10
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300120
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but
123this was deemed not useful and so it is currently unimplemented.
124
125Here's an example of typical usage for this API::
126
127 import gettext
128 gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
129 gettext.textdomain('myapplication')
130 _ = gettext.gettext
131 # ...
Georg Brandl6911e3c2007-09-04 07:15:32 +0000132 print(_('This is a translatable string.'))
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134
135Class-based API
136---------------
137
138The class-based API of the :mod:`gettext` module gives you more flexibility and
139greater convenience than the GNU :program:`gettext` API. It is the recommended
Serhiy Storchakac02a1f42017-10-04 20:28:20 +0300140way of localizing your Python applications and modules. :mod:`!gettext` defines
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100141a :class:`GNUTranslations` class which implements the parsing of GNU :file:`.mo` format
142files, and has methods for returning strings. Instances of this class can also
143install themselves in the built-in namespace as the function :func:`_`.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
Georg Brandl036490d2009-05-17 13:00:36 +0000146.. function:: find(domain, localedir=None, languages=None, all=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148 This function implements the standard :file:`.mo` file search algorithm. It
149 takes a *domain*, identical to what :func:`textdomain` takes. Optional
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100150 *localedir* is as in :func:`bindtextdomain`. Optional *languages* is a list of
Georg Brandl116aa622007-08-15 14:28:22 +0000151 strings, where each string is a language code.
152
153 If *localedir* is not given, then the default system locale directory is used.
154 [#]_ If *languages* is not given, then the following environment variables are
155 searched: :envvar:`LANGUAGE`, :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
156 :envvar:`LANG`. The first one returning a non-empty value is used for the
157 *languages* variable. The environment variables should contain a colon separated
158 list of languages, which will be split on the colon to produce the expected list
159 of language code strings.
160
161 :func:`find` then expands and normalizes the languages, and then iterates
162 through them, searching for an existing file built of these components:
163
Georg Brandl036490d2009-05-17 13:00:36 +0000164 :file:`{localedir}/{language}/LC_MESSAGES/{domain}.mo`
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166 The first such file name that exists is returned by :func:`find`. If no such
167 file is found, then ``None`` is returned. If *all* is given, it returns a list
168 of all file names, in the order in which they appear in the languages list or
169 the environment variables.
170
171
Georg Brandl036490d2009-05-17 13:00:36 +0000172.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100174 Return a :class:`*Translations` instance based on the *domain*, *localedir*,
Georg Brandlbded4d32008-07-17 18:15:35 +0000175 and *languages*, which are first passed to :func:`find` to get a list of the
Georg Brandl116aa622007-08-15 14:28:22 +0000176 associated :file:`.mo` file paths. Instances with identical :file:`.mo` file
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100177 names are cached. The actual class instantiated is *class_* if
Georg Brandlbded4d32008-07-17 18:15:35 +0000178 provided, otherwise :class:`GNUTranslations`. The class's constructor must
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000179 take a single :term:`file object` argument. If provided, *codeset* will change
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300180 the charset used to encode translated strings in the
181 :meth:`~NullTranslations.lgettext` and :meth:`~NullTranslations.lngettext`
182 methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000183
184 If multiple files are found, later files are used as fallbacks for earlier ones.
185 To allow setting the fallback, :func:`copy.copy` is used to clone each
186 translation object from the cache; the actual instance data is still shared with
187 the cache.
188
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200189 If no :file:`.mo` file is found, this function raises :exc:`OSError` if
Georg Brandl116aa622007-08-15 14:28:22 +0000190 *fallback* is false (which is the default), and returns a
191 :class:`NullTranslations` instance if *fallback* is true.
192
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200193 .. versionchanged:: 3.3
194 :exc:`IOError` used to be raised instead of :exc:`OSError`.
195
Serhiy Storchakafec35c92018-10-27 08:00:41 +0300196 .. deprecated-removed:: 3.8 3.10
197 The *codeset* parameter.
198
Georg Brandl116aa622007-08-15 14:28:22 +0000199
Georg Brandl036490d2009-05-17 13:00:36 +0000200.. function:: install(domain, localedir=None, codeset=None, names=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000201
Georg Brandl22b34312009-07-26 14:54:51 +0000202 This installs the function :func:`_` in Python's builtins namespace, based on
Georg Brandl116aa622007-08-15 14:28:22 +0000203 *domain*, *localedir*, and *codeset* which are passed to the function
Benjamin Peterson801844d2008-07-14 14:32:15 +0000204 :func:`translation`.
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206 For the *names* parameter, please see the description of the translation
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000207 object's :meth:`~NullTranslations.install` method.
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209 As seen below, you usually mark the strings in your application that are
210 candidates for translation, by wrapping them in a call to the :func:`_`
211 function, like this::
212
Georg Brandl6911e3c2007-09-04 07:15:32 +0000213 print(_('This string will be translated.'))
Georg Brandl116aa622007-08-15 14:28:22 +0000214
215 For convenience, you want the :func:`_` function to be installed in Python's
Georg Brandl22b34312009-07-26 14:54:51 +0000216 builtins namespace, so it is easily accessible in all modules of your
Georg Brandl116aa622007-08-15 14:28:22 +0000217 application.
218
Serhiy Storchakafec35c92018-10-27 08:00:41 +0300219 .. deprecated-removed:: 3.8 3.10
220 The *codeset* parameter.
221
Georg Brandl116aa622007-08-15 14:28:22 +0000222
223The :class:`NullTranslations` class
224^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
225
226Translation classes are what actually implement the translation of original
227source file message strings to translated message strings. The base class used
228by all translation classes is :class:`NullTranslations`; this provides the basic
229interface you can use to write your own specialized translation classes. Here
Serhiy Storchakac02a1f42017-10-04 20:28:20 +0300230are the methods of :class:`!NullTranslations`:
Georg Brandl116aa622007-08-15 14:28:22 +0000231
232
Georg Brandl036490d2009-05-17 13:00:36 +0000233.. class:: NullTranslations(fp=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Antoine Pitrou11cb9612010-09-15 11:11:28 +0000235 Takes an optional :term:`file object` *fp*, which is ignored by the base class.
Georg Brandl116aa622007-08-15 14:28:22 +0000236 Initializes "protected" instance variables *_info* and *_charset* which are set
237 by derived classes, as well as *_fallback*, which is set through
238 :meth:`add_fallback`. It then calls ``self._parse(fp)`` if *fp* is not
239 ``None``.
240
Georg Brandlbded4d32008-07-17 18:15:35 +0000241 .. method:: _parse(fp)
Georg Brandl116aa622007-08-15 14:28:22 +0000242
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100243 No-op in the base class, this method takes file object *fp*, and reads
Georg Brandlbded4d32008-07-17 18:15:35 +0000244 the data from the file, initializing its message catalog. If you have an
245 unsupported message catalog file format, you should override this method
246 to parse your format.
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248
Georg Brandlbded4d32008-07-17 18:15:35 +0000249 .. method:: add_fallback(fallback)
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Georg Brandlbded4d32008-07-17 18:15:35 +0000251 Add *fallback* as the fallback object for the current translation object.
252 A translation object should consult the fallback if it cannot provide a
253 translation for a given message.
Georg Brandl116aa622007-08-15 14:28:22 +0000254
255
Georg Brandlbded4d32008-07-17 18:15:35 +0000256 .. method:: gettext(message)
Georg Brandl116aa622007-08-15 14:28:22 +0000257
Serhiy Storchakac02a1f42017-10-04 20:28:20 +0300258 If a fallback has been set, forward :meth:`!gettext` to the fallback.
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300259 Otherwise, return *message*. Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Georg Brandlbded4d32008-07-17 18:15:35 +0000262 .. method:: ngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000263
Serhiy Storchakac02a1f42017-10-04 20:28:20 +0300264 If a fallback has been set, forward :meth:`!ngettext` to the fallback.
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300265 Otherwise, return *singular* if *n* is 1; return *plural* otherwise.
266 Overridden in derived classes.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
268
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300269 .. method:: lgettext(message)
Georg Brandlbded4d32008-07-17 18:15:35 +0000270 .. method:: lngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000271
Serhiy Storchakac02a1f42017-10-04 20:28:20 +0300272 Equivalent to :meth:`.gettext` and :meth:`.ngettext`, but the translation
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300273 is returned as a byte string encoded in the preferred system encoding
274 if no encoding was explicitly set with :meth:`set_output_charset`.
275 Overridden in derived classes.
276
277 .. warning::
278
279 These methods should be avoided in Python 3. See the warning for the
280 :func:`lgettext` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Serhiy Storchakafec35c92018-10-27 08:00:41 +0300282 .. deprecated-removed:: 3.8 3.10
283
Georg Brandl116aa622007-08-15 14:28:22 +0000284
Georg Brandlbded4d32008-07-17 18:15:35 +0000285 .. method:: info()
Georg Brandl116aa622007-08-15 14:28:22 +0000286
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100287 Return the "protected" :attr:`_info` variable, a dictionary containing
288 the metadata found in the message catalog file.
Georg Brandl116aa622007-08-15 14:28:22 +0000289
Georg Brandl116aa622007-08-15 14:28:22 +0000290
Georg Brandlbded4d32008-07-17 18:15:35 +0000291 .. method:: charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000292
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300293 Return the encoding of the message catalog file.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
Georg Brandl116aa622007-08-15 14:28:22 +0000295
Georg Brandlbded4d32008-07-17 18:15:35 +0000296 .. method:: output_charset()
Georg Brandl116aa622007-08-15 14:28:22 +0000297
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300298 Return the encoding used to return translated messages in :meth:`.lgettext`
299 and :meth:`.lngettext`.
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Serhiy Storchakafec35c92018-10-27 08:00:41 +0300301 .. deprecated-removed:: 3.8 3.10
302
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Georg Brandlbded4d32008-07-17 18:15:35 +0000304 .. method:: set_output_charset(charset)
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300306 Change the encoding used to return translated messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
Serhiy Storchakafec35c92018-10-27 08:00:41 +0300308 .. deprecated-removed:: 3.8 3.10
309
Benjamin Peterson801844d2008-07-14 14:32:15 +0000310
Georg Brandl036490d2009-05-17 13:00:36 +0000311 .. method:: install(names=None)
Benjamin Peterson801844d2008-07-14 14:32:15 +0000312
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300313 This method installs :meth:`.gettext` into the built-in namespace,
Georg Brandlbded4d32008-07-17 18:15:35 +0000314 binding it to ``_``.
Benjamin Peterson801844d2008-07-14 14:32:15 +0000315
Georg Brandlbded4d32008-07-17 18:15:35 +0000316 If the *names* parameter is given, it must be a sequence containing the
Georg Brandl22b34312009-07-26 14:54:51 +0000317 names of functions you want to install in the builtins namespace in
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300318 addition to :func:`_`. Supported names are ``'gettext'``, ``'ngettext'``,
Georg Brandlbded4d32008-07-17 18:15:35 +0000319 ``'lgettext'`` and ``'lngettext'``.
Benjamin Peterson801844d2008-07-14 14:32:15 +0000320
Georg Brandlbded4d32008-07-17 18:15:35 +0000321 Note that this is only one way, albeit the most convenient way, to make
322 the :func:`_` function available to your application. Because it affects
323 the entire application globally, and specifically the built-in namespace,
324 localized modules should never install :func:`_`. Instead, they should use
325 this code to make :func:`_` available to their module::
Benjamin Peterson801844d2008-07-14 14:32:15 +0000326
Georg Brandlbded4d32008-07-17 18:15:35 +0000327 import gettext
328 t = gettext.translation('mymodule', ...)
329 _ = t.gettext
Benjamin Peterson801844d2008-07-14 14:32:15 +0000330
Georg Brandlbded4d32008-07-17 18:15:35 +0000331 This puts :func:`_` only in the module's global namespace and so only
332 affects calls within this module.
Georg Brandl116aa622007-08-15 14:28:22 +0000333
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335The :class:`GNUTranslations` class
336^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
337
338The :mod:`gettext` module provides one additional class derived from
339:class:`NullTranslations`: :class:`GNUTranslations`. This class overrides
340:meth:`_parse` to enable reading GNU :program:`gettext` format :file:`.mo` files
Benjamin Peterson801844d2008-07-14 14:32:15 +0000341in both big-endian and little-endian format.
Georg Brandl116aa622007-08-15 14:28:22 +0000342
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100343:class:`GNUTranslations` parses optional metadata out of the translation
344catalog. It is convention with GNU :program:`gettext` to include metadata as
345the translation for the empty string. This metadata is in :rfc:`822`\ -style
Georg Brandl116aa622007-08-15 14:28:22 +0000346``key: value`` pairs, and should contain the ``Project-Id-Version`` key. If the
347key ``Content-Type`` is found, then the ``charset`` property is used to
348initialize the "protected" :attr:`_charset` instance variable, defaulting to
349``None`` if not found. If the charset encoding is specified, then all message
350ids and message strings read from the catalog are converted to Unicode using
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100351this encoding, else ASCII is assumed.
Georg Brandlbded4d32008-07-17 18:15:35 +0000352
353Since message ids are read as Unicode strings too, all :meth:`*gettext` methods
354will assume message ids as Unicode strings, not byte strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000355
356The entire set of key/value pairs are placed into a dictionary and set as the
357"protected" :attr:`_info` instance variable.
358
Antoine Pitroube8d06f2014-10-28 20:17:51 +0100359If the :file:`.mo` file's magic number is invalid, the major version number is
360unexpected, or if other problems occur while reading the file, instantiating a
361:class:`GNUTranslations` class can raise :exc:`OSError`.
Georg Brandl116aa622007-08-15 14:28:22 +0000362
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300363.. class:: GNUTranslations
364
365 The following methods are overridden from the base class implementation:
366
367 .. method:: gettext(message)
368
369 Look up the *message* id in the catalog and return the corresponding message
370 string, as a Unicode string. If there is no entry in the catalog for the
371 *message* id, and a fallback has been set, the look up is forwarded to the
372 fallback's :meth:`~NullTranslations.gettext` method. Otherwise, the
373 *message* id is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000374
375
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300376 .. method:: ngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000377
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300378 Do a plural-forms lookup of a message id. *singular* is used as the message id
379 for purposes of lookup in the catalog, while *n* is used to determine which
380 plural form to use. The returned message string is a Unicode string.
381
382 If the message id is not found in the catalog, and a fallback is specified,
383 the request is forwarded to the fallback's :meth:`~NullTranslations.ngettext`
384 method. Otherwise, when *n* is 1 *singular* is returned, and *plural* is
385 returned in all other cases.
386
387 Here is an example::
388
389 n = len(os.listdir('.'))
390 cat = GNUTranslations(somefile)
391 message = cat.ngettext(
392 'There is %(num)d file in this directory',
393 'There are %(num)d files in this directory',
394 n) % {'num': n}
Georg Brandl116aa622007-08-15 14:28:22 +0000395
396
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300397 .. method:: lgettext(message)
398 .. method:: lngettext(singular, plural, n)
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300400 Equivalent to :meth:`.gettext` and :meth:`.ngettext`, but the translation
401 is returned as a byte string encoded in the preferred system encoding
402 if no encoding was explicitly set with
403 :meth:`~NullTranslations.set_output_charset`.
Georg Brandl116aa622007-08-15 14:28:22 +0000404
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300405 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +0000406
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300407 These methods should be avoided in Python 3. See the warning for the
408 :func:`lgettext` function.
Georg Brandl116aa622007-08-15 14:28:22 +0000409
Serhiy Storchakafec35c92018-10-27 08:00:41 +0300410 .. deprecated-removed:: 3.8 3.10
411
Georg Brandl116aa622007-08-15 14:28:22 +0000412
Georg Brandl116aa622007-08-15 14:28:22 +0000413Solaris message catalog support
414^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
415
416The Solaris operating system defines its own binary :file:`.mo` file format, but
417since no documentation can be found on this format, it is not supported at this
418time.
419
420
421The Catalog constructor
422^^^^^^^^^^^^^^^^^^^^^^^
423
424.. index:: single: GNOME
425
426GNOME uses a version of the :mod:`gettext` module by James Henstridge, but this
427version has a slightly different API. Its documented usage was::
428
429 import gettext
430 cat = gettext.Catalog(domain, localedir)
431 _ = cat.gettext
Georg Brandl6911e3c2007-09-04 07:15:32 +0000432 print(_('hello world'))
Georg Brandl116aa622007-08-15 14:28:22 +0000433
434For compatibility with this older module, the function :func:`Catalog` is an
435alias for the :func:`translation` function described above.
436
437One difference between this module and Henstridge's: his catalog objects
438supported access through a mapping API, but this appears to be unused and so is
439not currently supported.
440
441
442Internationalizing your programs and modules
443--------------------------------------------
444
445Internationalization (I18N) refers to the operation by which a program is made
446aware of multiple languages. Localization (L10N) refers to the adaptation of
447your program, once internationalized, to the local language and cultural habits.
448In order to provide multilingual messages for your Python programs, you need to
449take the following steps:
450
451#. prepare your program or module by specially marking translatable strings
452
453#. run a suite of tools over your marked files to generate raw messages catalogs
454
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100455#. create language-specific translations of the message catalogs
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457#. use the :mod:`gettext` module so that message strings are properly translated
458
459In order to prepare your code for I18N, you need to look at all the strings in
460your files. Any string that needs to be translated should be marked by wrapping
461it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
462
463 filename = 'mylog.txt'
464 message = _('writing a log message')
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100465 with open(filename, 'w') as fp:
466 fp.write(message)
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468In this example, the string ``'writing a log message'`` is marked as a candidate
469for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not.
470
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500471There are a few tools to extract the strings meant for translation.
472The original GNU :program:`gettext` only supported C or C++ source
473code but its extended version :program:`xgettext` scans code written
474in a number of languages, including Python, to find strings marked as
475translatable. `Babel <http://babel.pocoo.org/>`__ is a Python
476internationalization library that includes a :file:`pybabel` script to
477extract and compile message catalogs. François Pinard's program
478called :program:`xpot` does a similar job and is available as part of
Georg Brandl525d3552014-10-29 10:26:56 +0100479his `po-utils package <https://github.com/pinard/po-utils>`__.
Georg Brandl116aa622007-08-15 14:28:22 +0000480
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500481(Python also includes pure-Python versions of these programs, called
482:program:`pygettext.py` and :program:`msgfmt.py`; some Python distributions
483will install them for you. :program:`pygettext.py` is similar to
484:program:`xgettext`, but only understands Python source code and
485cannot handle other programming languages such as C or C++.
486:program:`pygettext.py` supports a command-line interface similar to
487:program:`xgettext`; for details on its use, run ``pygettext.py
488--help``. :program:`msgfmt.py` is binary compatible with GNU
489:program:`msgfmt`. With these two programs, you may not need the GNU
490:program:`gettext` package to internationalize your Python
491applications.)
Georg Brandl116aa622007-08-15 14:28:22 +0000492
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500493:program:`xgettext`, :program:`pygettext`, and similar tools generate
494:file:`.po` files that are message catalogs. They are structured
Georg Brandled007d52013-11-24 16:09:26 +0100495human-readable files that contain every marked string in the source
496code, along with a placeholder for the translated versions of these
497strings.
Georg Brandl116aa622007-08-15 14:28:22 +0000498
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500499Copies of these :file:`.po` files are then handed over to the
500individual human translators who write translations for every
501supported natural language. They send back the completed
502language-specific versions as a :file:`<language-name>.po` file that's
503compiled into a machine-readable :file:`.mo` binary catalog file using
504the :program:`msgfmt` program. The :file:`.mo` files are used by the
505:mod:`gettext` module for the actual translation processing at
506run-time.
Georg Brandl116aa622007-08-15 14:28:22 +0000507
508How you use the :mod:`gettext` module in your code depends on whether you are
509internationalizing a single module or your entire application. The next two
510sections will discuss each case.
511
512
513Localizing your module
514^^^^^^^^^^^^^^^^^^^^^^
515
516If you are localizing your module, you must take care not to make global
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100517changes, e.g. to the built-in namespace. You should not use the GNU :program:`gettext`
Georg Brandl116aa622007-08-15 14:28:22 +0000518API but instead the class-based API.
519
520Let's say your module is called "spam" and the module's various natural language
521translation :file:`.mo` files reside in :file:`/usr/share/locale` in GNU
522:program:`gettext` format. Here's what you would put at the top of your
523module::
524
525 import gettext
526 t = gettext.translation('spam', '/usr/share/locale')
Serhiy Storchaka26cb4652017-06-20 17:13:29 +0300527 _ = t.gettext
Georg Brandl116aa622007-08-15 14:28:22 +0000528
Georg Brandl116aa622007-08-15 14:28:22 +0000529
530Localizing your application
531^^^^^^^^^^^^^^^^^^^^^^^^^^^
532
533If you are localizing your application, you can install the :func:`_` function
534globally into the built-in namespace, usually in the main driver file of your
535application. This will let all your application-specific files just use
536``_('...')`` without having to explicitly install it in each file.
537
538In the simple case then, you need only add the following bit of code to the main
539driver file of your application::
540
541 import gettext
542 gettext.install('myapplication')
543
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500544If you need to set the locale directory, you can pass it into the
Benjamin Peterson801844d2008-07-14 14:32:15 +0000545:func:`install` function::
Georg Brandl116aa622007-08-15 14:28:22 +0000546
547 import gettext
Benjamin Peterson801844d2008-07-14 14:32:15 +0000548 gettext.install('myapplication', '/usr/share/locale')
Georg Brandl116aa622007-08-15 14:28:22 +0000549
550
551Changing languages on the fly
552^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
553
554If your program needs to support many languages at the same time, you may want
555to create multiple translation instances and then switch between them
556explicitly, like so::
557
558 import gettext
559
560 lang1 = gettext.translation('myapplication', languages=['en'])
561 lang2 = gettext.translation('myapplication', languages=['fr'])
562 lang3 = gettext.translation('myapplication', languages=['de'])
563
564 # start by using language1
565 lang1.install()
566
567 # ... time goes by, user selects language 2
568 lang2.install()
569
570 # ... more time goes by, user selects language 3
571 lang3.install()
572
573
574Deferred translations
575^^^^^^^^^^^^^^^^^^^^^
576
577In most coding situations, strings are translated where they are coded.
578Occasionally however, you need to mark strings for translation, but defer actual
579translation until later. A classic example is::
580
581 animals = ['mollusk',
582 'albatross',
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000583 'rat',
584 'penguin',
585 'python', ]
Georg Brandl116aa622007-08-15 14:28:22 +0000586 # ...
587 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000588 print(a)
Georg Brandl116aa622007-08-15 14:28:22 +0000589
590Here, you want to mark the strings in the ``animals`` list as being
591translatable, but you don't actually want to translate them until they are
592printed.
593
594Here is one way you can handle this situation::
595
596 def _(message): return message
597
598 animals = [_('mollusk'),
599 _('albatross'),
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000600 _('rat'),
601 _('penguin'),
602 _('python'), ]
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604 del _
605
606 # ...
607 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000608 print(_(a))
Georg Brandl116aa622007-08-15 14:28:22 +0000609
610This works because the dummy definition of :func:`_` simply returns the string
611unchanged. And this dummy definition will temporarily override any definition
612of :func:`_` in the built-in namespace (until the :keyword:`del` command). Take
613care, though if you have a previous definition of :func:`_` in the local
614namespace.
615
616Note that the second use of :func:`_` will not identify "a" as being
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500617translatable to the :program:`gettext` program, because the parameter
618is not a string literal.
Georg Brandl116aa622007-08-15 14:28:22 +0000619
620Another way to handle this is with the following example::
621
622 def N_(message): return message
623
624 animals = [N_('mollusk'),
625 N_('albatross'),
Georg Brandla1c6a1c2009-01-03 21:26:05 +0000626 N_('rat'),
627 N_('penguin'),
628 N_('python'), ]
Georg Brandl116aa622007-08-15 14:28:22 +0000629
630 # ...
631 for a in animals:
Georg Brandl6911e3c2007-09-04 07:15:32 +0000632 print(_(a))
Georg Brandl116aa622007-08-15 14:28:22 +0000633
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500634In this case, you are marking translatable strings with the function
635:func:`N_`, which won't conflict with any definition of :func:`_`.
636However, you will need to teach your message extraction program to
637look for translatable strings marked with :func:`N_`. :program:`xgettext`,
638:program:`pygettext`, ``pybabel extract``, and :program:`xpot` all
Martin Panter5c679332016-10-30 04:20:17 +0000639support this through the use of the :option:`!-k` command-line switch.
Andrew Kuchling30c5ad22013-11-19 11:05:20 -0500640The choice of :func:`N_` here is totally arbitrary; it could have just
641as easily been :func:`MarkThisStringForTranslation`.
Georg Brandl116aa622007-08-15 14:28:22 +0000642
643
Georg Brandl116aa622007-08-15 14:28:22 +0000644Acknowledgements
645----------------
646
647The following people contributed code, feedback, design suggestions, previous
648implementations, and valuable experience to the creation of this module:
649
650* Peter Funk
651
652* James Henstridge
653
654* Juan David Ibáñez Palomar
655
656* Marc-André Lemburg
657
658* Martin von Löwis
659
660* François Pinard
661
662* Barry Warsaw
663
664* Gustavo Niemeyer
665
666.. rubric:: Footnotes
667
668.. [#] The default locale directory is system dependent; for example, on RedHat Linux
669 it is :file:`/usr/share/locale`, but on Solaris it is :file:`/usr/lib/locale`.
670 The :mod:`gettext` module does not try to support these system dependent
Stéphane Wirtel55f33172018-11-04 23:24:41 +0100671 defaults; instead its default is :file:`{sys.prefix}/share/locale` (see
672 :data:`sys.prefix`). For this reason, it is always best to call
673 :func:`bindtextdomain` with an explicit absolute path at the start of your
674 application.
Georg Brandl116aa622007-08-15 14:28:22 +0000675
676.. [#] See the footnote for :func:`bindtextdomain` above.