Skip to content

Instantly share code, notes, and snippets.

@GaretJax
Created November 13, 2014 07:33
Show Gist options
  • Save GaretJax/301dbfb0a47496562e89 to your computer and use it in GitHub Desktop.
Save GaretJax/301dbfb0a47496562e89 to your computer and use it in GitHub Desktop.
Remove fuzzy markers from manually updated translation files.

Simple script to remove the fuzzy keyword from translations files when those have been translated manually. Needs a file containing the modified lines, which can be created with the followign script:

git diff base.po new.po | grep '^+[^#]' > updated_lines

It can then be invoked with:

./remove_fuzzy.py new.po updated_lines new_removed.po

The resulting file can then be merged (i.e., with pomerge):

pomerge -t base.po -i new_removed.po updated.po
from __future__ import print_function
import sys
translations_file = sys.argv[1]
updated_lines_file = sys.argv[2]
if len(sys.argv) == 4:
output_file = sys.argv[3]
else:
output_file = translations_file
with open(translations_file, 'r') as fh:
translation_lines = list(fh)
with open(updated_lines_file, 'r') as fh:
updated_lines = list(fh)[1:]
i = len(translation_lines) - 1
while i >= 0:
if not updated_lines:
# All lines processed
break
if translation_lines[i] != updated_lines[-1][1:]:
# Not the line we are looking for
i -= 1
continue
# Line is a match, look for fuzzy, but keep i
j = i - 1
del updated_lines[-1]
while j > 0:
if translation_lines[j].startswith('#, fuzzy'):
# Fuzzy found, remove and continue
del translation_lines[j]
i -= 1
break
if (translation_lines[j].startswith('#: ')
or not translation_lines[j].strip()):
# Fuzzy not found, not looking further
break
# Search prev line
j -= 1
if updated_lines:
print('Not all modified lines found, not saving file.')
else:
with open(output_file, 'w') as fh:
fh.write(''.join(translation_lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment