diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-07-23 09:36:10 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-23 09:36:10 (GMT) |
commit | 63ae7edd3518ecc6364e51d1f2a5748d8fa36b85 (patch) | |
tree | 87f6fdab5dba4ce211cc9d2e34a69baf7a40d59f /Doc/howto | |
parent | 1703262c0a7ee9307f7104900f024cfc5235d01d (diff) | |
download | cpython-63ae7edd3518ecc6364e51d1f2a5748d8fa36b85.zip cpython-63ae7edd3518ecc6364e51d1f2a5748d8fa36b85.tar.gz cpython-63ae7edd3518ecc6364e51d1f2a5748d8fa36b85.tar.bz2 |
[3.12] gh-54738: Add argparse i18n howto (GH-104562) (#107102)
(cherry picked from commit dcd7acb04a719d8d30c8d03b80d3d48b6c035e14)
Co-authored-by: Tomas R <tomas.roun8@gmail.com>
Diffstat (limited to 'Doc/howto')
-rw-r--r-- | Doc/howto/argparse.rst | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Doc/howto/argparse.rst b/Doc/howto/argparse.rst index 52e98fa..ae5bab9 100644 --- a/Doc/howto/argparse.rst +++ b/Doc/howto/argparse.rst @@ -788,6 +788,59 @@ but not both at the same time: -q, --quiet +How to translate the argparse output +==================================== + +The output of the :mod:`argparse` module such as its help text and error +messages are all made translatable using the :mod:`gettext` module. This +allows applications to easily localize messages produced by +:mod:`argparse`. See also :ref:`i18n-howto`. + +For instance, in this :mod:`argparse` output: + +.. code-block:: shell-session + + $ python prog.py --help + usage: prog.py [-h] [-v | -q] x y + + calculate X to the power of Y + + positional arguments: + x the base + y the exponent + + options: + -h, --help show this help message and exit + -v, --verbose + -q, --quiet + +The strings ``usage:``, ``positional arguments:``, ``options:`` and +``show this help message and exit`` are all translatable. + +In order to translate these strings, they must first be extracted +into a ``.po`` file. For example, using `Babel <https://babel.pocoo.org/>`__, +run this command: + +.. code-block:: shell-session + + $ pybabel extract -o messages.po /usr/lib/python3.12/argparse.py + +This command will extract all translatable strings from the :mod:`argparse` +module and output them into a file named ``messages.po``. This command assumes +that your Python installation is in ``/usr/lib``. + +You can find out the location of the :mod:`argparse` module on your system +using this script:: + + import argparse + print(argparse.__file__) + +Once the messages in the ``.po`` file are translated and the translations are +installed using :mod:`gettext`, :mod:`argparse` will be able to display the +translated messages. + +To translate your own strings in the :mod:`argparse` output, use :mod:`gettext`. + Conclusion ========== |