Merge from 3.3

This commit is contained in:
Andrew Kuchling 2013-11-19 11:06:44 -05:00
commit 0e60f85ce4
1 changed files with 48 additions and 49 deletions

View File

@ -94,9 +94,10 @@ class-based API instead.
The Plural formula is taken from the catalog header. It is a C or Python The Plural formula is taken from the catalog header. It is a C or Python
expression that has a free variable *n*; the expression evaluates to the index expression that has a free variable *n*; the expression evaluates to the index
of the plural in the catalog. See the GNU gettext documentation for the precise of the plural in the catalog. See
syntax to be used in :file:`.po` files and the formulas for a variety of `the GNU gettext documentation <https://www.gnu.org/software/gettext/manual/gettext.html>`__
languages. for the precise syntax to be used in :file:`.po` files and the
formulas for a variety of languages.
.. function:: lngettext(singular, plural, n) .. function:: lngettext(singular, plural, n)
@ -451,35 +452,42 @@ it in ``_('...')`` --- that is, a call to the function :func:`_`. For example::
In this example, the string ``'writing a log message'`` is marked as a candidate In this example, the string ``'writing a log message'`` is marked as a candidate
for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not. for translation, while the strings ``'mylog.txt'`` and ``'w'`` are not.
The Python distribution comes with two tools which help you generate the message There are a few tools to extract the strings meant for translation.
catalogs once you've prepared your source code. These may or may not be The original GNU :program:`gettext` only supported C or C++ source
available from a binary distribution, but they can be found in a source code but its extended version :program:`xgettext` scans code written
distribution, in the :file:`Tools/i18n` directory. in a number of languages, including Python, to find strings marked as
translatable. `Babel <http://babel.pocoo.org/>`__ is a Python
internationalization library that includes a :file:`pybabel` script to
extract and compile message catalogs. François Pinard's program
called :program:`xpot` does a similar job and is available as part of
his `po-utils package <http://po-utils.progiciels-bpi.ca/>`__.
The :program:`pygettext` [#]_ program scans all your Python source code looking (Python also includes pure-Python versions of these programs, called
for the strings you previously marked as translatable. It is similar to the GNU :program:`pygettext.py` and :program:`msgfmt.py`; some Python distributions
:program:`gettext` program except that it understands all the intricacies of will install them for you. :program:`pygettext.py` is similar to
Python source code, but knows nothing about C or C++ source code. You don't :program:`xgettext`, but only understands Python source code and
need GNU ``gettext`` unless you're also going to be translating C code (such as cannot handle other programming languages such as C or C++.
C extension modules). :program:`pygettext.py` supports a command-line interface similar to
:program:`xgettext`; for details on its use, run ``pygettext.py
--help``. :program:`msgfmt.py` is binary compatible with GNU
:program:`msgfmt`. With these two programs, you may not need the GNU
:program:`gettext` package to internationalize your Python
applications.)
:program:`pygettext` generates textual Uniforum-style human readable message :program:`xgettext`, :program:`pygettext`, and similar tools generate
catalog :file:`.pot` files, essentially structured human readable files which :file:`.po` files that are message catalogs. They are structured
contain every marked string in the source code, along with a placeholder for the :human-readable files that contain every marked string in the source
translation strings. :program:`pygettext` is a command line script that supports :code, along with a placeholder for the translated versions of these
a similar command line interface as :program:`xgettext`; for details on its use, :strings.
run::
pygettext.py --help Copies of these :file:`.po` files are then handed over to the
individual human translators who write translations for every
Copies of these :file:`.pot` files are then handed over to the individual human supported natural language. They send back the completed
translators who write language-specific versions for every supported natural language-specific versions as a :file:`<language-name>.po` file that's
language. They send you back the filled in language-specific versions as a compiled into a machine-readable :file:`.mo` binary catalog file using
:file:`.po` file. Using the :program:`msgfmt.py` [#]_ program (in the the :program:`msgfmt` program. The :file:`.mo` files are used by the
:file:`Tools/i18n` directory), you take the :file:`.po` files from your :mod:`gettext` module for the actual translation processing at
translators and generate the machine-readable :file:`.mo` binary catalog files. run-time.
The :file:`.mo` files are what the :mod:`gettext` module uses for the actual
translation processing during run-time.
How you use the :mod:`gettext` module in your code depends on whether you are How you use the :mod:`gettext` module in your code depends on whether you are
internationalizing a single module or your entire application. The next two internationalizing a single module or your entire application. The next two
@ -517,7 +525,7 @@ driver file of your application::
import gettext import gettext
gettext.install('myapplication') gettext.install('myapplication')
If you need to set the locale directory, you can pass these into the If you need to set the locale directory, you can pass it into the
:func:`install` function:: :func:`install` function::
import gettext import gettext
@ -590,7 +598,8 @@ care, though if you have a previous definition of :func:`_` in the local
namespace. namespace.
Note that the second use of :func:`_` will not identify "a" as being Note that the second use of :func:`_` will not identify "a" as being
translatable to the :program:`pygettext` program, since it is not a string. translatable to the :program:`gettext` program, because the parameter
is not a string literal.
Another way to handle this is with the following example:: Another way to handle this is with the following example::
@ -606,11 +615,14 @@ Another way to handle this is with the following example::
for a in animals: for a in animals:
print(_(a)) print(_(a))
In this case, you are marking translatable strings with the function :func:`N_`, In this case, you are marking translatable strings with the function
[#]_ which won't conflict with any definition of :func:`_`. However, you will :func:`N_`, which won't conflict with any definition of :func:`_`.
need to teach your message extraction program to look for translatable strings However, you will need to teach your message extraction program to
marked with :func:`N_`. :program:`pygettext` and :program:`xpot` both support look for translatable strings marked with :func:`N_`. :program:`xgettext`,
this through the use of command line switches. :program:`pygettext`, ``pybabel extract``, and :program:`xpot` all
support this through the use of the :option:`-k` command-line switch.
The choice of :func:`N_` here is totally arbitrary; it could have just
as easily been :func:`MarkThisStringForTranslation`.
Acknowledgements Acknowledgements
@ -645,16 +657,3 @@ implementations, and valuable experience to the creation of this module:
absolute path at the start of your application. absolute path at the start of your application.
.. [#] See the footnote for :func:`bindtextdomain` above. .. [#] See the footnote for :func:`bindtextdomain` above.
.. [#] François Pinard has written a program called :program:`xpot` which does a
similar job. It is available as part of his `po-utils package
<http://po-utils.progiciels-bpi.ca/>`_.
.. [#] :program:`msgfmt.py` is binary compatible with GNU :program:`msgfmt` except that
it provides a simpler, all-Python implementation. With this and
:program:`pygettext.py`, you generally won't need to install the GNU
:program:`gettext` package to internationalize your Python applications.
.. [#] The choice of :func:`N_` here is totally arbitrary; it could have just as easily
been :func:`MarkThisStringForTranslation`.