如何在不打补丁的情况下本地化python的argparse模块? [英] How to localize Python's argparse module, without patching it?

查看:73
本文介绍了如何在不打补丁的情况下本地化python的argparse模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当消息的某些部分使用用户语言,而另一些消息使用英语时,本地化的命令行应用程序看起来很奇怪.

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

我不知道从源代码安装Python 3时是否搞砸了任何东西,似乎没有*.mo文件,因此argparse(整体上)不支持本地化.

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.

API似乎也没有提供本地化的方法.还是我错过了?

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

我可以修补argparse.py,但我不会,因为我希望它具有可移植性,并且我不建议用户修补Python安装.

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.

问题,用更少的词:如何在不修补Python标准库的情况下本地化argpase?

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

相关问题:这可能吗?

推荐答案

默认情况下,argparse使用gettext模块来翻译消息,以便您可以轻松地翻译它们.

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

要生成*.pot文件(在翻译后可以将其转换为*.mo文件),可以使用python内置gettext模块中可用的pygettext程序.

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.

用法:

python pygettext.py argparse.py

这将生成一个messages.pot,您可以将其翻译,之后只需生成.mo(执行此操作的多种方法,仅是google)即可.

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

有关pygettext的更多信息,请参见有关该主题的Python手册:

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

这篇关于如何在不打补丁的情况下本地化python的argparse模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆