如何以可移植的方式本地化argparse生成的消息? [英] How can I localize argparse generated messages in a portable way?

查看:58
本文介绍了如何以可移植的方式本地化argparse生成的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个小模块,根据其exif时间戳自动在目录中重命名照片(目标:轻松混合来自不同相机或智能手机的照片).它既可以作为Python包运行,也可以通过argparse通过一个小的包装程序直接在命令行中正常运行.

I am developping a small module to automatically rename photographs in a directory according to their exif timestamp (goal: easily mixing pictures from different cameras or smartphones). It works smoothly either as a Python package or directly from the command line through a tiny wrapper using argparse.

我刚刚有了一个(相当愚蠢的)想法,将其本地化为非英语语言.好的,gettext是我自己编写代码的朋友,但是当我来到agparse生成的消息时,我发现自己处于草率的状态...

And I have just had the (rather stupid) idea to localize it in non English language. Ok, gettext is my friend for all my own code, but when I came to agparse generated messages, I found myself on a sloppy ground...

我已经在SO上找到了一些资源:

I have already found some resources on SO:

  • How to make python's argparse generate Non-English text?
  • Translate argparse's internal strings

最后都将argparse中的相​​关字符串添加到po/mo文件中,并让argparse模块自动使用翻译后的字符串,因为它内部使用了_(...)包装器.到目前为止,一切都很好.

Both end in adding the relevant strings from argparse into a po/mo file and let the argparse module automatically use the translated strings because internally it uses the _(...) wrapper. So far, so good.

我觉得这是一种解决方法,而不是干净整洁的解决方案,因为:

I feel this more as a workaround than a clean and neat solution because:

  • 我在Python官方文档中找不到一个建议的词
  • 它似乎是一个正在进行的 :未记录下来,因此某些字符串可能会在将来的Python版本中更改(或者我错过了吗?)
  • I could not find a word advising it in official Python documentation
  • It looks like a work in progress: implemented by not documented, so some strings could change in a future Python release (or did I miss something?)

当前代码:

parser = argparse.ArgumentParser(
    prog = prog,
    description="Rename pictures according to their exif timestamp")
parser.add_argument("-v", "--version", action="version",
                    version="%(prog)s " + __version__)
parser.add_argument("--folder", "-f", default = ".",
                    help = "folder containing files to rename")
parser.add_argument("-s", "--src_mask", default="DSCF*.jpg",
                    help = "pattern to select the files to rename")
parser.add_argument("-d", "--dst_mask", default="%Y%m%d_%H%M%S",
                    help = "format for the new file name")
parser.add_argument("-e", "--ext", default=".jpg", dest="ext_mask",
                    help = "extension for the new file name")
parser.add_argument("-r", "--ref_file", default="names.log",
                    help = "a file to remember the old names")
parser.add_argument("-D", "--debug", action="store_true",
                    help = "print a line per rename")
parser.add_argument("-X", "--dry_run", action="store_true", dest="dummy",
                    help = "process normally except no rename occurs")

# subcommands configuration (rename, back, merge)
subparser = parser.add_subparsers(dest='subcommand', help="sub-commands")
ren = subparser.add_parser("rename", help=
                           "rename files by using their exif timestamp")
ren.add_argument("files", nargs="*",
                  help = "files to process (default: src_mask)")
back = subparser.add_parser("back",
                            help="rename files back to their original name")
back.add_argument("files", nargs="*",
                   help = "files to process (default: content of ref_file)")
merge = subparser.add_parser("merge",
                             help="merge files from a different folder")
merge.add_argument("src_folder", metavar="folder",
                    help = "folder from where merge picture files")
merge.add_argument("files", nargs="*",
                  help = "files to process (default: src_mask)")

我知道如何用_()包装我自己的字符串,并且 usage help 消息可能可以获得可接受的翻译,但是有很多错误当用户输入错误的语法时出现错误消息,并且我想防止法语程序中间出现英语错误消息...

I know how to wrap my own strings with _(), and I could probably have acceptable translations for the usage and help messages, but there are plenty of error messages when the user gives a wrong syntax, and I would like to prevent English error messages in the middle of French speaking program...

是否可以保证argparse模块中的字符串实现不会改变,或者是否有更健壮/便携式的方式来为其消息提供翻译?

Is there any guarantee that the implementation of strings in the argparse module will not change, or is there a more robust/portable way to provide translations for its messages?

推荐答案

经过更多研究和@hpaulj的好评后,我可以确认:

After some more research and @hpaulj's great comments, I can confirm:

  • argparse中的可本地化消息从3.3向上兼容到当前版本(旧消息从未更改,但为新功能添加了新消息)
  • 以上在3.3之前是不正确的
  • 2.7略有不同
  • the localizable messages from argparse are upward compatible from 3.3 to current version (old messages were never changed but new messages were added for new features)
  • the above is not true before 3.3
  • there are slight differences in 2.7

这意味着这里只有2条路径:

That means that only 2 paths are possible here:

  • 接受风险并提供当前版本的翻译,该版本将接受任何大于等于3.3的Python版本-风险是将来的版本会破坏翻译或添加新的(未翻译的)消息.无需多说,因为这将明确使用模块的实现细节
  • 完全不使用argparse模块,并基于getopt构建自定义解析器.对于不需要argparse
  • 的全部功能的简单用例,这可能是一个可接受的选项.
  • accept the risk and provide a translation for the current version that will accept any Python version >= 3.3 - the risk is that a future version breaks the translation or add new (untranslated) messages. Nothing more to say because this will explicitely use the implementation details of the module
  • do not use at all argparse module and build a custom parser based on getopt. It is probably an acceptable option for simple use cases that do not require the full power of argparse

他们中没有一个人真的很好,但是我无法想象会有更好的人...

None of them are really good, but I cannot imagine a better one...

我将尝试在github或gitlab上设置一个项目,以提供当前argparse的pot文件和法语翻译,并使其在PyPI上可用.如果有的话,我将在此处添加引用,并很高兴在此包含其他语言.

I will try to setup a project on github or gitlab providing the pot file and the french translation for the current argparse, and make it available on PyPI. If it ever exists, I will add references for it here and shall be glad to include other languages there.

目前已在 GitHUB 中提供了一个为argparse模块提供法语翻译的项目的Beta版本. >

A beta version of a project giving French translations for the argparse module is currently available on GitHUB

之所以将其称为Beta是因为目前尚未对其进行广泛的测试,但是可以直接使用它,也可以作为可以做什么的示例.二进制文件轮包含一个小的endian mo文件,但是源分发允许通过包含来自CPython的Tools i18n的msgfmt.py文件的副本,而在目标系统上自动生成mo文件,而没有其他依赖关系.

I call it beta because at this time, it has not been extensively tested, but it can be used either directly or as an example of what could be done. The binary wheel contains a little endian mo file, but the source distribution allows the mo file to be generated automatically on the target system with no additional dependency by including a copy of the msgfmt.py file from the Tools i18n of CPython.

这篇关于如何以可移植的方式本地化argparse生成的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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