Removing all fuzzy entries of a PO file

让人想犯罪 __ 提交于 2019-12-04 18:10:41

问题


Does anyone know a method to mass delete all fuzzy translations from a PO file. Something like:

if #, fuzzy == TRUE Then SET msgstr="" AND REMOVE #, fuzzy


回答1:


You can remove fuzzy strings with polib, which is THE library in Python for working with gettext po files:

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()

This script removes the fuzzy translation strings + fuzzy flags, but keeps the untranslated original msgids intact. Some languages (ru, cz, ...) have more than two plural forms, therefore, we check on msgstr_plural['2']. The list index has to be a string. Don't use integers for that.




回答2:


If gettext is installed you can use the msgattrib command to accomplish this:

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

The full documentation for msgattrib is here:

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




回答3:


If you have GNU gettext installed then you can use this command to remove fuzzy messages:

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



来源:https://stackoverflow.com/questions/7372414/removing-all-fuzzy-entries-of-a-po-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!