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

北慕城南 提交于 2019-12-07 02:12:00

问题


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?


回答1:


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: https://docs.python.org/2/library/gettext.html#internationalizing-your-programs-and-modules



来源:https://stackoverflow.com/questions/28730641/how-to-localize-pythons-argparse-module-without-patching-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!