Python 3 gettext无法用于argparse [英] Python 3 gettext not working for argparse

查看:104
本文介绍了Python 3 gettext无法用于argparse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将'usage:'转换为'foobar'.

I want to translate 'usage: ' into 'foobar'.

    print(gettext.find('test', 'i18n', ['en_US']))
    translator = gettext.translation('test', localedir='i18n', languages=['en_US'])
    translator.install()
    print(_('usage: '))
    parser = argparse.ArgumentParser(prog="jcheq.py",
                                     usage="%(prog)s [opciones] [paths...]\nThe paths are optional; if not given . is "
                                           "used.",
                                     add_help=False,
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

但是它输出:

~/Desktop/Proyectos/UNPAZ/jcheq/jcheq$ python3 jcheq.py --help
i18n/en_US/LC_MESSAGES/test.mo
foobar: 
usage: jcheq.py [opciones] [paths...]
The paths are optional; if not given . is used.

Opciones:
  -h, --help      Mostrar este mensaje de ayuda y salir.
  -m, --modified  show last modified date/time (default: False)

似乎它适用于我的字符串,但不适用于argparse的字符串:

Seems that it is working for my string, but not for argparse's one:

argparse.py:292 aprox

 def _format_usage(self, usage, actions, groups, prefix):
        if prefix is None:
            prefix = _('usage: ')

推荐答案

产生:

1702:~/mypy$ python3 stack35347168.py 
Verwendung: jcheq.py [opciones] [paths...]
The paths are optional; if not given . is used.

Verwendung: jcheq.py [opciones] [paths...]
The paths are optional; if not given . is used.

optional arguments:
  -h, --help  zeige diese Hilfe an und tue nichts weiteres

这篇关于Python 3 gettext无法用于argparse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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