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

查看:23
本文介绍了如何以可移植的方式本地化 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:

两者都以将来自 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)")

我知道如何用 _() 来包装我自己的字符串,而且我可能对 usagehelp 消息有可接受的翻译,但是当用户给出错误的语法时会出现大量的错误信息,我想防止法语程序中出现英文错误信息......

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

我称它为 beta 是因为目前它还没有经过广泛的测试,但它可以直接使用,也可以作为可以做什么的示例.二进制轮包含一个小字节序的 mo 文件,但源代码分发允许通过包含工具中 msgfmt.py 文件的副本在目标系统上自动生成 mo 文件,而无需额外的依赖CPython 的 i18n.

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天全站免登陆