Skip to content

Commit 8ee4650

Browse files
tomasr8miss-islington
authored andcommitted
pythongh-54738: Add argparse i18n howto (pythonGH-104562)
(cherry picked from commit dcd7acb) Co-authored-by: Tomas R <[email protected]>
1 parent 9875b17 commit 8ee4650

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

Doc/howto/argparse.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,59 @@ but not both at the same time:
759759
-q, --quiet
760760
761761
762+
How to translate the argparse output
763+
====================================
764+
765+
The output of the :mod:`argparse` module such as its help text and error
766+
messages are all made translatable using the :mod:`gettext` module. This
767+
allows applications to easily localize messages produced by
768+
:mod:`argparse`. See also :ref:`i18n-howto`.
769+
770+
For instance, in this :mod:`argparse` output:
771+
772+
.. code-block:: shell-session
773+
774+
$ python prog.py --help
775+
usage: prog.py [-h] [-v | -q] x y
776+
777+
calculate X to the power of Y
778+
779+
positional arguments:
780+
x the base
781+
y the exponent
782+
783+
options:
784+
-h, --help show this help message and exit
785+
-v, --verbose
786+
-q, --quiet
787+
788+
The strings ``usage:``, ``positional arguments:``, ``options:`` and
789+
``show this help message and exit`` are all translatable.
790+
791+
In order to translate these strings, they must first be extracted
792+
into a ``.po`` file. For example, using `Babel <https://babel.pocoo.org/>`__,
793+
run this command:
794+
795+
.. code-block:: shell-session
796+
797+
$ pybabel extract -o messages.po /usr/lib/python3.12/argparse.py
798+
799+
This command will extract all translatable strings from the :mod:`argparse`
800+
module and output them into a file named ``messages.po``. This command assumes
801+
that your Python installation is in ``/usr/lib``.
802+
803+
You can find out the location of the :mod:`argparse` module on your system
804+
using this script::
805+
806+
import argparse
807+
print(argparse.__file__)
808+
809+
Once the messages in the ``.po`` file are translated and the translations are
810+
installed using :mod:`gettext`, :mod:`argparse` will be able to display the
811+
translated messages.
812+
813+
To translate your own strings in the :mod:`argparse` output, use :mod:`gettext`.
814+
762815
Conclusion
763816
==========
764817

Doc/library/gettext.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ One difference between this module and Henstridge's: his catalog objects
411411
supported access through a mapping API, but this appears to be unused and so is
412412
not currently supported.
413413

414+
.. _i18n-howto:
414415

415416
Internationalizing your programs and modules
416417
--------------------------------------------
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add documentation on how to localize the :mod:`argparse` module.

0 commit comments

Comments
 (0)