Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to localize Python's argparse module, without patching it?

A localized command line application looks strange when some part of the messages are in the user language and some other parts, in English.

I don't know if I messed up anything when I installed Python 3 from source, it seems there are no *.mo files, so argparse (among the whole) is not localization aware.

The API does not seems to offer a way to localize, neither. Or did I missed it?

I could patch argparse.py, but I won't, as I want it to be portable, and I'm not OK with suggesting users to patch their Python installation.

Question in fewer words: how to localize argpase without patching Python standard library?

Related question: is this possible?

like image 942
Hibou57 Avatar asked Feb 25 '15 22:02

Hibou57


1 Answers

By default argparse uses the gettext module to translate the messages so you can easily translate them if you want to.

To generate the *.pot files (which you can convert to *.mo files after translation) you can use the pygettext program which is available in the built-in gettext module in python.

Usage:

python pygettext.py argparse.py

This will generate a messages.pot which you can translate, after that just generate the .mo (many ways to do this, just google).

For more info about pygettext see the Python manual about the subject: Internationalizing your programs and modules.

like image 109
Wolph Avatar answered Sep 28 '22 08:09

Wolph