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