Skip to content

Commit 30c5ad2

Browse files
committed
#1098749: re-word gettext docs to not encourage using pygettext so much.
Also, add a link to the Babel package.
1 parent 7ad11bf commit 30c5ad2

1 file changed

Lines changed: 51 additions & 52 deletions

File tree

Doc/library/gettext.rst

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ class-based API instead.
9494

9595
The Plural formula is taken from the catalog header. It is a C or Python
9696
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.
97+
of the plural in the catalog. See
98+
`the GNU gettext documentation <https://www.gnu.org/software/gettext/manual/gettext.html>`__
99+
for the precise syntax to be used in :file:`.po` files and the
100+
formulas for a variety of languages.
100101

101102

102103
.. function:: lngettext(singular, plural, n)
@@ -451,35 +452,42 @@ it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
451452
In this example, the string ``'writing a log message'`` is marked as a candidate
452453
for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not.
453454

454-
The Python distribution comes with two tools which help you generate the message
455-
catalogs once you've prepared your source code. These may or may not be
456-
available from a binary distribution, but they can be found in a source
457-
distribution, in the :file:`Tools/i18n` directory.
458-
459-
The :program:`pygettext` [#]_ program scans all your Python source code looking
460-
for the strings you previously marked as translatable. It is similar to the GNU
461-
:program:`gettext` program except that it understands all the intricacies of
462-
Python source code, but knows nothing about C or C++ source code. You don't
463-
need GNU ``gettext`` unless you're also going to be translating C code (such as
464-
C extension modules).
465-
466-
:program:`pygettext` generates textual Uniforum-style human readable message
467-
catalog :file:`.pot` files, essentially structured human readable files which
468-
contain every marked string in the source code, along with a placeholder for the
469-
translation strings. :program:`pygettext` is a command line script that supports
470-
a similar command line interface as :program:`xgettext`; for details on its use,
471-
run::
472-
473-
pygettext.py --help
474-
475-
Copies of these :file:`.pot` files are then handed over to the individual human
476-
translators who write language-specific versions for every supported natural
477-
language. They send you back the filled in language-specific versions as a
478-
:file:`.po` file. Using the :program:`msgfmt.py` [#]_ program (in the
479-
:file:`Tools/i18n` directory), you take the :file:`.po` files from your
480-
translators and generate the machine-readable :file:`.mo` binary catalog files.
481-
The :file:`.mo` files are what the :mod:`gettext` module uses for the actual
482-
translation processing during run-time.
455+
There are a few tools to extract the strings meant for translation.
456+
The original GNU :program:`gettext` only supported C or C++ source
457+
code but its extended version :program:`xgettext` scans code written
458+
in a number of languages, including Python, to find strings marked as
459+
translatable. `Babel <http://babel.pocoo.org/>`__ is a Python
460+
internationalization library that includes a :file:`pybabel` script to
461+
extract and compile message catalogs. François Pinard's program
462+
called :program:`xpot` does a similar job and is available as part of
463+
his `po-utils package <http://po-utils.progiciels-bpi.ca/>`__.
464+
465+
(Python also includes pure-Python versions of these programs, called
466+
:program:`pygettext.py` and :program:`msgfmt.py`; some Python distributions
467+
will install them for you. :program:`pygettext.py` is similar to
468+
:program:`xgettext`, but only understands Python source code and
469+
cannot handle other programming languages such as C or C++.
470+
:program:`pygettext.py` supports a command-line interface similar to
471+
:program:`xgettext`; for details on its use, run ``pygettext.py
472+
--help``. :program:`msgfmt.py` is binary compatible with GNU
473+
:program:`msgfmt`. With these two programs, you may not need the GNU
474+
:program:`gettext` package to internationalize your Python
475+
applications.)
476+
477+
:program:`xgettext`, :program:`pygettext`, and similar tools generate
478+
:file:`.po` files that are message catalogs. They are structured
479+
:human-readable files that contain every marked string in the source
480+
:code, along with a placeholder for the translated versions of these
481+
:strings.
482+
483+
Copies of these :file:`.po` files are then handed over to the
484+
individual human translators who write translations for every
485+
supported natural language. They send back the completed
486+
language-specific versions as a :file:`<language-name>.po` file that's
487+
compiled into a machine-readable :file:`.mo` binary catalog file using
488+
the :program:`msgfmt` program. The :file:`.mo` files are used by the
489+
:mod:`gettext` module for the actual translation processing at
490+
run-time.
483491

484492
How you use the :mod:`gettext` module in your code depends on whether you are
485493
internationalizing a single module or your entire application. The next two
@@ -517,7 +525,7 @@ driver file of your application::
517525
import gettext
518526
gettext.install('myapplication')
519527

520-
If you need to set the locale directory, you can pass these into the
528+
If you need to set the locale directory, you can pass it into the
521529
:func:`install` function::
522530

523531
import gettext
@@ -590,7 +598,8 @@ care, though if you have a previous definition of :func:`_` in the local
590598
namespace.
591599

592600
Note that the second use of :func:`_` will not identify "a" as being
593-
translatable to the :program:`pygettext` program, since it is not a string.
601+
translatable to the :program:`gettext` program, because the parameter
602+
is not a string literal.
594603

595604
Another way to handle this is with the following example::
596605

@@ -606,11 +615,14 @@ Another way to handle this is with the following example::
606615
for a in animals:
607616
print(_(a))
608617

609-
In this case, you are marking translatable strings with the function :func:`N_`,
610-
[#]_ which won't conflict with any definition of :func:`_`. However, you will
611-
need to teach your message extraction program to look for translatable strings
612-
marked with :func:`N_`. :program:`pygettext` and :program:`xpot` both support
613-
this through the use of command line switches.
618+
In this case, you are marking translatable strings with the function
619+
:func:`N_`, which won't conflict with any definition of :func:`_`.
620+
However, you will need to teach your message extraction program to
621+
look for translatable strings marked with :func:`N_`. :program:`xgettext`,
622+
:program:`pygettext`, ``pybabel extract``, and :program:`xpot` all
623+
support this through the use of the :option:`-k` command-line switch.
624+
The choice of :func:`N_` here is totally arbitrary; it could have just
625+
as easily been :func:`MarkThisStringForTranslation`.
614626

615627

616628
Acknowledgements
@@ -645,16 +657,3 @@ implementations, and valuable experience to the creation of this module:
645657
absolute path at the start of your application.
646658
647659
.. [#] See the footnote for :func:`bindtextdomain` above.
648-
649-
.. [#] François Pinard has written a program called :program:`xpot` which does a
650-
similar job. It is available as part of his `po-utils package
651-
<http://po-utils.progiciels-bpi.ca/>`_.
652-
653-
.. [#] :program:`msgfmt.py` is binary compatible with GNU :program:`msgfmt` except that
654-
it provides a simpler, all-Python implementation. With this and
655-
:program:`pygettext.py`, you generally won't need to install the GNU
656-
:program:`gettext` package to internationalize your Python applications.
657-
658-
.. [#] The choice of :func:`N_` here is totally arbitrary; it could have just as easily
659-
been :func:`MarkThisStringForTranslation`.
660-

0 commit comments

Comments
 (0)