17db96d56Sopenharmony_ci:mod:`gettext` --- Multilingual internationalization services 27db96d56Sopenharmony_ci============================================================= 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: gettext 57db96d56Sopenharmony_ci :synopsis: Multilingual internationalization services. 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci.. moduleauthor:: Barry A. Warsaw <barry@python.org> 87db96d56Sopenharmony_ci.. sectionauthor:: Barry A. Warsaw <barry@python.org> 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci**Source code:** :source:`Lib/gettext.py` 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ci-------------- 137db96d56Sopenharmony_ci 147db96d56Sopenharmony_ciThe :mod:`gettext` module provides internationalization (I18N) and localization 157db96d56Sopenharmony_ci(L10N) services for your Python modules and applications. It supports both the 167db96d56Sopenharmony_ciGNU :program:`gettext` message catalog API and a higher level, class-based API that may 177db96d56Sopenharmony_cibe more appropriate for Python files. The interface described below allows you 187db96d56Sopenharmony_cito write your module and application messages in one natural language, and 197db96d56Sopenharmony_ciprovide a catalog of translated messages for running under different natural 207db96d56Sopenharmony_cilanguages. 217db96d56Sopenharmony_ci 227db96d56Sopenharmony_ciSome hints on localizing your Python modules and applications are also given. 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ci 257db96d56Sopenharmony_ciGNU :program:`gettext` API 267db96d56Sopenharmony_ci-------------------------- 277db96d56Sopenharmony_ci 287db96d56Sopenharmony_ciThe :mod:`gettext` module defines the following API, which is very similar to 297db96d56Sopenharmony_cithe GNU :program:`gettext` API. If you use this API you will affect the 307db96d56Sopenharmony_citranslation of your entire application globally. Often this is what you want if 317db96d56Sopenharmony_ciyour application is monolingual, with the choice of language dependent on the 327db96d56Sopenharmony_cilocale of your user. If you are localizing a Python module, or if your 337db96d56Sopenharmony_ciapplication needs to switch languages on the fly, you probably want to use the 347db96d56Sopenharmony_ciclass-based API instead. 357db96d56Sopenharmony_ci 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci.. function:: bindtextdomain(domain, localedir=None) 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ci Bind the *domain* to the locale directory *localedir*. More concretely, 407db96d56Sopenharmony_ci :mod:`gettext` will look for binary :file:`.mo` files for the given domain using 417db96d56Sopenharmony_ci the path (on Unix): :file:`{localedir}/{language}/LC_MESSAGES/{domain}.mo`, where 427db96d56Sopenharmony_ci *language* is searched for in the environment variables :envvar:`LANGUAGE`, 437db96d56Sopenharmony_ci :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and :envvar:`LANG` respectively. 447db96d56Sopenharmony_ci 457db96d56Sopenharmony_ci If *localedir* is omitted or ``None``, then the current binding for *domain* is 467db96d56Sopenharmony_ci returned. [#]_ 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ci 497db96d56Sopenharmony_ci.. function:: textdomain(domain=None) 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ci Change or query the current global domain. If *domain* is ``None``, then the 527db96d56Sopenharmony_ci current global domain is returned, otherwise the global domain is set to 537db96d56Sopenharmony_ci *domain*, which is returned. 547db96d56Sopenharmony_ci 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ci.. index:: single: _ (underscore); gettext 577db96d56Sopenharmony_ci.. function:: gettext(message) 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci Return the localized translation of *message*, based on the current global 607db96d56Sopenharmony_ci domain, language, and locale directory. This function is usually aliased as 617db96d56Sopenharmony_ci :func:`_` in the local namespace (see examples below). 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ci 647db96d56Sopenharmony_ci.. function:: dgettext(domain, message) 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ci Like :func:`.gettext`, but look the message up in the specified *domain*. 677db96d56Sopenharmony_ci 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci.. function:: ngettext(singular, plural, n) 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci Like :func:`.gettext`, but consider plural forms. If a translation is found, 727db96d56Sopenharmony_ci apply the plural formula to *n*, and return the resulting message (some 737db96d56Sopenharmony_ci languages have more than two plural forms). If no translation is found, return 747db96d56Sopenharmony_ci *singular* if *n* is 1; return *plural* otherwise. 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ci The Plural formula is taken from the catalog header. It is a C or Python 777db96d56Sopenharmony_ci expression that has a free variable *n*; the expression evaluates to the index 787db96d56Sopenharmony_ci of the plural in the catalog. See 797db96d56Sopenharmony_ci `the GNU gettext documentation <https://www.gnu.org/software/gettext/manual/gettext.html>`__ 807db96d56Sopenharmony_ci for the precise syntax to be used in :file:`.po` files and the 817db96d56Sopenharmony_ci formulas for a variety of languages. 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ci 847db96d56Sopenharmony_ci.. function:: dngettext(domain, singular, plural, n) 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci Like :func:`ngettext`, but look the message up in the specified *domain*. 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ci 897db96d56Sopenharmony_ci.. function:: pgettext(context, message) 907db96d56Sopenharmony_ci.. function:: dpgettext(domain, context, message) 917db96d56Sopenharmony_ci.. function:: npgettext(context, singular, plural, n) 927db96d56Sopenharmony_ci.. function:: dnpgettext(domain, context, singular, plural, n) 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ci Similar to the corresponding functions without the ``p`` in the prefix (that 957db96d56Sopenharmony_ci is, :func:`gettext`, :func:`dgettext`, :func:`ngettext`, :func:`dngettext`), 967db96d56Sopenharmony_ci but the translation is restricted to the given message *context*. 977db96d56Sopenharmony_ci 987db96d56Sopenharmony_ci .. versionadded:: 3.8 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ci 1017db96d56Sopenharmony_ciNote that GNU :program:`gettext` also defines a :func:`dcgettext` method, but 1027db96d56Sopenharmony_cithis was deemed not useful and so it is currently unimplemented. 1037db96d56Sopenharmony_ci 1047db96d56Sopenharmony_ciHere's an example of typical usage for this API:: 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci import gettext 1077db96d56Sopenharmony_ci gettext.bindtextdomain('myapplication', '/path/to/my/language/directory') 1087db96d56Sopenharmony_ci gettext.textdomain('myapplication') 1097db96d56Sopenharmony_ci _ = gettext.gettext 1107db96d56Sopenharmony_ci # ... 1117db96d56Sopenharmony_ci print(_('This is a translatable string.')) 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ciClass-based API 1157db96d56Sopenharmony_ci--------------- 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ciThe class-based API of the :mod:`gettext` module gives you more flexibility and 1187db96d56Sopenharmony_cigreater convenience than the GNU :program:`gettext` API. It is the recommended 1197db96d56Sopenharmony_ciway of localizing your Python applications and modules. :mod:`!gettext` defines 1207db96d56Sopenharmony_cia :class:`GNUTranslations` class which implements the parsing of GNU :file:`.mo` format 1217db96d56Sopenharmony_cifiles, and has methods for returning strings. Instances of this class can also 1227db96d56Sopenharmony_ciinstall themselves in the built-in namespace as the function :func:`_`. 1237db96d56Sopenharmony_ci 1247db96d56Sopenharmony_ci 1257db96d56Sopenharmony_ci.. function:: find(domain, localedir=None, languages=None, all=False) 1267db96d56Sopenharmony_ci 1277db96d56Sopenharmony_ci This function implements the standard :file:`.mo` file search algorithm. It 1287db96d56Sopenharmony_ci takes a *domain*, identical to what :func:`textdomain` takes. Optional 1297db96d56Sopenharmony_ci *localedir* is as in :func:`bindtextdomain`. Optional *languages* is a list of 1307db96d56Sopenharmony_ci strings, where each string is a language code. 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ci If *localedir* is not given, then the default system locale directory is used. 1337db96d56Sopenharmony_ci [#]_ If *languages* is not given, then the following environment variables are 1347db96d56Sopenharmony_ci searched: :envvar:`LANGUAGE`, :envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and 1357db96d56Sopenharmony_ci :envvar:`LANG`. The first one returning a non-empty value is used for the 1367db96d56Sopenharmony_ci *languages* variable. The environment variables should contain a colon separated 1377db96d56Sopenharmony_ci list of languages, which will be split on the colon to produce the expected list 1387db96d56Sopenharmony_ci of language code strings. 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ci :func:`find` then expands and normalizes the languages, and then iterates 1417db96d56Sopenharmony_ci through them, searching for an existing file built of these components: 1427db96d56Sopenharmony_ci 1437db96d56Sopenharmony_ci :file:`{localedir}/{language}/LC_MESSAGES/{domain}.mo` 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci The first such file name that exists is returned by :func:`find`. If no such 1467db96d56Sopenharmony_ci file is found, then ``None`` is returned. If *all* is given, it returns a list 1477db96d56Sopenharmony_ci of all file names, in the order in which they appear in the languages list or 1487db96d56Sopenharmony_ci the environment variables. 1497db96d56Sopenharmony_ci 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ci.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False) 1527db96d56Sopenharmony_ci 1537db96d56Sopenharmony_ci Return a :class:`*Translations` instance based on the *domain*, *localedir*, 1547db96d56Sopenharmony_ci and *languages*, which are first passed to :func:`find` to get a list of the 1557db96d56Sopenharmony_ci associated :file:`.mo` file paths. Instances with identical :file:`.mo` file 1567db96d56Sopenharmony_ci names are cached. The actual class instantiated is *class_* if 1577db96d56Sopenharmony_ci provided, otherwise :class:`GNUTranslations`. The class's constructor must 1587db96d56Sopenharmony_ci take a single :term:`file object` argument. If provided, *codeset* will change 1597db96d56Sopenharmony_ci the charset used to encode translated strings in the 1607db96d56Sopenharmony_ci :meth:`~NullTranslations.lgettext` and :meth:`~NullTranslations.lngettext` 1617db96d56Sopenharmony_ci methods. 1627db96d56Sopenharmony_ci 1637db96d56Sopenharmony_ci If multiple files are found, later files are used as fallbacks for earlier ones. 1647db96d56Sopenharmony_ci To allow setting the fallback, :func:`copy.copy` is used to clone each 1657db96d56Sopenharmony_ci translation object from the cache; the actual instance data is still shared with 1667db96d56Sopenharmony_ci the cache. 1677db96d56Sopenharmony_ci 1687db96d56Sopenharmony_ci If no :file:`.mo` file is found, this function raises :exc:`OSError` if 1697db96d56Sopenharmony_ci *fallback* is false (which is the default), and returns a 1707db96d56Sopenharmony_ci :class:`NullTranslations` instance if *fallback* is true. 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ci .. versionchanged:: 3.3 1737db96d56Sopenharmony_ci :exc:`IOError` used to be raised instead of :exc:`OSError`. 1747db96d56Sopenharmony_ci 1757db96d56Sopenharmony_ci .. versionchanged:: 3.11 1767db96d56Sopenharmony_ci *codeset* parameter is removed. 1777db96d56Sopenharmony_ci 1787db96d56Sopenharmony_ci.. function:: install(domain, localedir=None, *, names=None) 1797db96d56Sopenharmony_ci 1807db96d56Sopenharmony_ci This installs the function :func:`_` in Python's builtins namespace, based on 1817db96d56Sopenharmony_ci *domain* and *localedir* which are passed to the function :func:`translation`. 1827db96d56Sopenharmony_ci 1837db96d56Sopenharmony_ci For the *names* parameter, please see the description of the translation 1847db96d56Sopenharmony_ci object's :meth:`~NullTranslations.install` method. 1857db96d56Sopenharmony_ci 1867db96d56Sopenharmony_ci As seen below, you usually mark the strings in your application that are 1877db96d56Sopenharmony_ci candidates for translation, by wrapping them in a call to the :func:`_` 1887db96d56Sopenharmony_ci function, like this:: 1897db96d56Sopenharmony_ci 1907db96d56Sopenharmony_ci print(_('This string will be translated.')) 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ci For convenience, you want the :func:`_` function to be installed in Python's 1937db96d56Sopenharmony_ci builtins namespace, so it is easily accessible in all modules of your 1947db96d56Sopenharmony_ci application. 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_ci .. versionchanged:: 3.11 1977db96d56Sopenharmony_ci *names* is now a keyword-only parameter. 1987db96d56Sopenharmony_ci 1997db96d56Sopenharmony_ciThe :class:`NullTranslations` class 2007db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2017db96d56Sopenharmony_ci 2027db96d56Sopenharmony_ciTranslation classes are what actually implement the translation of original 2037db96d56Sopenharmony_cisource file message strings to translated message strings. The base class used 2047db96d56Sopenharmony_ciby all translation classes is :class:`NullTranslations`; this provides the basic 2057db96d56Sopenharmony_ciinterface you can use to write your own specialized translation classes. Here 2067db96d56Sopenharmony_ciare the methods of :class:`!NullTranslations`: 2077db96d56Sopenharmony_ci 2087db96d56Sopenharmony_ci 2097db96d56Sopenharmony_ci.. class:: NullTranslations(fp=None) 2107db96d56Sopenharmony_ci 2117db96d56Sopenharmony_ci Takes an optional :term:`file object` *fp*, which is ignored by the base class. 2127db96d56Sopenharmony_ci Initializes "protected" instance variables *_info* and *_charset* which are set 2137db96d56Sopenharmony_ci by derived classes, as well as *_fallback*, which is set through 2147db96d56Sopenharmony_ci :meth:`add_fallback`. It then calls ``self._parse(fp)`` if *fp* is not 2157db96d56Sopenharmony_ci ``None``. 2167db96d56Sopenharmony_ci 2177db96d56Sopenharmony_ci .. method:: _parse(fp) 2187db96d56Sopenharmony_ci 2197db96d56Sopenharmony_ci No-op in the base class, this method takes file object *fp*, and reads 2207db96d56Sopenharmony_ci the data from the file, initializing its message catalog. If you have an 2217db96d56Sopenharmony_ci unsupported message catalog file format, you should override this method 2227db96d56Sopenharmony_ci to parse your format. 2237db96d56Sopenharmony_ci 2247db96d56Sopenharmony_ci 2257db96d56Sopenharmony_ci .. method:: add_fallback(fallback) 2267db96d56Sopenharmony_ci 2277db96d56Sopenharmony_ci Add *fallback* as the fallback object for the current translation object. 2287db96d56Sopenharmony_ci A translation object should consult the fallback if it cannot provide a 2297db96d56Sopenharmony_ci translation for a given message. 2307db96d56Sopenharmony_ci 2317db96d56Sopenharmony_ci 2327db96d56Sopenharmony_ci .. method:: gettext(message) 2337db96d56Sopenharmony_ci 2347db96d56Sopenharmony_ci If a fallback has been set, forward :meth:`!gettext` to the fallback. 2357db96d56Sopenharmony_ci Otherwise, return *message*. Overridden in derived classes. 2367db96d56Sopenharmony_ci 2377db96d56Sopenharmony_ci 2387db96d56Sopenharmony_ci .. method:: ngettext(singular, plural, n) 2397db96d56Sopenharmony_ci 2407db96d56Sopenharmony_ci If a fallback has been set, forward :meth:`!ngettext` to the fallback. 2417db96d56Sopenharmony_ci Otherwise, return *singular* if *n* is 1; return *plural* otherwise. 2427db96d56Sopenharmony_ci Overridden in derived classes. 2437db96d56Sopenharmony_ci 2447db96d56Sopenharmony_ci 2457db96d56Sopenharmony_ci .. method:: pgettext(context, message) 2467db96d56Sopenharmony_ci 2477db96d56Sopenharmony_ci If a fallback has been set, forward :meth:`pgettext` to the fallback. 2487db96d56Sopenharmony_ci Otherwise, return the translated message. Overridden in derived classes. 2497db96d56Sopenharmony_ci 2507db96d56Sopenharmony_ci .. versionadded:: 3.8 2517db96d56Sopenharmony_ci 2527db96d56Sopenharmony_ci 2537db96d56Sopenharmony_ci .. method:: npgettext(context, singular, plural, n) 2547db96d56Sopenharmony_ci 2557db96d56Sopenharmony_ci If a fallback has been set, forward :meth:`npgettext` to the fallback. 2567db96d56Sopenharmony_ci Otherwise, return the translated message. Overridden in derived classes. 2577db96d56Sopenharmony_ci 2587db96d56Sopenharmony_ci .. versionadded:: 3.8 2597db96d56Sopenharmony_ci 2607db96d56Sopenharmony_ci 2617db96d56Sopenharmony_ci .. method:: info() 2627db96d56Sopenharmony_ci 2637db96d56Sopenharmony_ci Return the "protected" :attr:`_info` variable, a dictionary containing 2647db96d56Sopenharmony_ci the metadata found in the message catalog file. 2657db96d56Sopenharmony_ci 2667db96d56Sopenharmony_ci 2677db96d56Sopenharmony_ci .. method:: charset() 2687db96d56Sopenharmony_ci 2697db96d56Sopenharmony_ci Return the encoding of the message catalog file. 2707db96d56Sopenharmony_ci 2717db96d56Sopenharmony_ci 2727db96d56Sopenharmony_ci .. method:: install(names=None) 2737db96d56Sopenharmony_ci 2747db96d56Sopenharmony_ci This method installs :meth:`.gettext` into the built-in namespace, 2757db96d56Sopenharmony_ci binding it to ``_``. 2767db96d56Sopenharmony_ci 2777db96d56Sopenharmony_ci If the *names* parameter is given, it must be a sequence containing the 2787db96d56Sopenharmony_ci names of functions you want to install in the builtins namespace in 2797db96d56Sopenharmony_ci addition to :func:`_`. Supported names are ``'gettext'``, ``'ngettext'``, 2807db96d56Sopenharmony_ci ``'pgettext'``, ``'npgettext'``, ``'lgettext'``, and ``'lngettext'``. 2817db96d56Sopenharmony_ci 2827db96d56Sopenharmony_ci Note that this is only one way, albeit the most convenient way, to make 2837db96d56Sopenharmony_ci the :func:`_` function available to your application. Because it affects 2847db96d56Sopenharmony_ci the entire application globally, and specifically the built-in namespace, 2857db96d56Sopenharmony_ci localized modules should never install :func:`_`. Instead, they should use 2867db96d56Sopenharmony_ci this code to make :func:`_` available to their module:: 2877db96d56Sopenharmony_ci 2887db96d56Sopenharmony_ci import gettext 2897db96d56Sopenharmony_ci t = gettext.translation('mymodule', ...) 2907db96d56Sopenharmony_ci _ = t.gettext 2917db96d56Sopenharmony_ci 2927db96d56Sopenharmony_ci This puts :func:`_` only in the module's global namespace and so only 2937db96d56Sopenharmony_ci affects calls within this module. 2947db96d56Sopenharmony_ci 2957db96d56Sopenharmony_ci .. versionchanged:: 3.8 2967db96d56Sopenharmony_ci Added ``'pgettext'`` and ``'npgettext'``. 2977db96d56Sopenharmony_ci 2987db96d56Sopenharmony_ci 2997db96d56Sopenharmony_ciThe :class:`GNUTranslations` class 3007db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3017db96d56Sopenharmony_ci 3027db96d56Sopenharmony_ciThe :mod:`gettext` module provides one additional class derived from 3037db96d56Sopenharmony_ci:class:`NullTranslations`: :class:`GNUTranslations`. This class overrides 3047db96d56Sopenharmony_ci:meth:`_parse` to enable reading GNU :program:`gettext` format :file:`.mo` files 3057db96d56Sopenharmony_ciin both big-endian and little-endian format. 3067db96d56Sopenharmony_ci 3077db96d56Sopenharmony_ci:class:`GNUTranslations` parses optional metadata out of the translation 3087db96d56Sopenharmony_cicatalog. It is convention with GNU :program:`gettext` to include metadata as 3097db96d56Sopenharmony_cithe translation for the empty string. This metadata is in :rfc:`822`\ -style 3107db96d56Sopenharmony_ci``key: value`` pairs, and should contain the ``Project-Id-Version`` key. If the 3117db96d56Sopenharmony_cikey ``Content-Type`` is found, then the ``charset`` property is used to 3127db96d56Sopenharmony_ciinitialize the "protected" :attr:`_charset` instance variable, defaulting to 3137db96d56Sopenharmony_ci``None`` if not found. If the charset encoding is specified, then all message 3147db96d56Sopenharmony_ciids and message strings read from the catalog are converted to Unicode using 3157db96d56Sopenharmony_cithis encoding, else ASCII is assumed. 3167db96d56Sopenharmony_ci 3177db96d56Sopenharmony_ciSince message ids are read as Unicode strings too, all :meth:`*gettext` methods 3187db96d56Sopenharmony_ciwill assume message ids as Unicode strings, not byte strings. 3197db96d56Sopenharmony_ci 3207db96d56Sopenharmony_ciThe entire set of key/value pairs are placed into a dictionary and set as the 3217db96d56Sopenharmony_ci"protected" :attr:`_info` instance variable. 3227db96d56Sopenharmony_ci 3237db96d56Sopenharmony_ciIf the :file:`.mo` file's magic number is invalid, the major version number is 3247db96d56Sopenharmony_ciunexpected, or if other problems occur while reading the file, instantiating a 3257db96d56Sopenharmony_ci:class:`GNUTranslations` class can raise :exc:`OSError`. 3267db96d56Sopenharmony_ci 3277db96d56Sopenharmony_ci.. class:: GNUTranslations 3287db96d56Sopenharmony_ci 3297db96d56Sopenharmony_ci The following methods are overridden from the base class implementation: 3307db96d56Sopenharmony_ci 3317db96d56Sopenharmony_ci .. method:: gettext(message) 3327db96d56Sopenharmony_ci 3337db96d56Sopenharmony_ci Look up the *message* id in the catalog and return the corresponding message 3347db96d56Sopenharmony_ci string, as a Unicode string. If there is no entry in the catalog for the 3357db96d56Sopenharmony_ci *message* id, and a fallback has been set, the look up is forwarded to the 3367db96d56Sopenharmony_ci fallback's :meth:`~NullTranslations.gettext` method. Otherwise, the 3377db96d56Sopenharmony_ci *message* id is returned. 3387db96d56Sopenharmony_ci 3397db96d56Sopenharmony_ci 3407db96d56Sopenharmony_ci .. method:: ngettext(singular, plural, n) 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ci Do a plural-forms lookup of a message id. *singular* is used as the message id 3437db96d56Sopenharmony_ci for purposes of lookup in the catalog, while *n* is used to determine which 3447db96d56Sopenharmony_ci plural form to use. The returned message string is a Unicode string. 3457db96d56Sopenharmony_ci 3467db96d56Sopenharmony_ci If the message id is not found in the catalog, and a fallback is specified, 3477db96d56Sopenharmony_ci the request is forwarded to the fallback's :meth:`~NullTranslations.ngettext` 3487db96d56Sopenharmony_ci method. Otherwise, when *n* is 1 *singular* is returned, and *plural* is 3497db96d56Sopenharmony_ci returned in all other cases. 3507db96d56Sopenharmony_ci 3517db96d56Sopenharmony_ci Here is an example:: 3527db96d56Sopenharmony_ci 3537db96d56Sopenharmony_ci n = len(os.listdir('.')) 3547db96d56Sopenharmony_ci cat = GNUTranslations(somefile) 3557db96d56Sopenharmony_ci message = cat.ngettext( 3567db96d56Sopenharmony_ci 'There is %(num)d file in this directory', 3577db96d56Sopenharmony_ci 'There are %(num)d files in this directory', 3587db96d56Sopenharmony_ci n) % {'num': n} 3597db96d56Sopenharmony_ci 3607db96d56Sopenharmony_ci 3617db96d56Sopenharmony_ci .. method:: pgettext(context, message) 3627db96d56Sopenharmony_ci 3637db96d56Sopenharmony_ci Look up the *context* and *message* id in the catalog and return the 3647db96d56Sopenharmony_ci corresponding message string, as a Unicode string. If there is no 3657db96d56Sopenharmony_ci entry in the catalog for the *message* id and *context*, and a fallback 3667db96d56Sopenharmony_ci has been set, the look up is forwarded to the fallback's 3677db96d56Sopenharmony_ci :meth:`pgettext` method. Otherwise, the *message* id is returned. 3687db96d56Sopenharmony_ci 3697db96d56Sopenharmony_ci .. versionadded:: 3.8 3707db96d56Sopenharmony_ci 3717db96d56Sopenharmony_ci 3727db96d56Sopenharmony_ci .. method:: npgettext(context, singular, plural, n) 3737db96d56Sopenharmony_ci 3747db96d56Sopenharmony_ci Do a plural-forms lookup of a message id. *singular* is used as the 3757db96d56Sopenharmony_ci message id for purposes of lookup in the catalog, while *n* is used to 3767db96d56Sopenharmony_ci determine which plural form to use. 3777db96d56Sopenharmony_ci 3787db96d56Sopenharmony_ci If the message id for *context* is not found in the catalog, and a 3797db96d56Sopenharmony_ci fallback is specified, the request is forwarded to the fallback's 3807db96d56Sopenharmony_ci :meth:`npgettext` method. Otherwise, when *n* is 1 *singular* is 3817db96d56Sopenharmony_ci returned, and *plural* is returned in all other cases. 3827db96d56Sopenharmony_ci 3837db96d56Sopenharmony_ci .. versionadded:: 3.8 3847db96d56Sopenharmony_ci 3857db96d56Sopenharmony_ci 3867db96d56Sopenharmony_ciSolaris message catalog support 3877db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 3887db96d56Sopenharmony_ci 3897db96d56Sopenharmony_ciThe Solaris operating system defines its own binary :file:`.mo` file format, but 3907db96d56Sopenharmony_cisince no documentation can be found on this format, it is not supported at this 3917db96d56Sopenharmony_citime. 3927db96d56Sopenharmony_ci 3937db96d56Sopenharmony_ci 3947db96d56Sopenharmony_ciThe Catalog constructor 3957db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^ 3967db96d56Sopenharmony_ci 3977db96d56Sopenharmony_ci.. index:: single: GNOME 3987db96d56Sopenharmony_ci 3997db96d56Sopenharmony_ciGNOME uses a version of the :mod:`gettext` module by James Henstridge, but this 4007db96d56Sopenharmony_civersion has a slightly different API. Its documented usage was:: 4017db96d56Sopenharmony_ci 4027db96d56Sopenharmony_ci import gettext 4037db96d56Sopenharmony_ci cat = gettext.Catalog(domain, localedir) 4047db96d56Sopenharmony_ci _ = cat.gettext 4057db96d56Sopenharmony_ci print(_('hello world')) 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ciFor compatibility with this older module, the function :func:`Catalog` is an 4087db96d56Sopenharmony_cialias for the :func:`translation` function described above. 4097db96d56Sopenharmony_ci 4107db96d56Sopenharmony_ciOne difference between this module and Henstridge's: his catalog objects 4117db96d56Sopenharmony_cisupported access through a mapping API, but this appears to be unused and so is 4127db96d56Sopenharmony_cinot currently supported. 4137db96d56Sopenharmony_ci 4147db96d56Sopenharmony_ci 4157db96d56Sopenharmony_ciInternationalizing your programs and modules 4167db96d56Sopenharmony_ci-------------------------------------------- 4177db96d56Sopenharmony_ci 4187db96d56Sopenharmony_ciInternationalization (I18N) refers to the operation by which a program is made 4197db96d56Sopenharmony_ciaware of multiple languages. Localization (L10N) refers to the adaptation of 4207db96d56Sopenharmony_ciyour program, once internationalized, to the local language and cultural habits. 4217db96d56Sopenharmony_ciIn order to provide multilingual messages for your Python programs, you need to 4227db96d56Sopenharmony_citake the following steps: 4237db96d56Sopenharmony_ci 4247db96d56Sopenharmony_ci#. prepare your program or module by specially marking translatable strings 4257db96d56Sopenharmony_ci 4267db96d56Sopenharmony_ci#. run a suite of tools over your marked files to generate raw messages catalogs 4277db96d56Sopenharmony_ci 4287db96d56Sopenharmony_ci#. create language-specific translations of the message catalogs 4297db96d56Sopenharmony_ci 4307db96d56Sopenharmony_ci#. use the :mod:`gettext` module so that message strings are properly translated 4317db96d56Sopenharmony_ci 4327db96d56Sopenharmony_ciIn order to prepare your code for I18N, you need to look at all the strings in 4337db96d56Sopenharmony_ciyour files. Any string that needs to be translated should be marked by wrapping 4347db96d56Sopenharmony_ciit in ``_('...')`` --- that is, a call to the function :func:`_`. For example:: 4357db96d56Sopenharmony_ci 4367db96d56Sopenharmony_ci filename = 'mylog.txt' 4377db96d56Sopenharmony_ci message = _('writing a log message') 4387db96d56Sopenharmony_ci with open(filename, 'w') as fp: 4397db96d56Sopenharmony_ci fp.write(message) 4407db96d56Sopenharmony_ci 4417db96d56Sopenharmony_ciIn this example, the string ``'writing a log message'`` is marked as a candidate 4427db96d56Sopenharmony_cifor translation, while the strings ``'mylog.txt'`` and ``'w'`` are not. 4437db96d56Sopenharmony_ci 4447db96d56Sopenharmony_ciThere are a few tools to extract the strings meant for translation. 4457db96d56Sopenharmony_ciThe original GNU :program:`gettext` only supported C or C++ source 4467db96d56Sopenharmony_cicode but its extended version :program:`xgettext` scans code written 4477db96d56Sopenharmony_ciin a number of languages, including Python, to find strings marked as 4487db96d56Sopenharmony_citranslatable. `Babel <https://babel.pocoo.org/>`__ is a Python 4497db96d56Sopenharmony_ciinternationalization library that includes a :file:`pybabel` script to 4507db96d56Sopenharmony_ciextract and compile message catalogs. François Pinard's program 4517db96d56Sopenharmony_cicalled :program:`xpot` does a similar job and is available as part of 4527db96d56Sopenharmony_cihis `po-utils package <https://github.com/pinard/po-utils>`__. 4537db96d56Sopenharmony_ci 4547db96d56Sopenharmony_ci(Python also includes pure-Python versions of these programs, called 4557db96d56Sopenharmony_ci:program:`pygettext.py` and :program:`msgfmt.py`; some Python distributions 4567db96d56Sopenharmony_ciwill install them for you. :program:`pygettext.py` is similar to 4577db96d56Sopenharmony_ci:program:`xgettext`, but only understands Python source code and 4587db96d56Sopenharmony_cicannot handle other programming languages such as C or C++. 4597db96d56Sopenharmony_ci:program:`pygettext.py` supports a command-line interface similar to 4607db96d56Sopenharmony_ci:program:`xgettext`; for details on its use, run ``pygettext.py 4617db96d56Sopenharmony_ci--help``. :program:`msgfmt.py` is binary compatible with GNU 4627db96d56Sopenharmony_ci:program:`msgfmt`. With these two programs, you may not need the GNU 4637db96d56Sopenharmony_ci:program:`gettext` package to internationalize your Python 4647db96d56Sopenharmony_ciapplications.) 4657db96d56Sopenharmony_ci 4667db96d56Sopenharmony_ci:program:`xgettext`, :program:`pygettext`, and similar tools generate 4677db96d56Sopenharmony_ci:file:`.po` files that are message catalogs. They are structured 4687db96d56Sopenharmony_cihuman-readable files that contain every marked string in the source 4697db96d56Sopenharmony_cicode, along with a placeholder for the translated versions of these 4707db96d56Sopenharmony_cistrings. 4717db96d56Sopenharmony_ci 4727db96d56Sopenharmony_ciCopies of these :file:`.po` files are then handed over to the 4737db96d56Sopenharmony_ciindividual human translators who write translations for every 4747db96d56Sopenharmony_cisupported natural language. They send back the completed 4757db96d56Sopenharmony_cilanguage-specific versions as a :file:`<language-name>.po` file that's 4767db96d56Sopenharmony_cicompiled into a machine-readable :file:`.mo` binary catalog file using 4777db96d56Sopenharmony_cithe :program:`msgfmt` program. The :file:`.mo` files are used by the 4787db96d56Sopenharmony_ci:mod:`gettext` module for the actual translation processing at 4797db96d56Sopenharmony_cirun-time. 4807db96d56Sopenharmony_ci 4817db96d56Sopenharmony_ciHow you use the :mod:`gettext` module in your code depends on whether you are 4827db96d56Sopenharmony_ciinternationalizing a single module or your entire application. The next two 4837db96d56Sopenharmony_cisections will discuss each case. 4847db96d56Sopenharmony_ci 4857db96d56Sopenharmony_ci 4867db96d56Sopenharmony_ciLocalizing your module 4877db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^ 4887db96d56Sopenharmony_ci 4897db96d56Sopenharmony_ciIf you are localizing your module, you must take care not to make global 4907db96d56Sopenharmony_cichanges, e.g. to the built-in namespace. You should not use the GNU :program:`gettext` 4917db96d56Sopenharmony_ciAPI but instead the class-based API. 4927db96d56Sopenharmony_ci 4937db96d56Sopenharmony_ciLet's say your module is called "spam" and the module's various natural language 4947db96d56Sopenharmony_citranslation :file:`.mo` files reside in :file:`/usr/share/locale` in GNU 4957db96d56Sopenharmony_ci:program:`gettext` format. Here's what you would put at the top of your 4967db96d56Sopenharmony_cimodule:: 4977db96d56Sopenharmony_ci 4987db96d56Sopenharmony_ci import gettext 4997db96d56Sopenharmony_ci t = gettext.translation('spam', '/usr/share/locale') 5007db96d56Sopenharmony_ci _ = t.gettext 5017db96d56Sopenharmony_ci 5027db96d56Sopenharmony_ci 5037db96d56Sopenharmony_ciLocalizing your application 5047db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^ 5057db96d56Sopenharmony_ci 5067db96d56Sopenharmony_ciIf you are localizing your application, you can install the :func:`_` function 5077db96d56Sopenharmony_ciglobally into the built-in namespace, usually in the main driver file of your 5087db96d56Sopenharmony_ciapplication. This will let all your application-specific files just use 5097db96d56Sopenharmony_ci``_('...')`` without having to explicitly install it in each file. 5107db96d56Sopenharmony_ci 5117db96d56Sopenharmony_ciIn the simple case then, you need only add the following bit of code to the main 5127db96d56Sopenharmony_cidriver file of your application:: 5137db96d56Sopenharmony_ci 5147db96d56Sopenharmony_ci import gettext 5157db96d56Sopenharmony_ci gettext.install('myapplication') 5167db96d56Sopenharmony_ci 5177db96d56Sopenharmony_ciIf you need to set the locale directory, you can pass it into the 5187db96d56Sopenharmony_ci:func:`install` function:: 5197db96d56Sopenharmony_ci 5207db96d56Sopenharmony_ci import gettext 5217db96d56Sopenharmony_ci gettext.install('myapplication', '/usr/share/locale') 5227db96d56Sopenharmony_ci 5237db96d56Sopenharmony_ci 5247db96d56Sopenharmony_ciChanging languages on the fly 5257db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 5267db96d56Sopenharmony_ci 5277db96d56Sopenharmony_ciIf your program needs to support many languages at the same time, you may want 5287db96d56Sopenharmony_cito create multiple translation instances and then switch between them 5297db96d56Sopenharmony_ciexplicitly, like so:: 5307db96d56Sopenharmony_ci 5317db96d56Sopenharmony_ci import gettext 5327db96d56Sopenharmony_ci 5337db96d56Sopenharmony_ci lang1 = gettext.translation('myapplication', languages=['en']) 5347db96d56Sopenharmony_ci lang2 = gettext.translation('myapplication', languages=['fr']) 5357db96d56Sopenharmony_ci lang3 = gettext.translation('myapplication', languages=['de']) 5367db96d56Sopenharmony_ci 5377db96d56Sopenharmony_ci # start by using language1 5387db96d56Sopenharmony_ci lang1.install() 5397db96d56Sopenharmony_ci 5407db96d56Sopenharmony_ci # ... time goes by, user selects language 2 5417db96d56Sopenharmony_ci lang2.install() 5427db96d56Sopenharmony_ci 5437db96d56Sopenharmony_ci # ... more time goes by, user selects language 3 5447db96d56Sopenharmony_ci lang3.install() 5457db96d56Sopenharmony_ci 5467db96d56Sopenharmony_ci 5477db96d56Sopenharmony_ciDeferred translations 5487db96d56Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^ 5497db96d56Sopenharmony_ci 5507db96d56Sopenharmony_ciIn most coding situations, strings are translated where they are coded. 5517db96d56Sopenharmony_ciOccasionally however, you need to mark strings for translation, but defer actual 5527db96d56Sopenharmony_citranslation until later. A classic example is:: 5537db96d56Sopenharmony_ci 5547db96d56Sopenharmony_ci animals = ['mollusk', 5557db96d56Sopenharmony_ci 'albatross', 5567db96d56Sopenharmony_ci 'rat', 5577db96d56Sopenharmony_ci 'penguin', 5587db96d56Sopenharmony_ci 'python', ] 5597db96d56Sopenharmony_ci # ... 5607db96d56Sopenharmony_ci for a in animals: 5617db96d56Sopenharmony_ci print(a) 5627db96d56Sopenharmony_ci 5637db96d56Sopenharmony_ciHere, you want to mark the strings in the ``animals`` list as being 5647db96d56Sopenharmony_citranslatable, but you don't actually want to translate them until they are 5657db96d56Sopenharmony_ciprinted. 5667db96d56Sopenharmony_ci 5677db96d56Sopenharmony_ciHere is one way you can handle this situation:: 5687db96d56Sopenharmony_ci 5697db96d56Sopenharmony_ci def _(message): return message 5707db96d56Sopenharmony_ci 5717db96d56Sopenharmony_ci animals = [_('mollusk'), 5727db96d56Sopenharmony_ci _('albatross'), 5737db96d56Sopenharmony_ci _('rat'), 5747db96d56Sopenharmony_ci _('penguin'), 5757db96d56Sopenharmony_ci _('python'), ] 5767db96d56Sopenharmony_ci 5777db96d56Sopenharmony_ci del _ 5787db96d56Sopenharmony_ci 5797db96d56Sopenharmony_ci # ... 5807db96d56Sopenharmony_ci for a in animals: 5817db96d56Sopenharmony_ci print(_(a)) 5827db96d56Sopenharmony_ci 5837db96d56Sopenharmony_ciThis works because the dummy definition of :func:`_` simply returns the string 5847db96d56Sopenharmony_ciunchanged. And this dummy definition will temporarily override any definition 5857db96d56Sopenharmony_ciof :func:`_` in the built-in namespace (until the :keyword:`del` command). Take 5867db96d56Sopenharmony_cicare, though if you have a previous definition of :func:`_` in the local 5877db96d56Sopenharmony_cinamespace. 5887db96d56Sopenharmony_ci 5897db96d56Sopenharmony_ciNote that the second use of :func:`_` will not identify "a" as being 5907db96d56Sopenharmony_citranslatable to the :program:`gettext` program, because the parameter 5917db96d56Sopenharmony_ciis not a string literal. 5927db96d56Sopenharmony_ci 5937db96d56Sopenharmony_ciAnother way to handle this is with the following example:: 5947db96d56Sopenharmony_ci 5957db96d56Sopenharmony_ci def N_(message): return message 5967db96d56Sopenharmony_ci 5977db96d56Sopenharmony_ci animals = [N_('mollusk'), 5987db96d56Sopenharmony_ci N_('albatross'), 5997db96d56Sopenharmony_ci N_('rat'), 6007db96d56Sopenharmony_ci N_('penguin'), 6017db96d56Sopenharmony_ci N_('python'), ] 6027db96d56Sopenharmony_ci 6037db96d56Sopenharmony_ci # ... 6047db96d56Sopenharmony_ci for a in animals: 6057db96d56Sopenharmony_ci print(_(a)) 6067db96d56Sopenharmony_ci 6077db96d56Sopenharmony_ciIn this case, you are marking translatable strings with the function 6087db96d56Sopenharmony_ci:func:`N_`, which won't conflict with any definition of :func:`_`. 6097db96d56Sopenharmony_ciHowever, you will need to teach your message extraction program to 6107db96d56Sopenharmony_cilook for translatable strings marked with :func:`N_`. :program:`xgettext`, 6117db96d56Sopenharmony_ci:program:`pygettext`, ``pybabel extract``, and :program:`xpot` all 6127db96d56Sopenharmony_cisupport this through the use of the :option:`!-k` command-line switch. 6137db96d56Sopenharmony_ciThe choice of :func:`N_` here is totally arbitrary; it could have just 6147db96d56Sopenharmony_cias easily been :func:`MarkThisStringForTranslation`. 6157db96d56Sopenharmony_ci 6167db96d56Sopenharmony_ci 6177db96d56Sopenharmony_ciAcknowledgements 6187db96d56Sopenharmony_ci---------------- 6197db96d56Sopenharmony_ci 6207db96d56Sopenharmony_ciThe following people contributed code, feedback, design suggestions, previous 6217db96d56Sopenharmony_ciimplementations, and valuable experience to the creation of this module: 6227db96d56Sopenharmony_ci 6237db96d56Sopenharmony_ci* Peter Funk 6247db96d56Sopenharmony_ci 6257db96d56Sopenharmony_ci* James Henstridge 6267db96d56Sopenharmony_ci 6277db96d56Sopenharmony_ci* Juan David Ibáñez Palomar 6287db96d56Sopenharmony_ci 6297db96d56Sopenharmony_ci* Marc-André Lemburg 6307db96d56Sopenharmony_ci 6317db96d56Sopenharmony_ci* Martin von Löwis 6327db96d56Sopenharmony_ci 6337db96d56Sopenharmony_ci* François Pinard 6347db96d56Sopenharmony_ci 6357db96d56Sopenharmony_ci* Barry Warsaw 6367db96d56Sopenharmony_ci 6377db96d56Sopenharmony_ci* Gustavo Niemeyer 6387db96d56Sopenharmony_ci 6397db96d56Sopenharmony_ci.. rubric:: Footnotes 6407db96d56Sopenharmony_ci 6417db96d56Sopenharmony_ci.. [#] The default locale directory is system dependent; for example, on RedHat Linux 6427db96d56Sopenharmony_ci it is :file:`/usr/share/locale`, but on Solaris it is :file:`/usr/lib/locale`. 6437db96d56Sopenharmony_ci The :mod:`gettext` module does not try to support these system dependent 6447db96d56Sopenharmony_ci defaults; instead its default is :file:`{sys.base_prefix}/share/locale` (see 6457db96d56Sopenharmony_ci :data:`sys.base_prefix`). For this reason, it is always best to call 6467db96d56Sopenharmony_ci :func:`bindtextdomain` with an explicit absolute path at the start of your 6477db96d56Sopenharmony_ci application. 6487db96d56Sopenharmony_ci 6497db96d56Sopenharmony_ci.. [#] See the footnote for :func:`bindtextdomain` above. 650