1:mod:`gettext` --- Multilingual internationalization services
2=============================================================
3
4.. module:: gettext
5   :synopsis: Multilingual internationalization services.
6
7.. moduleauthor:: Barry A. Warsaw <[email protected]>
8.. sectionauthor:: Barry A. Warsaw <[email protected]>
9
10**Source code:** :source:`Lib/gettext.py`
11
12--------------
13
14The :mod:`gettext` module provides internationalization (I18N) and localization
15(L10N) services for your Python modules and applications. It supports both the
16GNU :program:`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
37.. function:: bindtextdomain(domain, localedir=None)
38
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   *language* 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
49.. function:: textdomain(domain=None)
50
51   Change or query the current global domain.  If *domain* is ``None``, then the
52   current global domain is returned, otherwise the global domain is set to
53   *domain*, which is returned.
54
55
56.. index:: single: _ (underscore); gettext
57.. function:: gettext(message)
58
59   Return the localized translation of *message*, based on the current global
60   domain, language, and locale directory.  This function is usually aliased as
61   :func:`_` in the local namespace (see examples below).
62
63
64.. function:: dgettext(domain, message)
65
66   Like :func:`.gettext`, but look the message up in the specified *domain*.
67
68
69.. function:: ngettext(singular, plural, n)
70
71   Like :func:`.gettext`, but consider plural forms. If a translation is found,
72   apply the plural formula to *n*, and return the resulting message (some
73   languages have more than two plural forms). If no translation is found, return
74   *singular* if *n* is 1; return *plural* otherwise.
75
76   The Plural formula is taken from the catalog header. It is a C or Python
77   expression that has a free variable *n*; the expression evaluates to the index
78   of the plural in the catalog. See
79   `the GNU gettext documentation <https://www.gnu.org/software/gettext/manual/gettext.html>`__
80   for the precise syntax to be used in :file:`.po` files and the
81   formulas for a variety of languages.
82
83
84.. function:: dngettext(domain, singular, plural, n)
85
86   Like :func:`ngettext`, but look the message up in the specified *domain*.
87
88
89.. function:: pgettext(context, message)
90.. function:: dpgettext(domain, context, message)
91.. function:: npgettext(context, singular, plural, n)
92.. function:: dnpgettext(domain, context, singular, plural, n)
93
94   Similar to the corresponding functions without the ``p`` in the prefix (that
95   is, :func:`gettext`, :func:`dgettext`, :func:`ngettext`, :func:`dngettext`),
96   but the translation is restricted to the given message *context*.
97
98   .. versionadded:: 3.8
99
100
101Note that GNU :program:`gettext` also defines a :func:`dcgettext` method, but
102this was deemed not useful and so it is currently unimplemented.
103
104Here's an example of typical usage for this API::
105
106   import gettext
107   gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
108   gettext.textdomain('myapplication')
109   _ = gettext.gettext
110   # ...
111   print(_('This is a translatable string.'))
112
113
114Class-based API
115---------------
116
117The class-based API of the :mod:`gettext` module gives you more flexibility and
118greater convenience than the GNU :program:`gettext` API.  It is the recommended
119way of localizing your Python applications and modules.  :mod:`!gettext` defines
120a :class:`GNUTranslations` class which implements the parsing of GNU :file:`.mo` format
121files, and has methods for returning strings. Instances of this class can also
122install themselves in the built-in namespace as the function :func:`_`.
123
124
125.. function:: find(domain, localedir=None, languages=None, all=False)
126
127   This function implements the standard :file:`.mo` file search algorithm.  It
128   takes a *domain*, identical to what :func:`textdomain` takes.  Optional
129   *localedir* is as in :func:`bindtextdomain`. Optional *languages* is a list of
130   strings, where each string is a language code.
131
132   If *localedir* is not given, then the default system locale directory is used.
133   [#]_  If *languages* is not given, then the following environment variables are
134   searched: :envvar:`LANGUAGE`, :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and
135   :envvar:`LANG`.  The first one returning a non-empty value is used for the
136   *languages* variable. The environment variables should contain a colon separated
137   list of languages, which will be split on the colon to produce the expected list
138   of language code strings.
139
140   :func:`find` then expands and normalizes the languages, and then iterates
141   through them, searching for an existing file built of these components:
142
143   :file:`{localedir}/{language}/LC_MESSAGES/{domain}.mo`
144
145   The first such file name that exists is returned by :func:`find`. If no such
146   file is found, then ``None`` is returned. If *all* is given, it returns a list
147   of all file names, in the order in which they appear in the languages list or
148   the environment variables.
149
150
151.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False)
152
153   Return a :class:`*Translations` instance based on the *domain*, *localedir*,
154   and *languages*, which are first passed to :func:`find` to get a list of the
155   associated :file:`.mo` file paths.  Instances with identical :file:`.mo` file
156   names are cached.  The actual class instantiated is *class_* if
157   provided, otherwise :class:`GNUTranslations`.  The class's constructor must
158   take a single :term:`file object` argument.  If provided, *codeset* will change
159   the charset used to encode translated strings in the
160   :meth:`~NullTranslations.lgettext` and :meth:`~NullTranslations.lngettext`
161   methods.
162
163   If multiple files are found, later files are used as fallbacks for earlier ones.
164   To allow setting the fallback, :func:`copy.copy` is used to clone each
165   translation object from the cache; the actual instance data is still shared with
166   the cache.
167
168   If no :file:`.mo` file is found, this function raises :exc:`OSError` if
169   *fallback* is false (which is the default), and returns a
170   :class:`NullTranslations` instance if *fallback* is true.
171
172   .. versionchanged:: 3.3
173      :exc:`IOError` used to be raised instead of :exc:`OSError`.
174
175   .. versionchanged:: 3.11
176      *codeset* parameter is removed.
177
178.. function:: install(domain, localedir=None, *, names=None)
179
180   This installs the function :func:`_` in Python's builtins namespace, based on
181   *domain* and *localedir* which are passed to the function :func:`translation`.
182
183   For the *names* parameter, please see the description of the translation
184   object's :meth:`~NullTranslations.install` method.
185
186   As seen below, you usually mark the strings in your application that are
187   candidates for translation, by wrapping them in a call to the :func:`_`
188   function, like this::
189
190      print(_('This string will be translated.'))
191
192   For convenience, you want the :func:`_` function to be installed in Python's
193   builtins namespace, so it is easily accessible in all modules of your
194   application.
195
196   .. versionchanged:: 3.11
197      *names* is now a keyword-only parameter.
198
199The :class:`NullTranslations` class
200^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
201
202Translation classes are what actually implement the translation of original
203source file message strings to translated message strings. The base class used
204by all translation classes is :class:`NullTranslations`; this provides the basic
205interface you can use to write your own specialized translation classes.  Here
206are the methods of :class:`!NullTranslations`:
207
208
209.. class:: NullTranslations(fp=None)
210
211   Takes an optional :term:`file object` *fp*, which is ignored by the base class.
212   Initializes "protected" instance variables *_info* and *_charset* which are set
213   by derived classes, as well as *_fallback*, which is set through
214   :meth:`add_fallback`.  It then calls ``self._parse(fp)`` if *fp* is not
215   ``None``.
216
217   .. method:: _parse(fp)
218
219      No-op in the base class, this method takes file object *fp*, and reads
220      the data from the file, initializing its message catalog.  If you have an
221      unsupported message catalog file format, you should override this method
222      to parse your format.
223
224
225   .. method:: add_fallback(fallback)
226
227      Add *fallback* as the fallback object for the current translation object.
228      A translation object should consult the fallback if it cannot provide a
229      translation for a given message.
230
231
232   .. method:: gettext(message)
233
234      If a fallback has been set, forward :meth:`!gettext` to the fallback.
235      Otherwise, return *message*.  Overridden in derived classes.
236
237
238   .. method:: ngettext(singular, plural, n)
239
240      If a fallback has been set, forward :meth:`!ngettext` to the fallback.
241      Otherwise, return *singular* if *n* is 1; return *plural* otherwise.
242      Overridden in derived classes.
243
244
245   .. method:: pgettext(context, message)
246
247      If a fallback has been set, forward :meth:`pgettext` to the fallback.
248      Otherwise, return the translated message.  Overridden in derived classes.
249
250      .. versionadded:: 3.8
251
252
253   .. method:: npgettext(context, singular, plural, n)
254
255      If a fallback has been set, forward :meth:`npgettext` to the fallback.
256      Otherwise, return the translated message.  Overridden in derived classes.
257
258      .. versionadded:: 3.8
259
260
261   .. method:: info()
262
263      Return the "protected" :attr:`_info` variable, a dictionary containing
264      the metadata found in the message catalog file.
265
266
267   .. method:: charset()
268
269      Return the encoding of the message catalog file.
270
271
272   .. method:: install(names=None)
273
274      This method installs :meth:`.gettext` into the built-in namespace,
275      binding it to ``_``.
276
277      If the *names* parameter is given, it must be a sequence containing the
278      names of functions you want to install in the builtins namespace in
279      addition to :func:`_`.  Supported names are ``'gettext'``, ``'ngettext'``,
280      ``'pgettext'``, ``'npgettext'``, ``'lgettext'``, and ``'lngettext'``.
281
282      Note that this is only one way, albeit the most convenient way, to make
283      the :func:`_` function available to your application.  Because it affects
284      the entire application globally, and specifically the built-in namespace,
285      localized modules should never install :func:`_`. Instead, they should use
286      this code to make :func:`_` available to their module::
287
288         import gettext
289         t = gettext.translation('mymodule', ...)
290         _ = t.gettext
291
292      This puts :func:`_` only in the module's global namespace and so only
293      affects calls within this module.
294
295      .. versionchanged:: 3.8
296         Added ``'pgettext'`` and ``'npgettext'``.
297
298
299The :class:`GNUTranslations` class
300^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
301
302The :mod:`gettext` module provides one additional class derived from
303:class:`NullTranslations`: :class:`GNUTranslations`.  This class overrides
304:meth:`_parse` to enable reading GNU :program:`gettext` format :file:`.mo` files
305in both big-endian and little-endian format.
306
307:class:`GNUTranslations` parses optional metadata out of the translation
308catalog. It is convention with GNU :program:`gettext` to include metadata as
309the translation for the empty string. This metadata is in :rfc:`822`\ -style
310``key: value`` pairs, and should contain the ``Project-Id-Version`` key.  If the
311key ``Content-Type`` is found, then the ``charset`` property is used to
312initialize the "protected" :attr:`_charset` instance variable, defaulting to
313``None`` if not found.  If the charset encoding is specified, then all message
314ids and message strings read from the catalog are converted to Unicode using
315this encoding, else ASCII is assumed.
316
317Since message ids are read as Unicode strings too, all :meth:`*gettext` methods
318will assume message ids as Unicode strings, not byte strings.
319
320The entire set of key/value pairs are placed into a dictionary and set as the
321"protected" :attr:`_info` instance variable.
322
323If the :file:`.mo` file's magic number is invalid, the major version number is
324unexpected, or if other problems occur while reading the file, instantiating a
325:class:`GNUTranslations` class can raise :exc:`OSError`.
326
327.. class:: GNUTranslations
328
329   The following methods are overridden from the base class implementation:
330
331   .. method:: gettext(message)
332
333      Look up the *message* id in the catalog and return the corresponding message
334      string, as a Unicode string.  If there is no entry in the catalog for the
335      *message* id, and a fallback has been set, the look up is forwarded to the
336      fallback's :meth:`~NullTranslations.gettext` method.  Otherwise, the
337      *message* id is returned.
338
339
340   .. method:: ngettext(singular, plural, n)
341
342      Do a plural-forms lookup of a message id.  *singular* is used as the message id
343      for purposes of lookup in the catalog, while *n* is used to determine which
344      plural form to use.  The returned message string is a Unicode string.
345
346      If the message id is not found in the catalog, and a fallback is specified,
347      the request is forwarded to the fallback's :meth:`~NullTranslations.ngettext`
348      method.  Otherwise, when *n* is 1 *singular* is returned, and *plural* is
349      returned in all other cases.
350
351      Here is an example::
352
353         n = len(os.listdir('.'))
354         cat = GNUTranslations(somefile)
355         message = cat.ngettext(
356             'There is %(num)d file in this directory',
357             'There are %(num)d files in this directory',
358             n) % {'num': n}
359
360
361   .. method:: pgettext(context, message)
362
363      Look up the *context* and *message* id in the catalog and return the
364      corresponding message string, as a Unicode string.  If there is no
365      entry in the catalog for the *message* id and *context*, and a fallback
366      has been set, the look up is forwarded to the fallback's
367      :meth:`pgettext` method.  Otherwise, the *message* id is returned.
368
369      .. versionadded:: 3.8
370
371
372   .. method:: npgettext(context, singular, plural, n)
373
374      Do a plural-forms lookup of a message id.  *singular* is used as the
375      message id for purposes of lookup in the catalog, while *n* is used to
376      determine which plural form to use.
377
378      If the message id for *context* is not found in the catalog, and a
379      fallback is specified, the request is forwarded to the fallback's
380      :meth:`npgettext` method.  Otherwise, when *n* is 1 *singular* is
381      returned, and *plural* is returned in all other cases.
382
383      .. versionadded:: 3.8
384
385
386Solaris message catalog support
387^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
388
389The Solaris operating system defines its own binary :file:`.mo` file format, but
390since no documentation can be found on this format, it is not supported at this
391time.
392
393
394The Catalog constructor
395^^^^^^^^^^^^^^^^^^^^^^^
396
397.. index:: single: GNOME
398
399GNOME uses a version of the :mod:`gettext` module by James Henstridge, but this
400version has a slightly different API.  Its documented usage was::
401
402   import gettext
403   cat = gettext.Catalog(domain, localedir)
404   _ = cat.gettext
405   print(_('hello world'))
406
407For compatibility with this older module, the function :func:`Catalog` is an
408alias for the :func:`translation` function described above.
409
410One difference between this module and Henstridge's: his catalog objects
411supported access through a mapping API, but this appears to be unused and so is
412not currently supported.
413
414
415Internationalizing your programs and modules
416--------------------------------------------
417
418Internationalization (I18N) refers to the operation by which a program is made
419aware of multiple languages.  Localization (L10N) refers to the adaptation of
420your program, once internationalized, to the local language and cultural habits.
421In order to provide multilingual messages for your Python programs, you need to
422take the following steps:
423
424#. prepare your program or module by specially marking translatable strings
425
426#. run a suite of tools over your marked files to generate raw messages catalogs
427
428#. create language-specific translations of the message catalogs
429
430#. use the :mod:`gettext` module so that message strings are properly translated
431
432In order to prepare your code for I18N, you need to look at all the strings in
433your files.  Any string that needs to be translated should be marked by wrapping
434it in ``_('...')`` --- that is, a call to the function :func:`_`.  For example::
435
436   filename = 'mylog.txt'
437   message = _('writing a log message')
438   with open(filename, 'w') as fp:
439       fp.write(message)
440
441In this example, the string ``'writing a log message'`` is marked as a candidate
442for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not.
443
444There are a few tools to extract the strings meant for translation.
445The original GNU :program:`gettext` only supported C or C++ source
446code but its extended version :program:`xgettext` scans code written
447in a number of languages, including Python, to find strings marked as
448translatable.  `Babel <https://babel.pocoo.org/>`__ is a Python
449internationalization library that includes a :file:`pybabel` script to
450extract and compile message catalogs.  François Pinard's program
451called :program:`xpot` does a similar job and is available as part of
452his `po-utils package <https://github.com/pinard/po-utils>`__.
453
454(Python also includes pure-Python versions of these programs, called
455:program:`pygettext.py` and :program:`msgfmt.py`; some Python distributions
456will install them for you.  :program:`pygettext.py` is similar to
457:program:`xgettext`, but only understands Python source code and
458cannot handle other programming languages such as C or C++.
459:program:`pygettext.py` supports a command-line interface similar to
460:program:`xgettext`; for details on its use, run ``pygettext.py
461--help``.  :program:`msgfmt.py` is binary compatible with GNU
462:program:`msgfmt`.  With these two programs, you may not need the GNU
463:program:`gettext` package to internationalize your Python
464applications.)
465
466:program:`xgettext`, :program:`pygettext`, and similar tools generate
467:file:`.po` files that are message catalogs.  They are structured
468human-readable files that contain every marked string in the source
469code, along with a placeholder for the translated versions of these
470strings.
471
472Copies of these :file:`.po` files are then handed over to the
473individual human translators who write translations for every
474supported natural language.  They send back the completed
475language-specific versions as a :file:`<language-name>.po` file that's
476compiled into a machine-readable :file:`.mo` binary catalog file using
477the :program:`msgfmt` program.  The :file:`.mo` files are used by the
478:mod:`gettext` module for the actual translation processing at
479run-time.
480
481How you use the :mod:`gettext` module in your code depends on whether you are
482internationalizing a single module or your entire application. The next two
483sections will discuss each case.
484
485
486Localizing your module
487^^^^^^^^^^^^^^^^^^^^^^
488
489If you are localizing your module, you must take care not to make global
490changes, e.g. to the built-in namespace. You should not use the GNU :program:`gettext`
491API but instead the class-based API.
492
493Let's say your module is called "spam" and the module's various natural language
494translation :file:`.mo` files reside in :file:`/usr/share/locale` in GNU
495:program:`gettext` format.  Here's what you would put at the top of your
496module::
497
498   import gettext
499   t = gettext.translation('spam', '/usr/share/locale')
500   _ = t.gettext
501
502
503Localizing your application
504^^^^^^^^^^^^^^^^^^^^^^^^^^^
505
506If you are localizing your application, you can install the :func:`_` function
507globally into the built-in namespace, usually in the main driver file of your
508application.  This will let all your application-specific files just use
509``_('...')`` without having to explicitly install it in each file.
510
511In the simple case then, you need only add the following bit of code to the main
512driver file of your application::
513
514   import gettext
515   gettext.install('myapplication')
516
517If you need to set the locale directory, you can pass it into the
518:func:`install` function::
519
520   import gettext
521   gettext.install('myapplication', '/usr/share/locale')
522
523
524Changing languages on the fly
525^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
526
527If your program needs to support many languages at the same time, you may want
528to create multiple translation instances and then switch between them
529explicitly, like so::
530
531   import gettext
532
533   lang1 = gettext.translation('myapplication', languages=['en'])
534   lang2 = gettext.translation('myapplication', languages=['fr'])
535   lang3 = gettext.translation('myapplication', languages=['de'])
536
537   # start by using language1
538   lang1.install()
539
540   # ... time goes by, user selects language 2
541   lang2.install()
542
543   # ... more time goes by, user selects language 3
544   lang3.install()
545
546
547Deferred translations
548^^^^^^^^^^^^^^^^^^^^^
549
550In most coding situations, strings are translated where they are coded.
551Occasionally however, you need to mark strings for translation, but defer actual
552translation until later.  A classic example is::
553
554   animals = ['mollusk',
555              'albatross',
556              'rat',
557              'penguin',
558              'python', ]
559   # ...
560   for a in animals:
561       print(a)
562
563Here, you want to mark the strings in the ``animals`` list as being
564translatable, but you don't actually want to translate them until they are
565printed.
566
567Here is one way you can handle this situation::
568
569   def _(message): return message
570
571   animals = [_('mollusk'),
572              _('albatross'),
573              _('rat'),
574              _('penguin'),
575              _('python'), ]
576
577   del _
578
579   # ...
580   for a in animals:
581       print(_(a))
582
583This works because the dummy definition of :func:`_` simply returns the string
584unchanged.  And this dummy definition will temporarily override any definition
585of :func:`_` in the built-in namespace (until the :keyword:`del` command). Take
586care, though if you have a previous definition of :func:`_` in the local
587namespace.
588
589Note that the second use of :func:`_` will not identify "a" as being
590translatable to the :program:`gettext` program, because the parameter
591is not a string literal.
592
593Another way to handle this is with the following example::
594
595   def N_(message): return message
596
597   animals = [N_('mollusk'),
598              N_('albatross'),
599              N_('rat'),
600              N_('penguin'),
601              N_('python'), ]
602
603   # ...
604   for a in animals:
605       print(_(a))
606
607In this case, you are marking translatable strings with the function
608:func:`N_`, which won't conflict with any definition of :func:`_`.
609However, you will need to teach your message extraction program to
610look for translatable strings marked with :func:`N_`. :program:`xgettext`,
611:program:`pygettext`, ``pybabel extract``, and :program:`xpot` all
612support this through the use of the :option:`!-k` command-line switch.
613The choice of :func:`N_` here is totally arbitrary; it could have just
614as easily been :func:`MarkThisStringForTranslation`.
615
616
617Acknowledgements
618----------------
619
620The following people contributed code, feedback, design suggestions, previous
621implementations, and valuable experience to the creation of this module:
622
623* Peter Funk
624
625* James Henstridge
626
627* Juan David Ibáñez Palomar
628
629* Marc-André Lemburg
630
631* Martin von Löwis
632
633* François Pinard
634
635* Barry Warsaw
636
637* Gustavo Niemeyer
638
639.. rubric:: Footnotes
640
641.. [#] The default locale directory is system dependent; for example, on RedHat Linux
642   it is :file:`/usr/share/locale`, but on Solaris it is :file:`/usr/lib/locale`.
643   The :mod:`gettext` module does not try to support these system dependent
644   defaults; instead its default is :file:`{sys.base_prefix}/share/locale` (see
645   :data:`sys.base_prefix`). For this reason, it is always best to call
646   :func:`bindtextdomain` with an explicit absolute path at the start of your
647   application.
648
649.. [#] See the footnote for :func:`bindtextdomain` above.
650