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
@@ -136,17 +136,16 @@ Class-based API
136
136
The class-based API of the :mod: `gettext ` module gives you more flexibility and
137
137
greater convenience than the GNU :program: `gettext ` API. It is the recommended
138
138
way of localizing your Python applications and modules. :mod: `!gettext ` defines
139
- a "translations" class which implements the parsing of GNU :file: `.mo ` format
140
- files, and has methods for returning strings. Instances of this "translations"
141
- class can also install themselves in the built-in namespace as the function
142
- :func: `_ `.
139
+ a :class: `GNUTranslations ` class which implements the parsing of GNU :file: `.mo ` format
140
+ files, and has methods for returning strings. Instances of this class can also
141
+ install themselves in the built-in namespace as the function :func: `_ `.
143
142
144
143
145
144
.. function :: find(domain, localedir=None, languages=None, all=False)
146
145
147
146
This function implements the standard :file: `.mo ` file search algorithm. It
148
147
takes a *domain *, identical to what :func: `textdomain ` takes. Optional
149
- *localedir * is as in :func: `bindtextdomain ` Optional *languages * is a list of
148
+ *localedir * is as in :func: `bindtextdomain `. Optional *languages * is a list of
150
149
strings, where each string is a language code.
151
150
152
151
If *localedir * is not given, then the default system locale directory is used.
@@ -170,10 +169,10 @@ class can also install themselves in the built-in namespace as the function
170
169
171
170
.. function :: translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)
172
171
173
- Return a :class: `Translations ` instance based on the *domain *, *localedir *,
172
+ Return a :class: `* Translations ` instance based on the *domain *, *localedir *,
174
173
and *languages *, which are first passed to :func: `find ` to get a list of the
175
174
associated :file: `.mo ` file paths. Instances with identical :file: `.mo ` file
176
- names are cached. The actual class instantiated is either *class_ * if
175
+ names are cached. The actual class instantiated is *class_ * if
177
176
provided, otherwise :class: `GNUTranslations `. The class's constructor must
178
177
take a single :term: `file object ` argument. If provided, *codeset * will change
179
178
the charset used to encode translated strings in the
@@ -233,7 +232,7 @@ are the methods of :class:`!NullTranslations`:
233
232
234
233
.. method :: _parse(fp)
235
234
236
- No-op'd in the base class, this method takes file object *fp *, and reads
235
+ No-op in the base class, this method takes file object *fp *, and reads
237
236
the data from the file, initializing its message catalog. If you have an
238
237
unsupported message catalog file format, you should override this method
239
238
to parse your format.
@@ -275,7 +274,8 @@ are the methods of :class:`!NullTranslations`:
275
274
276
275
.. method :: info()
277
276
278
- Return the "protected" :attr: `_info ` variable.
277
+ Return the "protected" :attr: `_info ` variable, a dictionary containing
278
+ the metadata found in the message catalog file.
279
279
280
280
281
281
.. method :: charset()
@@ -326,15 +326,15 @@ The :mod:`gettext` module provides one additional class derived from
326
326
:meth: `_parse ` to enable reading GNU :program: `gettext ` format :file: `.mo ` files
327
327
in both big-endian and little-endian format.
328
328
329
- :class: `GNUTranslations ` parses optional meta-data out of the translation
330
- catalog. It is convention with GNU :program: `gettext ` to include meta-data as
331
- the translation for the empty string. This meta-data is in :rfc: `822 `\ -style
329
+ :class: `GNUTranslations ` parses optional metadata out of the translation
330
+ catalog. It is convention with GNU :program: `gettext ` to include metadata as
331
+ the translation for the empty string. This metadata is in :rfc: `822 `\ -style
332
332
``key: value `` pairs, and should contain the ``Project-Id-Version `` key. If the
333
333
key ``Content-Type `` is found, then the ``charset `` property is used to
334
334
initialize the "protected" :attr: `_charset ` instance variable, defaulting to
335
335
``None `` if not found. If the charset encoding is specified, then all message
336
336
ids and message strings read from the catalog are converted to Unicode using
337
- this encoding, else ASCII encoding is assumed.
337
+ this encoding, else ASCII is assumed.
338
338
339
339
Since message ids are read as Unicode strings too, all :meth: `*gettext ` methods
340
340
will assume message ids as Unicode strings, not byte strings.
@@ -436,7 +436,7 @@ take the following steps:
436
436
437
437
#. run a suite of tools over your marked files to generate raw messages catalogs
438
438
439
- #. create language specific translations of the message catalogs
439
+ #. create language- specific translations of the message catalogs
440
440
441
441
#. use the :mod: `gettext ` module so that message strings are properly translated
442
442
@@ -446,9 +446,8 @@ it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
446
446
447
447
filename = 'mylog.txt'
448
448
message = _('writing a log message')
449
- fp = open(filename, 'w')
450
- fp.write(message)
451
- fp.close()
449
+ with open(filename, 'w') as fp:
450
+ fp.write(message)
452
451
453
452
In this example, the string ``'writing a log message' `` is marked as a candidate
454
453
for translation, while the strings ``'mylog.txt' `` and ``'w' `` are not.
@@ -499,7 +498,7 @@ Localizing your module
499
498
^^^^^^^^^^^^^^^^^^^^^^
500
499
501
500
If you are localizing your module, you must take care not to make global
502
- changes, e.g. to the built-in namespace. You should not use the GNU `` gettext ` `
501
+ changes, e.g. to the built-in namespace. You should not use the GNU :program: ` gettext `
503
502
API but instead the class-based API.
504
503
505
504
Let's say your module is called "spam" and the module's various natural language
@@ -653,8 +652,9 @@ implementations, and valuable experience to the creation of this module:
653
652
.. [# ] The default locale directory is system dependent; for example, on RedHat Linux
654
653
it is :file: `/usr/share/locale `, but on Solaris it is :file: `/usr/lib/locale `.
655
654
The :mod: `gettext ` module does not try to support these system dependent
656
- defaults; instead its default is :file: `sys.prefix/share/locale `. For this
657
- reason, it is always best to call :func: `bindtextdomain ` with an explicit
658
- absolute path at the start of your application.
655
+ defaults; instead its default is :file: `{ sys.prefix } /share/locale ` (see
656
+ :data: `sys.prefix `). For this reason, it is always best to call
657
+ :func: `bindtextdomain ` with an explicit absolute path at the start of your
658
+ application.
659
659
660
660
.. [# ] See the footnote for :func: `bindtextdomain ` above.
0 commit comments