Skip to content

Commit 98b360e

Browse files
[3.7] bpo-10536: Enhancements to gettext docs (GH-10324) (GH-13224)
(cherry picked from commit 55f3317) Co-authored-by: Stéphane Wirtel <[email protected]> https://bugs.python.org/issue10536
1 parent 6daaf3f commit 98b360e

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

Doc/library/gettext.rst

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
The :mod:`gettext` module provides internationalization (I18N) and localization
1515
(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
1717
be more appropriate for Python files. The interface described below allows you
1818
to write your module and application messages in one natural language, and
1919
provide a catalog of translated messages for running under different natural
@@ -38,7 +38,7 @@ class-based API instead.
3838

3939
Bind the *domain* to the locale directory *localedir*. More concretely,
4040
: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
4242
*languages* is searched for in the environment variables :envvar:`LANGUAGE`,
4343
:envvar:`LC_ALL`, :envvar:`LC_MESSAGES`, and :envvar:`LANG` respectively.
4444

@@ -136,17 +136,16 @@ Class-based API
136136
The class-based API of the :mod:`gettext` module gives you more flexibility and
137137
greater convenience than the GNU :program:`gettext` API. It is the recommended
138138
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:`_`.
143142

144143

145144
.. function:: find(domain, localedir=None, languages=None, all=False)
146145

147146
This function implements the standard :file:`.mo` file search algorithm. It
148147
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
150149
strings, where each string is a language code.
151150

152151
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
170169

171170
.. function:: translation(domain, localedir=None, languages=None, class_=None, fallback=False, codeset=None)
172171

173-
Return a :class:`Translations` instance based on the *domain*, *localedir*,
172+
Return a :class:`*Translations` instance based on the *domain*, *localedir*,
174173
and *languages*, which are first passed to :func:`find` to get a list of the
175174
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
177176
provided, otherwise :class:`GNUTranslations`. The class's constructor must
178177
take a single :term:`file object` argument. If provided, *codeset* will change
179178
the charset used to encode translated strings in the
@@ -233,7 +232,7 @@ are the methods of :class:`!NullTranslations`:
233232

234233
.. method:: _parse(fp)
235234

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
237236
the data from the file, initializing its message catalog. If you have an
238237
unsupported message catalog file format, you should override this method
239238
to parse your format.
@@ -275,7 +274,8 @@ are the methods of :class:`!NullTranslations`:
275274

276275
.. method:: info()
277276

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.
279279

280280

281281
.. method:: charset()
@@ -326,15 +326,15 @@ The :mod:`gettext` module provides one additional class derived from
326326
:meth:`_parse` to enable reading GNU :program:`gettext` format :file:`.mo` files
327327
in both big-endian and little-endian format.
328328

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
332332
``key: value`` pairs, and should contain the ``Project-Id-Version`` key. If the
333333
key ``Content-Type`` is found, then the ``charset`` property is used to
334334
initialize the "protected" :attr:`_charset` instance variable, defaulting to
335335
``None`` if not found. If the charset encoding is specified, then all message
336336
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.
338338

339339
Since message ids are read as Unicode strings too, all :meth:`*gettext` methods
340340
will assume message ids as Unicode strings, not byte strings.
@@ -436,7 +436,7 @@ take the following steps:
436436

437437
#. run a suite of tools over your marked files to generate raw messages catalogs
438438

439-
#. create language specific translations of the message catalogs
439+
#. create language-specific translations of the message catalogs
440440

441441
#. use the :mod:`gettext` module so that message strings are properly translated
442442

@@ -446,9 +446,8 @@ it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
446446

447447
filename = 'mylog.txt'
448448
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)
452451

453452
In this example, the string ``'writing a log message'`` is marked as a candidate
454453
for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not.
@@ -499,7 +498,7 @@ Localizing your module
499498
^^^^^^^^^^^^^^^^^^^^^^
500499

501500
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`
503502
API but instead the class-based API.
504503

505504
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:
653652
.. [#] The default locale directory is system dependent; for example, on RedHat Linux
654653
it is :file:`/usr/share/locale`, but on Solaris it is :file:`/usr/lib/locale`.
655654
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.
659659
660660
.. [#] See the footnote for :func:`bindtextdomain` above.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Enhance the gettext docs. Patch by Éric Araujo

0 commit comments

Comments
 (0)