用polib在Python中修改/更新po文件中的条目
polib 似乎是处理 Python 中 gettext/po 文件的首选库。文档展示了如何遍历消息字符串、保存 po 和 mo 文件等等。不过,我不太明白,如何编辑一个特定的条目呢?
假设我遍历一个现有的 po 文件中的所有消息,并把它们显示在一个包含文本区域的 HTML 表单中。提交表单后,我会得到 - 比如说 - 原始的 msgid = "Hello World" 和通过文本区域翻译的 msgstr = "Hallo Welt"
po 文件中的原始部分可能看起来是这样的:
#: .\accounts\forms.py:26 .\accounts\registration\forms.py:48
msgid "Hello World"
msgstr ""
或者带有模糊标记的情况:
#: .\accounts\forms.py:26 .\accounts\registration\forms.py:48
#, fuzzy
msgid "Hello World"
msgstr "Hallo"
那么,我该如何在实际的 po 文件中更新这个特定的翻译呢?如果这个消息被标记为“模糊”,我又该如何去掉这个标记呢?
任何帮助都非常感谢……
1 个回答
10
好的,在查看了polib的源代码后,我找到了实现我想要的方式:
entry = po.find('Email address')
if entry:
entry.msgstr = 'E-Mail-Adresse'
if 'fuzzy' in entry.flags:
entry.flags.remove('fuzzy')
这似乎是正确的做法……
以复数形式为例:
entry = po.find('%s hour ago')
if entry and entry.msgid_plural:
entry.msgstr_plural['0'] = 'Vor %s Stunde'
entry.msgstr_plural['1'] = 'Vor %s Stunden'
polib的文档确实应该更新一下。总的来说,这是个很棒的工具。