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