Add option to overwrite existing translations

This is useful if you know that the translation of a particular language has
not been updated at all in master, but the stable version has been worked on.
This commit is contained in:
Georg Baum 2015-12-03 21:02:47 +01:00
parent be7df4a13c
commit a6b5c72820

View File

@ -59,15 +59,24 @@ def parse_msg(lines):
return polib.unescape(msg)
def translate(msgid, msgstr_lines, po2):
def translate(msgid, msgstr_lines, po2, overwrite):
msgstr = parse_msg(msgstr_lines)
if msgstr != '':
return 0
other = po2.find(msgid)
if not other:
return 0
if not other.translated():
return 0
if overwrite:
other = po2.find(msgid)
if not other:
return 0
if not other.translated():
return 0
if msgstr == other.msgstr:
return 0
else:
if msgstr != '':
return 0
other = po2.find(msgid)
if not other:
return 0
if not other.translated():
return 0
msgstr = other.msgstr
obsolete = (msgstr_lines[0].find('#~') == 0)
j = msgstr_lines[0].find('"')
@ -84,23 +93,32 @@ def translate(msgid, msgstr_lines, po2):
return 1
def mergepo_polib(target, source):
def mergepo_polib(target, source, overwrite):
changed = 0
po1 = polib.pofile(target)
po2 = polib.pofile(source)
for entry in po1.untranslated_entries():
other = po2.find(entry.msgid, include_obsolete_entries=True)
if not other:
continue
if other.translated():
entry.msgstr = other.msgstr
changed = changed + 1
if overwrite:
for entry in po1.entries():
other = po2.find(entry.msgid, include_obsolete_entries=True)
if not other:
continue
if other.translated() and other.msgstr != entry.msgstr:
entry.msgstr = other.msgstr
changed = changed + 1
else:
for entry in po1.untranslated_entries():
other = po2.find(entry.msgid, include_obsolete_entries=True)
if not other:
continue
if other.translated():
entry.msgstr = other.msgstr
changed = changed + 1
if changed > 0:
po1.save(target)
return changed
def mergepo_minimaldiff(target, source):
def mergepo_minimaldiff(target, source, overwrite):
changed = 0
po2 = polib.pofile(source)
target_enc = polib.detect_encoding(target)
@ -131,7 +149,7 @@ def mergepo_minimaldiff(target, source):
msgstr_lines.append(line)
else:
in_msgstr = False
changed = changed + translate(msgid, msgstr_lines, po2)
changed = changed + translate(msgid, msgstr_lines, po2, overwrite)
newlines.extend(msgstr_lines)
msgstr_lines = []
msgid = ''
@ -149,7 +167,7 @@ def mergepo_minimaldiff(target, source):
newlines.append(line)
if msgid != '':
# the file ended with a msgstr
changed = changed + translate(msgid, msgstr_lines, po2)
changed = changed + translate(msgid, msgstr_lines, po2, overwrite)
newlines.extend(msgstr_lines)
msgstr_lines = []
msgid = ''
@ -162,7 +180,7 @@ def mergepo_minimaldiff(target, source):
return changed
def mergepo(target, source):
def mergepo(target, source, overwrite):
if not os.path.exists(source):
sys.stderr.write('Skipping %s since %s does not exist.\n' % (target, source))
return
@ -171,9 +189,9 @@ def mergepo(target, source):
return
sys.stderr.write('Merging %s into %s: ' % (source, target))
try:
changed = mergepo_minimaldiff(target, source)
changed = mergepo_minimaldiff(target, source, overwrite)
except:
changed = mergepo_polib(target, source)
changed = mergepo_polib(target, source, overwrite)
sys.stderr.write('Updated %d translations.\n' % changed)
@ -188,6 +206,8 @@ yet in the target .po files are not updated.""", usage = "Usage: %prog [options]
help="target directory containing .po files. If missing, it is determined from the script location.")
parser.add_option("-l", "--language", dest="language",
help="language for which translations are merged (if missing, all languages are merged)")
parser.add_option("-o", "--overwrite", action="store_true", dest="overwrite", default=False,
help="overwrite existing target translations with source translations (if missing, only new translations are added)")
(options, args) = parser.parse_args(argv)
if len(args) <= 1:
parser.print_help()
@ -202,13 +222,13 @@ yet in the target .po files are not updated.""", usage = "Usage: %prog [options]
if options.language:
name = options.language + '.po'
mergepo(os.path.join(podir1, name), os.path.join(podir2, name))
mergepo(os.path.join(podir1, name), os.path.join(podir2, name), options.overwrite)
else:
for i in os.listdir(podir1):
(base, ext) = os.path.splitext(i)
if ext != ".po":
continue
mergepo(os.path.join(podir1, i), os.path.join(podir2, i))
mergepo(os.path.join(podir1, i), os.path.join(podir2, i), options.overwrite)
return 0