14

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

Martijn Burger
  • 7,315
  • 8
  • 54
  • 94
  • You need to go into more detail, I think. – Tom Zych Sep 10 '11 at 14:40
  • What details do you miss, Tom? – Martijn Burger Sep 10 '11 at 14:59
  • Maybe this additional info helps: http://www.gnu.org/s/hello/manual/gettext/PO-Files.html – Martijn Burger Sep 10 '11 at 15:00
  • It looked like it might be too vague a question, but I wasn't sure if I should flag it, so I asked. I don't know gettext, but someone who does should be able to help you. It looks like they should be manipulated with gettext, so I'm reluctant to write a Python script to do it. – Tom Zych Sep 10 '11 at 16:04
  • Yeah that was my thought too, write a script to do it, but I was wondering if there was a simpler solution. – Martijn Burger Sep 10 '11 at 23:27
  • 1
    nearly 5 years later, but did you consider simply disabling fuzzy matching during PO generation? `--no-fuzzy-matching` option for msgmerge does that – dfherr Jun 12 '16 at 18:10
  • I thought that wasn't an option for me for me at the time. But I really cannot remember why anymore. :) – Martijn Burger Jun 13 '16 at 06:41

3 Answers3

20

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

Aaron M
  • 301
  • 2
  • 7
11

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'].

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
Simon Steinberger
  • 6,605
  • 5
  • 55
  • 97
  • 1
    Fully rewritten answer that should do exactly what you are looking for. You've probably solved the issue by now, but maybe anyone else runs into the same problem. – Simon Steinberger Jan 21 '13 at 15:52
8

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

binhnv
  • 97
  • 1
  • 1
    this removing fuzzy elements, but question is for set empty msgstr for that, i searching solution for that too, this is not correct. – Svisstack Jan 06 '13 at 00:12
  • How about doing `msgattrib --no-fuzzy` followed by `msgmerge` with the template? That should bring back empty messages from the template. – Mikko Rantalainen Apr 26 '13 at 07:43
  • This removes fuzzy entries. OP wants to clear fuzzy translations and remove the fuzzy flag, but keep the entries. – Sam Kauffman Dec 05 '17 at 23:18