Search code examples
translationeditgettextpo

Removing all fuzzy entries of a PO file


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


Solution

  • 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]. With older versions of polib, the index to msgstr_plural is a string, so this would become msgstr_plural['2'].