Beispiel #1
0
    def __parse_xliff(self, contents):
        ns = {"xliff": "urn:oasis:names:tc:xliff:document:1.2"}

        tree = ElementTree.parse(self.xliff_path)
        root = tree.getroot()

        for file in root:
            localized_contents = LocalizedContents()
            localized_contents.file = file.get("original")
            localized_contents.source = file.get("source-language")
            localized_contents.target = file.get("target-language")

            header = file.find("xliff:header", ns)
            header_tool = header.find("xliff:tool", ns)
            tool = Tool()
            tool.id = header_tool.get("tool-id")
            tool.name = header_tool.get("tool-name")
            tool.version = header_tool.get("tool-version")
            tool.build = header_tool.get("build-num")
            localized_contents.tool = tool

            for trans_unit in (body := file.find("xliff:body", ns)):
                translation = Translation()
                translation.id = trans_unit.get("id")

                if (source_tag := trans_unit.find("xliff:source",
                                                  ns)) is not None:
                    translation.source = source_tag.text

                if (target_tag := trans_unit.find("xliff:target",
                                                  ns)) is not None:
                    translation.target = target_tag.text
    def __parse_translation_sheets(self, contents, wb):
        # Get metadata
        index = wb["_metadata_"]
        temp_localized_contents = []
        for i in range(4, index.max_row + 1):
            localized_contents = LocalizedContents()

            localized_contents.file = index["C" + str(i)].value
            localized_contents.source = index["D" + str(i)].value
            localized_contents.target = index["E" + str(i)].value

            tool = Tool()
            tool.id = index["F" + str(i)].value
            tool.name = index["G" + str(i)].value
            tool.version = index["H" + str(i)].value
            tool.build = index["I" + str(i)].value
            localized_contents.tool = tool

            temp_localized_contents.append(localized_contents)

        # Parse all sheets
        for sheetname in wb.sheetnames:
            if sheetname == "_metadata_":
                continue

            ws = wb[sheetname]
            row = 1

            if ws["A" + str(row)].value != "No":
                row += 1

            if ws["A" + str(row)].value != "No":
                print("Sheet format is invalidated : {}".format(sheetname))
                continue

            row += 1

            # Get localized content with metadata
            filtered_localized_contents = list(
                filter(lambda c: c.basename == sheetname,
                       temp_localized_contents))
            if filtered_localized_contents == False:
                print("Sheet metadata not found: {}".format(sheetname))
                continue

            localized_contents = filtered_localized_contents[0]

            # Add translations
            for i in range(row, ws.max_row + 1):
                translation = Translation()
                translation.id = ws["B" + str(i)].value
                translation.source = ws["C" + str(i)].value
                translation.target = ws["D" + str(i)].value
                translation.note = ws["E" + str(i)].value

                localized_contents.translations.append(translation)

            contents.localized_contents.append(localized_contents)