Skip to content

Instantly share code, notes, and snippets.

@minorua
Created June 30, 2015 21:22
Show Gist options
  • Save minorua/788243accef7edaf4ab2 to your computer and use it in GitHub Desktop.
Save minorua/788243accef7edaf4ab2 to your computer and use it in GitHub Desktop.
A script to backport latest translations to 2.8 branch
# -*- coding: utf-8 -*-
# A script to backport latest translations to 2.8 branch
# 2015-06-30
import codecs
from xml.dom import minidom
from xml.etree import ElementTree
class Message:
def __init__(self, index, context, source, translation, msg_type=None):
self.index = index
self.context = context
self.source = source
self.translation = translation
self.type = msg_type
def escape(string):
if string is None:
return ""
return string.replace("\t", "\\t").replace("\n", "\\n")
def loadTsFile(filename):
tree = ElementTree.parse(filename)
index = 0
messages = {}
for context in tree.getroot():
context_name = context.find("name").text
messages[context_name] = {}
for message in context.findall("message"):
source = message.find("source")
translation = message.find("translation")
msg_type = translation.get("type")
if msg_type == "obsolete":
continue
numerusform = translation.find("numerusform")
if numerusform is not None:
translation = numerusform
source = source.text
translation = translation.text
messages[context_name][source] = Message(index, context_name, source, translation, msg_type)
index += 1
return messages
def backport_ts(ts_from, ts_to):
messages_from = loadTsFile(ts_from)
messages_to = loadTsFile(ts_to)
stats = {"added": 0, "updated": 0}
doc = minidom.parse(ts_to)
for context in doc.getElementsByTagName("context"):
context_name = context.getElementsByTagName("name")[0].childNodes[0].data
messages = context.getElementsByTagName("message")
for i, msg in enumerate(messages):
source = msg.getElementsByTagName("source")[0].childNodes[0].data
t = msg.getElementsByTagName("translation")[0]
status = t.getAttribute("type")
msg_from = messages_from.get(context_name, {}).get(source)
msg_to = messages_to.get(context_name, {}).get(source)
if msg_from and msg_from.translation and msg_to and msg_to.translation:
if msg_from.translation != msg_to.translation:
t.childNodes[0].data = msg_from.translation
print "update: %s, %s" % (context_name, escape(source))
stats["updated"] += 1
elif status == "unfinished" and len(t.childNodes) == 0:
if msg_from and msg_from.translation:
t.removeAttribute("type")
t.appendChild(doc.createTextNode(msg_from.translation))
print "found: %s, %s" % (context_name, escape(source))
stats["added"] += 1
print str(stats)
# write xml
f = codecs.open('./backport/backported.ts', 'w', 'utf-8')
txt = doc.toxml()
txt = txt.replace('<?xml version="1.0" ?><!DOCTYPE TS><TS language="ja" version="2.0">', '<?xml version="1.0" encoding="utf-8"?>\n<!DOCTYPE TS>\n<TS version="2.0" language="ja">')
txt = txt.replace("'", "&apos;")
#txt = txt.replace("<translation/>", "<translation></translation>")
txt = txt.replace('<translation type="unfinished"/>', '<translation type="unfinished"></translation>')
f.write(txt + "\n")
f.close()
if __name__ == "__main__":
import sys
# from: latest translation file
# to: 2.8 translation file (updated with update_ts_files.sh)
backport_ts("translations/QGIS.qgis-application/ja.ts", "backport/qgis_ja.ts")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment