python - 为什么我得到 'msgid' 格式字符串未命名参数警告,(仅)在 wxPython 上为 gettext 运行 mki18n.py 时

标签 python wxpython

尝试运行 mki18n.py脚本在我的 Python 代码上,我在这一行收到警告:

 >>> dlg = wx.MessageDialog(None, str (_("Attached device is \"%s\",\nschedule file header is for \"%s\"") % (rt_model, line)), _("Device mismatch"), wx.OK | wx.ICON_ERROR)

然后给出这个:

warning: 'msgid' format string with unnamed arguments cannot be properly localized:
         The translator cannot reorder the arguments.
         Please consider using a format string with named arguments,
         and a mapping instead of a tuple for the arguments.

mki18n.py 脚本不喜欢出现两个连续的 s%,但我无法解码警告消息的内容。否则(在不关心 i18n 的情况下运行我的程序)我没有收到任何错误并且该对话框始终显示正常。

那一行有什么问题? (有什么可以改进的?)

编辑 通过使用 geni18n.py (及其来自 i18nwxapp 包的相关文件)我得到了预期的结果,没有错误(即生成 .pot 文件进行翻译)。我仍然不知道我的代码中是否存在 geni18n 容忍的问题,或者 mki18n 是否存在由我的特定代码行触发的问题 (?)。

最佳答案

所以基本上消息说的是,如果他/她需要,翻译人员将难以重新排序变量。因此请考虑以下几点:

my_string = "Hello %s, it is %s o'clock." % ('Foo', 'two')

现在假设翻译器首先需要 two,然后是 Foo:

translated_string = "It is %s o'clock, Mr. %s" % ('Foo', 'two')

如果不使用带有命名参数的格式字符串Foo 仍会进入第一个%s,从而给您:

print translated_string
# Output: It is Foo o'clock, Mr. two.

要解决这个问题,只需使用命名参数。所以上面的例子是:

my_string = "Hello %(person)s, it is %(time)s o'clock." % ({
    'person':'Foo', 
    'time':'two'
})

translated_string = "It is %(time)s o'clock, Mr. %(person)s" % ({
    'person':'Foo', 
    'time':'two'
})

这样,译者就可以把persontime放在他/她想要的地方。它还让他们了解变量的含义,如果您仅使用 %s,他们将不知道。

希望澄清事情。

关于python - 为什么我得到 'msgid' 格式字符串未命名参数警告,(仅)在 wxPython 上为 gettext 运行 mki18n.py 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23405521/

相关文章:

python - 获取整数的后三位数字

python - 使用 arrayName 和 arrayName[ :] in Python? 有什么区别

python - 在Python脚本中访问存储在HDFS分布式缓存中的文件

Python:用于快速全屏 jpg/png 显示的 OSX 库

python - 在wxPython中确定选定的图像

user-interface - wxpython 使用 time.sleep() 不阻塞完整的 GUI

python - 时间间隔选择的最佳界面范例?

python - 将 x Axis 分成 2 个以上的部分 (Python)

python - wxPython 中的 sendMessage() takes at most 3 arguments (4 given) 错误

python - 使用scrapy提取动态数据-基于邮政编码的位置