在 Python 中使用 polib 修改/更新 po 文件中的条目

Sim*_*ger 3 python translation gettext poedit

poedit似乎是在 Python 中处理 gettext/po 文件的首选库。该文档显示如何通过迭代消息字符串,保存PO和MO文件,等等。但是,这不是我清楚,怎么能一个编辑的特定项?

比方说,我遍历现有 po 文件中的所有消息,并以带有 textareas 的 HTML 表单显示它们。通过提交表单,我得到 - 例如 - 原始 msgid = "Hello World" 和 via textarea 翻译的 msgstr = "Hallo Welt"

po 文件中的原始部分可能如下所示:

#: .\accounts\forms.py:26 .\accounts\registration\forms.py:48
msgid "Hello World"
msgstr ""
Run Code Online (Sandbox Code Playgroud)

或设置模糊标志:

#: .\accounts\forms.py:26 .\accounts\registration\forms.py:48
#, fuzzy
msgid "Hello World"
msgstr "Hallo"
Run Code Online (Sandbox Code Playgroud)

现在我如何在实际的 po 文件中更新这个特定的翻译?如果此消息被标记为“模糊”,我该如何删除此标志?

任何帮助表示赞赏...

Sim*_*ger 7

好吧,通读了polib的源代码后,我找到了这种方式来实现,我想要的:

entry = po.find('Email address')
if entry:
    entry.msgstr = 'E-Mail-Adresse'
    if 'fuzzy' in entry.flags:
        entry.flags.remove('fuzzy')
Run Code Online (Sandbox Code Playgroud)

这似乎是要走的路......

在复数的情况下 - 仅举个例子:

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'
Run Code Online (Sandbox Code Playgroud)

polib 的文档应该最终更新。否则很棒的工具。