13
13
14
14
The :mod: `gettext ` module provides internationalization (I18N) and localization
15
15
(L10N) services for your Python modules and applications. It supports both the
16
- GNU `` gettext ` ` message catalog API and a higher level, class-based API that may
16
+ GNU :program: ` gettext ` message catalog API and a higher level, class-based API that may
17
17
be more appropriate for Python files. The interface described below allows you
18
18
to write your module and application messages in one natural language, and
19
19
provide a catalog of translated messages for running under different natural
@@ -38,7 +38,7 @@ class-based API instead.
38
38
39
39
Bind the *domain * to the locale directory *localedir *. More concretely,
40
40
:mod: `gettext ` will look for binary :file: `.mo ` files for the given domain using
41
- the path (on Unix): :file: `localedir/ language/LC_MESSAGES/domain.mo `, where
41
+ the path (on Unix): :file: `{ localedir } / { language } /LC_MESSAGES/{ domain } .mo `, where
42
42
*languages * is searched for in the environment variables :envvar: `LANGUAGE `,
43
43
:envvar: `LC_ALL `, :envvar: `LC_MESSAGES `, and :envvar: `LANG ` respectively.
44
44
@@ -138,17 +138,16 @@ Class-based API
138
138
The class-based API of the :mod: `gettext ` module gives you more flexibility and
139
139
greater convenience than the GNU :program: `gettext ` API. It is the recommended
140
140
way of localizing your Python applications and modules. :mod: `!gettext ` defines
141
- a "translations" class which implements the parsing of GNU :file: `.mo ` format
142
- files, and has methods for returning strings. Instances of this "translations"
143
- class can also install themselves in the built-in namespace as the function
144
- :func: `_ `.
141
+ a :class: `GNUTranslations ` class which implements the parsing of GNU :file: `.mo ` format
142
+ files, and has methods for returning strings. Instances of this class can also
143
+ install themselves in the built-in namespace as the function :func: `_ `.
145
144
146
145
147
146
.. function :: find(domain, localedir=None, languages=None, all=False)
148
147
149
148
This function implements the standard :file: `.mo ` file search algorithm. It
150
149
takes a *domain *, identical to what :func: `textdomain ` takes. Optional
151
- *localedir * is as in :func: `bindtextdomain ` Optional *languages * is a list of
150
+ *localedir * is as in :func: `bindtextdomain `. Optional *languages * is a list of
152
151
strings, where each string is a language code.
153
152
154
153
If *localedir * is not given, then the default system locale directory is used.
@@ -172,10 +171,10 @@ class can also install themselves in the built-in namespace as the function
172
171
173
172
.. function :: translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)
174
173
175
- Return a :class: `Translations ` instance based on the *domain *, *localedir *,
174
+ Return a :class: `* Translations ` instance based on the *domain *, *localedir *,
176
175
and *languages *, which are first passed to :func: `find ` to get a list of the
177
176
associated :file: `.mo ` file paths. Instances with identical :file: `.mo ` file
178
- names are cached. The actual class instantiated is either *class_ * if
177
+ names are cached. The actual class instantiated is *class_ * if
179
178
provided, otherwise :class: `GNUTranslations `. The class's constructor must
180
179
take a single :term: `file object ` argument. If provided, *codeset * will change
181
180
the charset used to encode translated strings in the
@@ -241,7 +240,7 @@ are the methods of :class:`!NullTranslations`:
241
240
242
241
.. method :: _parse(fp)
243
242
244
- No-op'd in the base class, this method takes file object *fp *, and reads
243
+ No-op in the base class, this method takes file object *fp *, and reads
245
244
the data from the file, initializing its message catalog. If you have an
246
245
unsupported message catalog file format, you should override this method
247
246
to parse your format.
@@ -285,7 +284,8 @@ are the methods of :class:`!NullTranslations`:
285
284
286
285
.. method :: info()
287
286
288
- Return the "protected" :attr: `_info ` variable.
287
+ Return the "protected" :attr: `_info ` variable, a dictionary containing
288
+ the metadata found in the message catalog file.
289
289
290
290
291
291
.. method :: charset()
@@ -340,15 +340,15 @@ The :mod:`gettext` module provides one additional class derived from
340
340
:meth: `_parse ` to enable reading GNU :program: `gettext ` format :file: `.mo ` files
341
341
in both big-endian and little-endian format.
342
342
343
- :class: `GNUTranslations ` parses optional meta-data out of the translation
344
- catalog. It is convention with GNU :program: `gettext ` to include meta-data as
345
- the translation for the empty string. This meta-data is in :rfc: `822 `\ -style
343
+ :class: `GNUTranslations ` parses optional metadata out of the translation
344
+ catalog. It is convention with GNU :program: `gettext ` to include metadata as
345
+ the translation for the empty string. This metadata is in :rfc: `822 `\ -style
346
346
``key: value `` pairs, and should contain the ``Project-Id-Version `` key. If the
347
347
key ``Content-Type `` is found, then the ``charset `` property is used to
348
348
initialize the "protected" :attr: `_charset ` instance variable, defaulting to
349
349
``None `` if not found. If the charset encoding is specified, then all message
350
350
ids and message strings read from the catalog are converted to Unicode using
351
- this encoding, else ASCII encoding is assumed.
351
+ this encoding, else ASCII is assumed.
352
352
353
353
Since message ids are read as Unicode strings too, all :meth: `*gettext ` methods
354
354
will assume message ids as Unicode strings, not byte strings.
@@ -452,7 +452,7 @@ take the following steps:
452
452
453
453
#. run a suite of tools over your marked files to generate raw messages catalogs
454
454
455
- #. create language specific translations of the message catalogs
455
+ #. create language- specific translations of the message catalogs
456
456
457
457
#. use the :mod: `gettext ` module so that message strings are properly translated
458
458
@@ -462,9 +462,8 @@ it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
462
462
463
463
filename = 'mylog.txt'
464
464
message = _('writing a log message')
465
- fp = open(filename, 'w')
466
- fp.write(message)
467
- fp.close()
465
+ with open(filename, 'w') as fp:
466
+ fp.write(message)
468
467
469
468
In this example, the string ``'writing a log message' `` is marked as a candidate
470
469
for translation, while the strings ``'mylog.txt' `` and ``'w' `` are not.
@@ -515,7 +514,7 @@ Localizing your module
515
514
^^^^^^^^^^^^^^^^^^^^^^
516
515
517
516
If you are localizing your module, you must take care not to make global
518
- changes, e.g. to the built-in namespace. You should not use the GNU `` gettext ` `
517
+ changes, e.g. to the built-in namespace. You should not use the GNU :program: ` gettext `
519
518
API but instead the class-based API.
520
519
521
520
Let's say your module is called "spam" and the module's various natural language
@@ -669,8 +668,9 @@ implementations, and valuable experience to the creation of this module:
669
668
.. [# ] The default locale directory is system dependent; for example, on RedHat Linux
670
669
it is :file: `/usr/share/locale `, but on Solaris it is :file: `/usr/lib/locale `.
671
670
The :mod: `gettext ` module does not try to support these system dependent
672
- defaults; instead its default is :file: `sys.prefix/share/locale `. For this
673
- reason, it is always best to call :func: `bindtextdomain ` with an explicit
674
- absolute path at the start of your application.
671
+ defaults; instead its default is :file: `{ sys.prefix } /share/locale ` (see
672
+ :data: `sys.prefix `). For this reason, it is always best to call
673
+ :func: `bindtextdomain ` with an explicit absolute path at the start of your
674
+ application.
675
675
676
676
.. [# ] See the footnote for :func: `bindtextdomain ` above.
0 commit comments