#!/usr/bin/env python # -* coding: utf-8 -*- import sys, re from itertools import * from polib import * def info(name,alt): print "{0}: {1} translated, {2} untranslated, {3} obsolete, {4} fuzzy.".format( name, len(alt.translated_entries()), len(alt.untranslated_entries()), len(alt.obsolete_entries()), len(alt.fuzzy_entries()) ) def hack(dst, tra): rehacks=( re.compile(r"(?:^|(?<=\n))((?:\\t|\s|\t)*)(\S*)((?:\\t|\s|\t)*)(.+?)([.:!]*(?:[\n]|$))"), ) def cha0(el,reh): return "".join([ch[1]+ch[3] for ch in reh.findall(el)]) mf={cha0(e,reh):tra[e] for reh in rehacks for e in tra if len(tra[e])>1} for e in dst: if e.msgid and not e.msgstr: for reh in rehacks: key = cha0(e.msgid,reh) if key in mf and mf[key]: mesg, trans = reh.findall(e.msgid), reh.findall(mf[key]) if mesg and trans: print (e.msgid,) print mesg, trans e.msgstr = "".join([m[0]+t[1]+m[2]+t[3]+m[4] for t,m in zip(trans,mesg)]) def extran(src): rehacks=( ( re.compile(r"(.*)", re.DOTALL), r"\1"), ( re.compile(r" WARN "), r" %s "), ( re.compile(r"'([^']*)'"), r'"\1"'), ) tra={} # patched for e in src: if e.msgstr: for rep, to in rehacks: tra[rep.sub(to, e.msgid)]=rep.sub(to, e.msgstr) for k in tra.keys(): if k.count('\n')>1: kk,tt = k.split('\n'), tra[k].split('\n') if len(kk) == len(tt): for ke, te in zip(kk,tt): tra[ke]=te return tra def keepeng(dst): for e in dst: if not e.msgstr: # TODO autotranslate some autogeneratings e.msgstr = e.msgid e.flags.append("fuzzy") def rmerrs(dst): '''Errors produced by ./checkpo''' rehacks=( (re.compile(r"(.*?)(!+|[.]{2,})?[\n]*$",re.DOTALL),r"\1"), ) for e in dst: for reh, rep in rehacks: e.msgstr = reh.sub(rep, e.msgstr) dst = pofile(sys.argv[1]) info("dst",dst) traslations ={} for fsrc in sys.argv[1:]: src = pofile(fsrc) info(fsrc,src) traslations.update(extran(src)) hack(dst, traslations) info("hacked",dst) keepeng(dst) info("keep englsh",dst) rmerrs(dst) dst.save(sys.argv[1])