【问题标题】:Removing all fuzzy entries of a PO file删除 PO 文件的所有模糊条目
【发布时间】:2011-11-14 09:41:59
【问题描述】:

有谁知道从 PO 文件中批量删除所有模糊翻译的方法。比如:

如果 #,fuzzy == TRUE Then SET msgstr="" AND REMOVE #,fuzzy

【问题讨论】:

  • 我认为您需要详细说明。
  • 汤姆,你错过了什么细节?
  • 也许这些附加信息会有所帮助:gnu.org/s/hello/manual/gettext/PO-Files.html
  • 看起来这个问题可能过于模糊,但我不确定是否应该标记它,所以我问了。我不知道gettext,但是知道的人应该可以为您提供帮助。看起来他们应该用gettext来操作,所以我不愿意写一个Python脚本来做。
  • 将近 5 年后,但您是否考虑过在 PO 生成过程中简单地禁用模糊匹配? --no-fuzzy-matching msgmerge 的选项可以做到这一点

标签: translation edit gettext po


【解决方案1】:

您可以使用polib 删除模糊字符串,这是 Python 中用于处理 gettext po 文件的库:

import os, polib
for dirname, dirnames, filenames in os.walk('/path/to/your/project/'):
    for filename in filenames:
        try: ext = filename.rsplit('.', 1)[1]
        except: ext = ''
        if ext == 'po':
            po = polib.pofile(os.path.join(dirname, filename))
            for entry in po.fuzzy_entries():
                entry.msgstr = ''
                if entry.msgid_plural: entry.msgstr_plural['0'] = ''
                if entry.msgid_plural and '1' in entry.msgstr_plural: entry.msgstr_plural['1'] = ''
                if entry.msgid_plural and '2' in entry.msgstr_plural: entry.msgstr_plural['2'] = ''
                entry.flags.remove('fuzzy')
            po.save()

此脚本删除了模糊翻译字符串 + 模糊标志,但保持未翻译的原始 msgid 完整。一些语言(ru、cz、...)有两个以上的复数形式,因此,我们检查 msgstr_plural['2']。列表索引必须是字符串。不要为此使用整数。

【讨论】:

  • 完全重写的答案应该完全符合您的要求。您现在可能已经解决了这个问题,但也许其他人也遇到了同样的问题。
【解决方案2】:

如果安装了 gettext,您可以使用 msgattrib 命令来完成此操作:

msgattrib --clear-fuzzy --empty -o /path/to/output.po /path/to/input.po

msgattrib 的完整文档在这里:

https://www.gnu.org/software/gettext/manual/html_node/msgattrib-Invocation.html

【讨论】:

  • --empty 似乎是一个新的选择。今天,这应该是公认的答案。谢谢!
  • 好的,这给了我一个“无效的多字节序列”错误,即使 PO 文件将 UTF8 设置为编码......
【解决方案3】:

如果您安装了 GNU gettext,则可以使用此命令删除模糊消息:

msgattrib --no-fuzzy -o path/to/your/output/po/file path/to/your/input/po/file

【讨论】:

  • 这个删除模糊元素,但问题是为此设置空 msgstr,我也在寻找解决方案,这是不正确的。
  • 使用模板执行msgattrib --no-fuzzy 后跟msgmerge 怎么样?这应该会从模板中带回空消息。
  • 这会删除模糊条目。 OP想要清除模糊翻译并删除模糊标志,但保留条目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
  • 2010-11-05
  • 2015-08-08
  • 2013-04-24
  • 2014-04-06
  • 1970-01-01
相关资源
最近更新 更多