def convert_Excel2Xliff(self, src, dst): src_wb = openpyxl.load_workbook(src) src_ws = src_wb.worksheets[0] en_col = 1 th_col = 2 for col in range(1,src_ws.max_column): cell_value = src_ws.cell(1, col).value.lower() if 'en' == cell_value: en_col = col if 'th' == cell_value: th_col = col xliff_file = xlifffile() xliff_file.setsourcelanguage('en') xliff_file.settargetlanguage('th') for row in range(2, src_ws.max_row+1): new_node = xliffunit(src_ws.cell(row, en_col).value) new_node.settarget(src_ws.cell(row, th_col).value) xliff_file.addunit(new_node) xliff_file.savefile(dst) fin = open(dst, "r", encoding='utf-8') data = fin.read() fin.close() data = data.replace('<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" version="1.1">', '<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">') fout = open(dst, 'w', encoding='utf-8') fout.write(data) fout.close() QMessageBox.information(self, "Information", "Converting was done successfully")
def maketargetunit(self, part1, part2, translators_comment, key, subkey): """makes a base unit (.po or XLIFF) out of a subkey of two parts""" #TODO: Do better text1 = getattr(part1, subkey) if text1 == "": return None text2 = getattr(part2, subkey) unit = xliff.xliffunit(text1) unit.target = text2 if unit.target: unit.markfuzzy(False) else: unit.markfuzzy(True) unit.addlocation(key + "." + subkey) if getattr(translators_comment, subkey).strip() != "": unit.addnote(getattr(translators_comment, subkey), origin="developer") return unit
def setsource(self, source, sourcelang="en"): # TODO: consider changing from plural to singular, etc. self._rich_source = None if not hasplurals(source): super(PoXliffUnit, self).setsource(source, sourcelang) else: target = self.target for unit in self.units: try: self.xmlelement.remove(unit.xmlelement) except xml.dom.NotFoundErr: pass self.units = [] for s in source.strings: newunit = xliff.xliffunit(s) # newunit.namespace = self.namespace #XXX?necessary? self.units.append(newunit) self.xmlelement.append(newunit.xmlelement) self.target = target
def convert_Tmx2Xliff(self, src, dst): xliff_file = xlifffile() xliff_file.setsourcelanguage('en') xliff_file.settargetlanguage('th') with open(src, 'rb') as fin: tmx_file = tmxfile(fin, 'en', 'th') for node in tmx_file.unit_iter(): new_node = xliffunit(node.getsource()) new_node.settarget(node.gettarget()) xliff_file.addunit(new_node) xliff_file.savefile(dst) fin = open(dst, "r", encoding='utf-8') data = fin.read() fin.close() data = data.replace('<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" version="1.1">', '<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">') fout = open(dst, 'w', encoding='utf-8') fout.write(data) fout.close() QMessageBox.information(self, "Information", "Converting was done successfully")
def add_translatable_to_store(parent_translatable, translatable): """Construct a new translation unit, set its source and location information and add it to 'store'. """ xliff_unit = xliffunit(u'') placeables = _to_placeables(parent_translatable, translatable, id_maker) xliff_unit.rich_source = [StringElem(placeables)] # Get the plain text for the unit source. The output is enclosed within # XLIFF source tags we don't want, so strip them. unit_source = etree.tostring(xliff_unit.source_dom) unit_source = unit_source[unit_source.find(">", 1) + 1:] unit_source = unit_source[:unit_source.rfind("<", 1)] # Create the PO unit and add it to the PO store. po_unit = store.UnitClass(unit_source) po_unit.addlocation(translatable.xpath) po_unit.addlocation(filename) store.addunit(po_unit)
def convert_Text2Xliff(self, src, dst): xliff_file = xlifffile() xliff_file.setsourcelanguage('en') xliff_file.settargetlanguage('th') lines = open(src, encoding='utf-8').read().strip().split('\n') for line in lines: s = line.split(delimiter) node = xliffunit(s[0]) node.settarget(s[1]) xliff_file.addunit(node) xliff_file.savefile(dst) fin = open(dst, "r", encoding='utf-8') data = fin.read() fin.close() data = data.replace('<xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" version="1.1">', '<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2">') fout = open(dst, 'w', encoding='utf-8') fout.write(data) fout.close() QMessageBox.information(self, "Information", "Converting was done successfully")