def partial_translate(inpath, refpath, outpath, dc): dict_in = extract_xml(inpath) dict_ref = extract_xml(refpath) key_ref = dict_ref.keys() inc = dc.keys() - key_ref identical = set() for k in inc: if dc[k] == dict_in[k]: identical.add(k) inc -= identical if len(inc) == 0: print("No new translations added, skip") return print(len(inc), inc) def find_elem_by_id(tree, id): e = None if "." in id: # needs transform if ".product." in id: ss = id.split(".", 3) xpath = "/string[@name='%s'][@product='%s']"% (ss[0],ss[2]) e = tree.find(xpath) elif id.startswith("array."): ss = id.split(".", 3) xpath = "/string-array[@name='%s']"%ss[1] e = tree.find(xpath) elif id.startswith("plurals."): #TODO print("!!! ERROR: plurals not supported yet !!!") else: print("!!! complex representation !!! " + id) raise NotImplementedError else: xpath = "/string[@name='%s']"%id e = tree.find(xpath) return e appendix = [] tree1 = etree.parse(inpath) for id in inc: elem = find_elem_by_id(tree1, id) if (elem is None) or (elem in appendix): pass else: elem.tail = '\n ' appendix.append(elem) for e in appendix: translate_element(e, dc) tree2 = etree.parse(refpath) tree2.getroot()[-1].tail = '\n ' tree2.getroot().extend(appendix) os.makedirs(os.path.dirname(outpath), exist_ok=True) tree2.getroot()[-1].tail = '\n' tree2.write(outpath, encoding="utf-8", xml_declaration=True, method="xml")
def partial_translate(inpath, refpath, outpath, dc): dict_in = extract_xml(inpath) dict_ref = extract_xml(refpath) key_ref = dict_ref.keys() inc = dc.keys() - key_ref identical = set() for k in inc: if dc[k] == dict_in[k]: identical.add(k) inc -= identical if len(inc) == 0: print("No new translations added, skip") return print(len(inc), inc) def find_elem_by_id(tree, id): e = None if "." in id: # needs transform if ".product." in id: ss = id.split(".", 3) xpath = "/string[@name='%s'][@product='%s']" % (ss[0], ss[2]) e = tree.find(xpath) elif id.startswith("array."): ss = id.split(".", 3) xpath = "/string-array[@name='%s']" % ss[1] e = tree.find(xpath) elif id.startswith("plurals."): #TODO print("!!! ERROR: plurals not supported yet !!!") else: print("!!! complex representation !!! " + id) raise NotImplementedError else: xpath = "/string[@name='%s']" % id e = tree.find(xpath) return e appendix = [] tree1 = etree.parse(inpath) for id in inc: elem = find_elem_by_id(tree1, id) if (elem is None) or (elem in appendix): pass else: elem.tail = '\n ' appendix.append(elem) for e in appendix: translate_element(e, dc) tree2 = etree.parse(refpath) tree2.getroot()[-1].tail = '\n ' tree2.getroot().extend(appendix) os.makedirs(os.path.dirname(outpath), exist_ok=True) tree2.getroot()[-1].tail = '\n' tree2.write(outpath, encoding="utf-8", xml_declaration=True, method="xml")
def process_app(base, res): flist = os.listdir(os.path.join(base, res, 'values')) print(flist) for f in flist: csv_writer.writerow([]) csv_writer.writerow([res]) csv_writer.writerow([f]) dicts = [] for lang in languages: abspath = os.path.join(base, res, lang, f) dicts.append(extract_xml(abspath)) ''' abspath = os.path.join(base, res, languages[1], f) dict1 = extract_xml(abspath) abspath = os.path.join(base, res, languages[2], f) dict2 = extract_xml(abspath) ''' dict_all = merge_dicts(*dicts) for k in sorted(dict_all.keys()): it = dict_all[k] #print('%s, "%s", "%s", "%s"'%(k,it[0],it[1],it[2]), file=fp) #print(k,it, file=fp) # if English string is empty, or all languages are translated # no bother to output it lens = list(map(len, it)) #if lens[0] and (0 in lens[1:]): # csv_writer.writerow([k] + list(it)) csv_writer.writerow([k] + list(it)) print(len(dict_all))
def process_app(base, res): csv_writer.writerow([]) csv_writer.writerow([res]) flist = os.listdir(os.path.join(base, res, 'values')) print(flist) for f in flist: csv_writer.writerow([f]) # English abspath = os.path.join(base, res, "values", f) dict_en = extract_xml(abspath) # Chinese abspath = os.path.join(base, res, "values-zh-rCN", f) dict_zh = extract_xml(abspath) # Hindi abspath = os.path.join(base, res, "values-hi", f) dict_hi = extract_xml(abspath) dict_all = merge_dict3(dict_en, dict_zh, dict_hi) for k in dict_all: it = dict_all[k] #print('%s, "%s", "%s", "%s"'%(k,it[0],it[1],it[2]), file=fp) #print(k,it, file=fp) csv_writer.writerow([k] + list(it)) print(len(dict_all))