Пример #1
0
def parse(path, source_path=None, locale=None):
    with open(path) as f:
        # To be removed as part of bug 1600344.
        if sys.version_info.major == 2:
            xliff_file = xliff.xlifffile(f)
        else:
            xml = f.read().encode("utf-8")
            xliff_file = xliff.xlifffile(xml)
    return XLIFFResource(path, xliff_file)
Пример #2
0
    def test_rich_target(self):
        xlifffile = xliff.xlifffile()
        xliffunit = xlifffile.addsourceunit(u'')

        # Test 1
        xliffunit.set_rich_target([StringElem([u'foo', X(id='bar'), u'baz'])], u'fr')
        target_dom_node = xliffunit.getlanguageNode(None, 1)
        x_placeable = target_dom_node[0]

        assert target_dom_node.text == 'foo'
        assert x_placeable.tag == u'x'
        assert x_placeable.attrib['id'] == 'bar'
        assert x_placeable.tail == 'baz'

        # Test 2
        xliffunit.set_rich_target([StringElem([u'foo', u'baz', G(id='oof', sub=[G(id='zab', sub=[u'bar', u'rab'])])])], u'fr')
        target_dom_node = xliffunit.getlanguageNode(None, 1)
        g_placeable = target_dom_node[0]
        nested_g_placeable = g_placeable[0]

        assert target_dom_node.text == u'foobaz'

        assert g_placeable.tag == u'g'
        print 'g_placeable.text: %s (%s)' % (g_placeable.text, type(g_placeable.text))
        assert g_placeable.text is None
        assert g_placeable.attrib[u'id'] == u'oof'
        assert g_placeable.tail is None

        assert nested_g_placeable.tag == u'g'
        assert nested_g_placeable.text == u'barrab'
        assert nested_g_placeable.attrib[u'id'] == u'zab'
        assert nested_g_placeable.tail is None

        xliffunit.rich_target[0].print_tree(2)
        assert xliffunit.rich_target == [StringElem([u'foobaz', G(id='oof', sub=[G(id='zab', sub=[u'barrab'])])])]
Пример #3
0
 def test_target(self):
     xlifffile = xliff.xlifffile()
     xliffunit = xlifffile.addsourceunit("Concept")
     xliffunit.target = "Konsep"
     newfile = xliff.xlifffile.parsestring(str(xlifffile))
     print str(xlifffile)
     assert newfile.findunit("Concept").target == "Konsep"
Пример #4
0
    def test_notes(self):
        xlifffile = xliff.xlifffile()
        unit = xlifffile.addsourceunit("Concept")
        # We don't want to add unnecessary notes
        assert not "note" in str(xlifffile)
        unit.addnote(None)
        assert not "note" in str(xlifffile)
        unit.addnote("")
        assert not "note" in str(xlifffile)

        unit.addnote("Please buy bread")
        assert unit.getnotes() == "Please buy bread"
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 1

        unit.addnote("Please buy milk", origin="Mom")
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 2
        assert not "from" in notenodes[0].attrib
        assert notenodes[1].get("from") == "Mom"
        assert unit.getnotes(origin="Mom") == "Please buy milk"

        unit.addnote("Don't forget the beer", origin="Dad")
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 3
        assert notenodes[1].get("from") == "Mom"
        assert notenodes[2].get("from") == "Dad"
        assert unit.getnotes(origin="Dad") == "Don't forget the beer"

        assert not unit.getnotes(origin="Bob") == "Please buy bread\nPlease buy milk\nDon't forget the beer"
        assert not notenodes[2].get("from") == "Mom"
        assert not "from" in notenodes[0].attrib
        assert unit.getnotes() == "Please buy bread\nPlease buy milk\nDon't forget the beer"
        assert unit.correctorigin(notenodes[2], "ad") == True
        assert unit.correctorigin(notenodes[2], "om") == False
Пример #5
0
    def test_notes(self):
        xlifffile = xliff.xlifffile()
        unit = xlifffile.addsourceunit("Concept")
        # We don't want to add unnecessary notes
        assert "note" not in bytes(xlifffile).decode('utf-8')
        unit.addnote(None)
        assert "note" not in bytes(xlifffile).decode('utf-8')
        unit.addnote("")
        assert "note" not in bytes(xlifffile).decode('utf-8')

        unit.addnote("Please buy bread")
        assert unit.getnotes() == "Please buy bread"
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 1

        unit.addnote("Please buy milk", origin="Mom")
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 2
        assert "from" not in notenodes[0].attrib
        assert notenodes[1].get("from") == "Mom"
        assert unit.getnotes(origin="Mom") == "Please buy milk"

        unit.addnote("Don't forget the beer", origin="Dad")
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 3
        assert notenodes[1].get("from") == "Mom"
        assert notenodes[2].get("from") == "Dad"
        assert unit.getnotes(origin="Dad") == "Don't forget the beer"

        assert not unit.getnotes(origin="Bob") == "Please buy bread\nPlease buy milk\nDon't forget the beer"
        assert not notenodes[2].get("from") == "Mom"
        assert "from" not in notenodes[0].attrib
        assert unit.getnotes() == "Please buy bread\nPlease buy milk\nDon't forget the beer"
        assert unit.correctorigin(notenodes[2], "ad")
        assert not unit.correctorigin(notenodes[2], "om")
Пример #6
0
    def test_rich_target(self):
        xlifffile = xliff.xlifffile()
        xliffunit = xlifffile.addsourceunit(u'')

        # Test 1
        xliffunit.set_rich_target([StringElem([u'foo', X(id='bar'), u'baz'])], u'fr')
        target_dom_node = xliffunit.getlanguageNode(None, 1)
        x_placeable = target_dom_node[0]

        assert target_dom_node.text == 'foo'
        assert x_placeable.tag == u'x'
        assert x_placeable.attrib['id'] == 'bar'
        assert x_placeable.tail == 'baz'

        # Test 2
        xliffunit.set_rich_target([StringElem([u'foo', u'baz', G(id='oof', sub=[G(id='zab', sub=[u'bar', u'rab'])])])], u'fr')
        target_dom_node = xliffunit.getlanguageNode(None, 1)
        g_placeable = target_dom_node[0]
        nested_g_placeable = g_placeable[0]

        assert target_dom_node.text == u'foobaz'

        assert g_placeable.tag == u'g'
        print('g_placeable.text: %s (%s)' % (g_placeable.text, type(g_placeable.text)))
        assert g_placeable.text is None
        assert g_placeable.attrib[u'id'] == u'oof'
        assert g_placeable.tail is None

        assert nested_g_placeable.tag == u'g'
        assert nested_g_placeable.text == u'barrab'
        assert nested_g_placeable.attrib[u'id'] == u'zab'
        assert nested_g_placeable.tail is None

        xliffunit.rich_target[0].print_tree(2)
        assert xliffunit.rich_target == [StringElem([u'foobaz', G(id='oof', sub=[G(id='zab', sub=[u'barrab'])])])]
Пример #7
0
 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")
Пример #8
0
    def test_notes(self):
        xlifffile = xliff.xlifffile()
        unit = xlifffile.addsourceunit("Concept")
        unit.addnote("Please buy bread")
        assert unit.getnotes() == "Please buy bread"
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 1

        unit.addnote("Please buy milk", origin="Mom")
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 2
        assert not "from" in notenodes[0].attrib
        assert notenodes[1].get("from") == "Mom"
        assert unit.getnotes(origin="Mom") == "Please buy milk"

        unit.addnote("Don't forget the beer", origin="Dad")
        notenodes = unit.xmlelement.findall(".//%s" % unit.namespaced("note"))
        assert len(notenodes) == 3
        assert notenodes[1].get("from") == "Mom"
        assert notenodes[2].get("from") == "Dad"
        assert unit.getnotes(origin="Dad") == "Don't forget the beer"

        assert not unit.getnotes(origin="Bob") == "Please buy bread\nPlease buy milk\nDon't forget the beer"
        assert not notenodes[2].get("from") == "Mom"
        assert not "from" in notenodes[0].attrib
        assert unit.getnotes() == "Please buy bread\nPlease buy milk\nDon't forget the beer"
        assert unit.correctorigin(notenodes[2], "ad") == True
        assert unit.correctorigin(notenodes[2], "om") == False
Пример #9
0
 def test_target(self):
     xlifffile = xliff.xlifffile()
     xliffunit = xlifffile.addsourceunit("Concept")
     xliffunit.target = "Konsep"
     newfile = xliff.xlifffile.parsestring(str(xlifffile))
     print str(xlifffile)
     assert newfile.findunit("Concept").target == "Konsep"
Пример #10
0
 def mergexliff(
     self,
     templatesource,
     inputsource,
     mergeblanks="yes",
     mergefuzzy="yes",
     mergecomments="yes",
 ):
     """merges the sources of the given files and returns a new xlifffile object"""
     templatefile = BytesIO(templatesource.encode())
     inputfile = BytesIO(inputsource.encode())
     outputfile = BytesIO()
     assert pomerge.mergestore(
         inputfile,
         outputfile,
         templatefile,
         mergeblanks=mergeblanks,
         mergefuzzy=mergefuzzy,
         mergecomments=mergecomments,
     )
     outputxliffstring = outputfile.getvalue()
     print("Generated XML:")
     print(outputxliffstring)
     outputxlifffile = xliff.xlifffile(outputxliffstring)
     return outputxlifffile
Пример #11
0
 def test_source(self):
     xlifffile = xliff.xlifffile()
     xliffunit = xlifffile.addsourceunit("Concept")
     xliffunit.source = "Term"
     newfile = xliff.xlifffile.parsestring(str(xlifffile))
     print str(xlifffile)
     assert newfile.findunit("Concept") is None
     assert newfile.findunit("Term") is not None
Пример #12
0
 def test_source(self):
     xlifffile = xliff.xlifffile()
     xliffunit = xlifffile.addsourceunit("Concept")
     xliffunit.source = "Term"
     newfile = xliff.xlifffile.parsestring(str(xlifffile))
     print str(xlifffile)
     assert newfile.findunit("Concept") is None
     assert newfile.findunit("Term") is not None
Пример #13
0
 def convertfile(storefile):
     store = xlifffile()
     store.setfilename(store.getfilenode("NoName"), "odf")
     contents = open_odf(storefile)
     for data in contents.values():
         parse_state = ParseState(no_translate_content_elements, inline_elements)
         build_store(BytesIO(data), store, parse_state)
     return store
Пример #14
0
def parse(path, source_path=None, locale=None):
    with open(path) as f:
        xml = f.read().encode("utf-8")

        try:
            xliff_file = xliff.xlifffile(xml)
        except etree.XMLSyntaxError as err:
            raise ParseError(f"Failed to parse {path}: {err}")

    return XLIFFResource(path, xliff_file)
Пример #15
0
    def test_preserve_filename(self):
        """Ensures that the filename is preserved."""
        xliffsource = self.xliffskeleton % '''<trans-unit xml:space="preserve">
        <source>nonsense</source>
        <target>matlhapolosa</target>
</trans-unit>'''
        self.create_testfile("snippet.xlf", xliffsource)
        xlifffile = xliff.xlifffile(self.open_testfile("snippet.xlf"))
        assert xlifffile.filename.endswith("snippet.xlf")
        xlifffile.parse(xliffsource)
        assert xlifffile.filename.endswith("snippet.xlf")
Пример #16
0
 def test_basic(self):
     xlifffile = xliff.xlifffile()
     assert xlifffile.units == []
     xlifffile.addsourceunit("Bla")
     assert len(xlifffile.units) == 1
     newfile = xliff.xlifffile.parsestring(str(xlifffile))
     print str(xlifffile)
     assert len(newfile.units) == 1
     assert newfile.units[0].source == "Bla"
     assert newfile.findunit("Bla").source == "Bla"
     assert newfile.findunit("dit") is None
Пример #17
0
 def mergexliff(self, templatesource, inputsource):
     """merges the sources of the given files and returns a new xlifffile object"""
     templatefile = wStringIO.StringIO(templatesource)
     inputfile = wStringIO.StringIO(inputsource)
     outputfile = wStringIO.StringIO()
     assert pomerge.mergestore(inputfile, outputfile, templatefile)
     outputxliffstring = outputfile.getvalue()
     print "Generated XML:"
     print outputxliffstring
     outputxlifffile = xliff.xlifffile(outputxliffstring)
     return outputxlifffile
Пример #18
0
 def convertstore(self, theoofile, duplicatestyle="msgctxt"):
     """converts an entire oo file to a base class format (.po or XLIFF)"""
     thetargetfile = xliff.xlifffile()
     thetargetfile.setsourcelanguage(self.sourcelanguage)
     thetargetfile.settargetlanguage(self.targetlanguage)
     # go through the oo and convert each element
     for theoo in theoofile.units:
         unitlist = self.convertelement(theoo)
         for unit in unitlist:
             thetargetfile.addunit(unit)
     return thetargetfile
Пример #19
0
 def test_basic(self):
     xlifffile = xliff.xlifffile()
     assert xlifffile.units == []
     xlifffile.addsourceunit("Bla")
     assert len(xlifffile.units) == 1
     newfile = xliff.xlifffile.parsestring(str(xlifffile))
     print str(xlifffile)
     assert len(newfile.units) == 1
     assert newfile.units[0].source == "Bla"
     assert newfile.findunit("Bla").source == "Bla"
     assert newfile.findunit("dit") is None
Пример #20
0
    def test_rich_source():
        xlifffile = xliff.xlifffile()
        xliffunit = xlifffile.addsourceunit("")

        # Test 1
        xliffunit.rich_source = [StringElem(["foo", X(id="bar"), "baz"])]
        source_dom_node = xliffunit.getlanguageNode(None, 0)
        x_placeable = source_dom_node[0]

        assert source_dom_node.text == "foo"

        assert x_placeable.tag == "x"
        assert x_placeable.attrib["id"] == "bar"
        assert x_placeable.tail == "baz"

        xliffunit.rich_source[0].print_tree(2)
        print(xliffunit.rich_source)
        assert xliffunit.rich_source == [
            StringElem([StringElem("foo"),
                        X(id="bar"),
                        StringElem("baz")])
        ]

        # Test 2
        xliffunit.rich_source = [
            StringElem([
                "foo", "baz",
                G(id="oof", sub=[G(id="zab", sub=["bar", "rab"])])
            ])
        ]
        source_dom_node = xliffunit.getlanguageNode(None, 0)
        g_placeable = source_dom_node[0]
        nested_g_placeable = g_placeable[0]

        assert source_dom_node.text == "foobaz"

        assert g_placeable.tag == "g"
        assert g_placeable.text is None
        assert g_placeable.attrib["id"] == "oof"
        assert g_placeable.tail is None

        assert nested_g_placeable.tag == "g"
        assert nested_g_placeable.text == "barrab"
        assert nested_g_placeable.attrib["id"] == "zab"
        assert nested_g_placeable.tail is None

        rich_source = xliffunit.rich_source
        rich_source[0].print_tree(2)
        assert rich_source == [
            StringElem(
                ["foobaz",
                 G(id="oof", sub=[G(id="zab", sub=["barrab"])])])
        ]
Пример #21
0
    def test_rich_source(self):
        xlifffile = xliff.xlifffile()
        xliffunit = xlifffile.addsourceunit(u'')

        # Test 1
        xliffunit.rich_source = [StringElem([u'foo', X(id='bar'), u'baz'])]
        source_dom_node = xliffunit.getlanguageNode(None, 0)
        x_placeable = source_dom_node[0]

        assert source_dom_node.text == 'foo'

        assert x_placeable.tag == u'x'
        assert x_placeable.attrib['id'] == 'bar'
        assert x_placeable.tail == 'baz'

        xliffunit.rich_source[0].print_tree(2)
        print xliffunit.rich_source
        assert xliffunit.rich_source == [
            StringElem([StringElem(u'foo'),
                        X(id='bar'),
                        StringElem(u'baz')])
        ]

        # Test 2
        xliffunit.rich_source = [
            StringElem([
                u'foo', u'baz',
                G(id='oof', sub=[G(id='zab', sub=[u'bar', u'rab'])])
            ])
        ]
        source_dom_node = xliffunit.getlanguageNode(None, 0)
        g_placeable = source_dom_node[0]
        nested_g_placeable = g_placeable[0]

        assert source_dom_node.text == u'foobaz'

        assert g_placeable.tag == u'g'
        assert g_placeable.text is None
        assert g_placeable.attrib[u'id'] == u'oof'
        assert g_placeable.tail is None

        assert nested_g_placeable.tag == u'g'
        assert nested_g_placeable.text == u'barrab'
        assert nested_g_placeable.attrib[u'id'] == u'zab'
        assert nested_g_placeable.tail is None

        rich_source = xliffunit.rich_source
        rich_source[0].print_tree(2)
        assert rich_source == [
            StringElem(
                [u'foobaz',
                 G(id='oof', sub=[G(id='zab', sub=[u'barrab'])])])
        ]
Пример #22
0
    def pretranslatexliff(self, input_source, template_source=None):
        """helper that converts strings to po source without requiring files"""
        input_file = wStringIO.StringIO(input_source)
        if template_source:
            template_file = wStringIO.StringIO(template_source)
        else:
            template_file = None
        output_file = wStringIO.StringIO()

        pretranslate.pretranslate_file(input_file, output_file, template_file)
        output_file.seek(0)
        return xliff.xlifffile(output_file.read())
Пример #23
0
    def pretranslatexliff(self, input_source, template_source=None):
        """helper that converts strings to po source without requiring files"""
        input_file = wStringIO.StringIO(input_source)
        if template_source:
            template_file = wStringIO.StringIO(template_source)
        else:
            template_file = None
        output_file = wStringIO.StringIO()

        pretranslate.pretranslate_file(input_file, output_file, template_file)
        output_file.seek(0)
        return xliff.xlifffile(output_file.read())
Пример #24
0
 def convertstore(self, theoofile, duplicatestyle="msgctxt"):
     """converts an entire oo file to a base class format (.po or XLIFF)"""
     thetargetfile = xliff.xlifffile()
     thetargetfile.setsourcelanguage(self.sourcelanguage)
     thetargetfile.settargetlanguage(self.targetlanguage)
     # create a header for the file
     bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi' + ('''?subcomponent=ui&comment=&short_desc=Localization issue in file: %(filename)s&component=l10n&form_name=enter_issue''' % {"filename": theoofile.filename}).replace(" ", "%20").replace(":", "%3A")
     # go through the oo and convert each element
     for theoo in theoofile.units:
         unitlist = self.convertelement(theoo)
         for unit in unitlist:
             thetargetfile.addunit(unit)
     return thetargetfile
Пример #25
0
    def test_rich_target(self):
        xlifffile = xliff.xlifffile()
        xliffunit = xlifffile.addsourceunit("")

        # Test 1
        xliffunit.set_rich_target(
            [StringElem(["foo", X(id="bar"), "baz"])], "fr")
        target_dom_node = xliffunit.getlanguageNode(None, 1)
        x_placeable = target_dom_node[0]

        assert target_dom_node.text == "foo"
        assert x_placeable.tag == "x"
        assert x_placeable.attrib["id"] == "bar"
        assert x_placeable.tail == "baz"

        # Test 2
        xliffunit.set_rich_target(
            [
                StringElem([
                    "foo", "baz",
                    G(id="oof", sub=[G(id="zab", sub=["bar", "rab"])])
                ])
            ],
            "fr",
        )
        target_dom_node = xliffunit.getlanguageNode(None, 1)
        g_placeable = target_dom_node[0]
        nested_g_placeable = g_placeable[0]

        assert target_dom_node.text == "foobaz"

        assert g_placeable.tag == "g"
        print("g_placeable.text: {} ({})".format(g_placeable.text,
                                                 type(g_placeable.text)))
        assert g_placeable.text is None
        assert g_placeable.attrib["id"] == "oof"
        assert g_placeable.tail is None

        assert nested_g_placeable.tag == "g"
        assert nested_g_placeable.text == "barrab"
        assert nested_g_placeable.attrib["id"] == "zab"
        assert nested_g_placeable.tail is None

        xliffunit.rich_target[0].print_tree(2)
        assert xliffunit.rich_target == [
            StringElem(
                ["foobaz",
                 G(id="oof", sub=[G(id="zab", sub=["barrab"])])])
        ]
Пример #26
0
    def test_alttrans(self):
        """Test xliff <alt-trans> accessors"""
        xlifffile = xliff.xlifffile()
        unit = xlifffile.addsourceunit("Testing")

        unit.addalttrans("ginmi")
        unit.addalttrans("shikenki")
        alternatives = unit.getalttrans()
        assert alternatives[0].source == "Testing"
        assert alternatives[0].target == "ginmi"
        assert alternatives[1].target == "shikenki"

        assert not unit.target

        unit.addalttrans("Tasting", origin="bob", lang="eng")
        alternatives = unit.getalttrans()
        assert alternatives[2].target == "Tasting"

        alternatives = unit.getalttrans(origin="bob")
        assert alternatives[0].target == "Tasting"

        unit.delalttrans(alternatives[0])
        assert len(unit.getalttrans(origin="bob")) == 0
        alternatives = unit.getalttrans()
        assert len(alternatives) == 2
        assert alternatives[0].target == "ginmi"
        assert alternatives[1].target == "shikenki"

        # clean up:
        alternatives = unit.getalttrans()
        for alt in alternatives:
            unit.delalttrans(alt)
        unit.addalttrans("targetx", sourcetxt="sourcex")
        # test that the source node is before the target node:
        alt = unit.getalttrans()[0]
        altformat = etree.tostring(alt.xmlelement, encoding="unicode")
        print(altformat)
        assert altformat.find("<source") < altformat.find("<target")

        # test that a new target is still before alt-trans (bug 1098)
        unit.target = "newester target"
        unitformat = str(unit)
        print(unitformat)
        assert (
            unitformat.find("<source")
            < unitformat.find("<target")
            < unitformat.find("<alt-trans")
        )
Пример #27
0
def dump_xliff(project, locale, relative_path):
    """Dump .xliff file with relative path from database."""

    locale_directory_path = get_locale_directory(project, locale)["path"]
    path = os.path.join(locale_directory_path, relative_path)
    resource = Resource.objects.filter(project=project, path=relative_path)
    entities = Entity.objects.filter(resource=resource, obsolete=False)

    with open(path, 'r+') as f:
        xf = xliff.xlifffile(f)

        # Update target-language attribute in file nodes
        for node in xf.document.getroot().iterchildren(xf.namespaced("file")):
            node.set("target-language", locale.code)

        for unit in xf.units:
            key = unit.getid()

            try:
                entity = Entity.objects.get(resource=resource, key=key)

            except Entity.DoesNotExist as e:
                log.error('%s: Entity "%s" does not exist' % (path, original))
                continue

            try:
                translation = Translation.objects.filter(
                    entity=entity, locale=locale, approved=True) \
                    .latest('date').string
                unit.settarget(translation)

            except Translation.DoesNotExist as e:
                # Remove "approved" attribute
                try:
                    del unit.xmlelement.attrib['approved']
                except KeyError:
                    pass

                # Remove "target" element
                target = unit.xmlelement.find(unit.namespaced("target"))
                if target:
                    unit.xmlelement.remove(target)

        # Erase file and then write, otherwise content gets appended
        f.seek(0)
        f.truncate()
        f.writelines(xf.__str__())
        log.debug("File updated: " + path)
Пример #28
0
 def convertstore(self, theoofile, duplicatestyle="msgctxt"):
     """converts an entire oo file to a base class format (.po or XLIFF)"""
     thetargetfile = xliff.xlifffile()
     thetargetfile.setsourcelanguage(self.sourcelanguage)
     thetargetfile.settargetlanguage(self.targetlanguage)
     # create a header for the file
     bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi' + (
         '''?subcomponent=ui&comment=&short_desc=Localization issue in file: %(filename)s&component=l10n&form_name=enter_issue'''
         % {
             "filename": theoofile.filename
         }).replace(" ", "%20").replace(":", "%3A")
     # go through the oo and convert each element
     for theoo in theoofile.units:
         unitlist = self.convertelement(theoo)
         for unit in unitlist:
             thetargetfile.addunit(unit)
     return thetargetfile
Пример #29
0
    def test_alttrans(self):
        """Test xliff <alt-trans> accessors"""
        xlifffile = xliff.xlifffile()
        unit = xlifffile.addsourceunit("Testing")

        unit.addalttrans("ginmi")
        unit.addalttrans("shikenki")
        alternatives = unit.getalttrans()
        assert alternatives[0].source == "Testing"
        assert alternatives[0].target == "ginmi"
        assert alternatives[1].target == "shikenki"

        assert not unit.target

        unit.addalttrans("Tasting", origin="bob", lang="eng")
        alternatives = unit.getalttrans()
        assert alternatives[2].target == "Tasting"

        alternatives = unit.getalttrans(origin="bob")
        assert alternatives[0].target == "Tasting"

        unit.delalttrans(alternatives[0])
        assert len(unit.getalttrans(origin="bob")) == 0
        alternatives = unit.getalttrans()
        assert len(alternatives) == 2
        assert alternatives[0].target == "ginmi"
        assert alternatives[1].target == "shikenki"

        #clean up:
        alternatives = unit.getalttrans()
        for alt in alternatives:
            unit.delalttrans(alt)
        unit.addalttrans("targetx", sourcetxt="sourcex")
        # test that the source node is before the target node:
        alt = unit.getalttrans()[0]
        altformat = etree.tostring(alt.xmlelement)
        if isinstance(altformat, bytes):
            altformat=altformat.decode('utf-8')
        print(altformat)
        assert altformat.find("<source") < altformat.find("<target")

        # test that a new target is still before alt-trans (bug 1098)
        unit.target = "newester target"
        unitformat = str(unit)
        print(unitformat)
        assert unitformat.find("<source") < unitformat.find("<target") < unitformat.find("<alt-trans")
Пример #30
0
 def mergexliff(self, templatesource, inputsource, mergeblanks="yes",
                mergefuzzy="yes",
                mergecomments="yes"):
     """merges the sources of the given files and returns a new xlifffile
     object"""
     templatefile = wStringIO.StringIO(templatesource)
     inputfile = wStringIO.StringIO(inputsource)
     outputfile = wStringIO.StringIO()
     assert pomerge.mergestore(inputfile, outputfile, templatefile,
                               mergeblanks=mergeblanks,
                               mergefuzzy=mergefuzzy,
                               mergecomments=mergecomments)
     outputxliffstring = outputfile.getvalue()
     print("Generated XML:")
     print(outputxliffstring)
     outputxlifffile = xliff.xlifffile(outputxliffstring)
     return outputxlifffile
Пример #31
0
 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")
Пример #32
0
 def convertstore(self, theoofile, duplicatestyle="msgctxt"):
     """converts an entire oo file to a base class format (.po or XLIFF)"""
     thetargetfile = xliff.xlifffile()
     thetargetfile.setsourcelanguage(self.sourcelanguage)
     thetargetfile.settargetlanguage(self.targetlanguage)
     # create a header for the file
     bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi?%s' % \
               urlencode({"subcomponent": "ui",
                          "comment": "",
                          "short_desc": "Localization issue in file: %s" % \
                                        theoofile.filename,
                          "component": "l10n",
                          "form_name": "enter_issue",
                         })
     # go through the oo and convert each element
     for theoo in theoofile.units:
         unitlist = self.convertelement(theoo)
         for unit in unitlist:
             thetargetfile.addunit(unit)
     return thetargetfile
Пример #33
0
 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")
Пример #34
0
 def convertstore(self, theoofile, duplicatestyle="msgctxt"):
     """converts an entire oo file to a base class format (.po or XLIFF)"""
     thetargetfile = xliff.xlifffile()
     thetargetfile.setsourcelanguage(self.sourcelanguage)
     thetargetfile.settargetlanguage(self.targetlanguage)
     # create a header for the file
     bug_url = 'http://qa.openoffice.org/issues/enter_bug.cgi?%s' % \
               urlencode({"subcomponent": "ui",
                          "comment": "",
                          "short_desc": "Localization issue in file: %s" % \
                                        theoofile.filename,
                          "component": "l10n",
                          "form_name": "enter_issue",
                         })
     # go through the oo and convert each element
     for theoo in theoofile.units:
         unitlist = self.convertelement(theoo)
         for unit in unitlist:
             thetargetfile.addunit(unit)
     return thetargetfile
Пример #35
0
    def test_rich_source(self):
        xlifffile = xliff.xlifffile()
        xliffunit = xlifffile.addsourceunit(u'')

        # Test 1
        xliffunit.rich_source = [StringElem([u'foo', X(id='bar'), u'baz'])]
        source_dom_node = xliffunit.getlanguageNode(None, 0)
        x_placeable = source_dom_node[0]

        assert source_dom_node.text == 'foo'

        assert x_placeable.tag == u'x'
        assert x_placeable.attrib['id'] == 'bar'
        assert x_placeable.tail == 'baz'

        xliffunit.rich_source[0].print_tree(2)
        print xliffunit.rich_source
        assert xliffunit.rich_source == [StringElem([StringElem(u'foo'), X(id='bar'), StringElem(u'baz')])]

        # Test 2
        xliffunit.rich_source = [StringElem([u'foo', u'baz', G(id='oof', sub=[G(id='zab', sub=[u'bar', u'rab'])])])]
        source_dom_node = xliffunit.getlanguageNode(None, 0)
        g_placeable = source_dom_node[0]
        nested_g_placeable = g_placeable[0]

        assert source_dom_node.text == u'foobaz'

        assert g_placeable.tag == u'g'
        assert g_placeable.text is None
        assert g_placeable.attrib[u'id'] == u'oof'
        assert g_placeable.tail is None

        assert nested_g_placeable.tag == u'g'
        assert nested_g_placeable.text == u'barrab'
        assert nested_g_placeable.attrib[u'id'] == u'zab'
        assert nested_g_placeable.tail is None

        rich_source = xliffunit.rich_source
        rich_source[0].print_tree(2)
        assert rich_source == [StringElem([u'foobaz', G(id='oof', sub=[G(id='zab', sub=[u'barrab'])])])]
Пример #36
0
    def test_multiple_filenodes(self):
        xlfsource = '''<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
  <file original="file0" source-language="en" datatype="plaintext">
    <body>
      <trans-unit id="hello" approved="yes">
        <source>Hello</source>
      </trans-unit>
    </body>
  </file>
  <file original="file1" source-language="en" datatype="plaintext">
    <body>
      <trans-unit id="world" approved="yes">
        <source>World</source>
      </trans-unit>
    </body>
  </file>
</xliff>'''
        xfile = xliff.xlifffile.parsestring(xlfsource)
        assert len(xfile.units) == 2
        assert xfile.units[0].getid() == "file0\x04hello"
        assert xfile.units[1].getid() == "file1\x04world"
        xunit = xliff.xlifffile.UnitClass(source="goodbye")
        xunit.setid("file2\x04goodbye")
        xfile.addunit(xunit)
        assert xfile.units[2].getid() == "file2\x04goodbye"
        # if there is no file set it will use the active context
        xunit = xliff.xlifffile.UnitClass(source="lastfile")
        xunit.setid("lastfile")
        xfile.addunit(xunit)
        assert xfile.units[3].getid() == "file2\x04lastfile"
        newxfile = xliff.xlifffile()
        newxfile.addunit(xfile.units[0])
        newxfile.addunit(xfile.units[1])
        assert newxfile.units[0].getid() == "file0\x04hello"
        assert newxfile.units[1].getid() == "file1\x04world"
        assert newxfile.getfilenode("file0") is not None
        assert newxfile.getfilenode("file1") is not None
        assert not newxfile.getfilenode("foo")
Пример #37
0
    def test_fuzzy(self):
        xlifffile = xliff.xlifffile()
        unit = xlifffile.addsourceunit("Concept")
        unit.markfuzzy()
        assert unit.isfuzzy()
        unit.target = "Konsep"
        assert unit.isfuzzy()
        unit.markfuzzy()
        assert unit.isfuzzy()
        unit.markfuzzy(False)
        assert not unit.isfuzzy()
        unit.markfuzzy(True)
        assert unit.isfuzzy()

        #If there is no target, we can't really indicate fuzzyness, so we set
        #approved to "no". If we want isfuzzy() to reflect that, the line can
        #be uncommented
        unit.target = None
        assert unit.target is None
        print unit
        unit.markfuzzy(True)
        assert 'approved="no"' in str(unit)
Пример #38
0
    def test_multiple_filenodes(self):
        xlfsource = '''<?xml version="1.0" encoding="utf-8"?>
<xliff version="1.1" xmlns="urn:oasis:names:tc:xliff:document:1.1">
  <file original="file0" source-language="en" datatype="plaintext">
    <body>
      <trans-unit id="hello" approved="yes">
        <source>Hello</source>
      </trans-unit>
    </body>
  </file>
  <file original="file1" source-language="en" datatype="plaintext">
    <body>
      <trans-unit id="world" approved="yes">
        <source>World</source>
      </trans-unit>
    </body>
  </file>
</xliff>'''
        xfile = xliff.xlifffile.parsestring(xlfsource)
        assert len(xfile.units) == 2
        assert xfile.units[0].getid() == "file0\x04hello"
        assert xfile.units[1].getid() == "file1\x04world"
        xunit = xliff.xlifffile.UnitClass(source="goodbye")
        xunit.setid("file2\x04goodbye")
        xfile.addunit(xunit)
        assert xfile.units[2].getid() == "file2\x04goodbye"
        # if there is no file set it will use the active context
        xunit = xliff.xlifffile.UnitClass(source="lastfile")
        xunit.setid("lastfile")
        xfile.addunit(xunit)
        assert xfile.units[3].getid() == "file2\x04lastfile"
        newxfile = xliff.xlifffile()
        newxfile.addunit(xfile.units[0])
        newxfile.addunit(xfile.units[1])
        assert newxfile.units[0].getid() == "file0\x04hello"
        assert newxfile.units[1].getid() == "file1\x04world"
        assert newxfile.getfilenode("file0")
        assert newxfile.getfilenode("file1")
        assert not newxfile.getfilenode("foo")
Пример #39
0
    def test_fuzzy(self):
        xlifffile = xliff.xlifffile()
        unit = xlifffile.addsourceunit("Concept")
        unit.markfuzzy()
        assert not unit.isfuzzy()  # untranslated
        unit.target = "Konsep"
        assert unit.isfuzzy()
        unit.markfuzzy()
        assert unit.isfuzzy()
        unit.markfuzzy(False)
        assert not unit.isfuzzy()
        unit.markfuzzy(True)
        assert unit.isfuzzy()

        #If there is no target, we can't really indicate fuzzyness, so we set
        #approved to "no". If we want isfuzzy() to reflect that, the line can
        #be uncommented
        unit.target = None
        assert unit.target is None
        print unit
        unit.markfuzzy(True)
        assert 'approved="no"' in str(unit)
Пример #40
0
def extract_xliff(project, locale, path, entities=False):
    """Extract .xliff file with path and save or update in DB."""

    with open(path) as f:
        xf = xliff.xlifffile(f)

        relative_path = get_relative_path(path, locale)
        resource, created = Resource.objects.get_or_create(project=project,
                                                           path=relative_path,
                                                           format='xliff')

        if entities:
            for order, unit in enumerate(xf.units):
                save_entity(resource=resource,
                            string=unicode(unit.get_rich_source()[0]),
                            key=unit.getid(),
                            comment=unit.getnotes(),
                            order=order)

            update_entity_count(resource)

        else:
            for unit in xf.units:
                translation = unicode(unit.get_rich_target()[0])
                if translation:
                    try:
                        e = Entity.objects.get(resource=resource,
                                               key=unit.getid())
                        save_translation(entity=e,
                                         locale=locale,
                                         string=translation)

                    except Entity.DoesNotExist:
                        continue

            update_stats(resource, locale)

        log.debug("[" + locale.code + "]: " + path + " saved to DB.")
Пример #41
0
def extract_xliff(project, locale, path, entities=False):
    """Extract .xliff file with path and save or update in DB."""

    with open(path) as f:
        xf = xliff.xlifffile(f)

        relative_path = get_relative_path(path, locale)
        resource, created = Resource.objects.get_or_create(
            project=project, path=relative_path, format='xliff')

        if entities:
            for order, unit in enumerate(xf.units):
                save_entity(
                    resource=resource,
                    string=unicode(unit.get_rich_source()[0]),
                    key=unit.getid(),
                    comment=unit.getnotes(),
                    order=order)

            update_entity_count(resource)

        else:
            for unit in xf.units:
                translation = unicode(unit.get_rich_target()[0])
                if translation:
                    try:
                        e = Entity.objects.get(
                            resource=resource, key=unit.getid())
                        save_translation(
                            entity=e, locale=locale, string=translation)

                    except Entity.DoesNotExist:
                        continue

            update_stats(resource, locale)

        log.debug("[" + locale.code + "]: " + path + " saved to DB.")
Пример #42
0
 def test_sourcelanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="xh")
     xmltext = xlifffile.serialize()
     print(xmltext)
     assert xmltext.find('source-language="xh"') > 0
Пример #43
0
 def test_targetlanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="zu", targetlanguage="af")
     xmltext = xlifffile.serialize()
     print(xmltext)
     assert xmltext.find('source-language="zu"') > 0
     assert xmltext.find('target-language="af"') > 0
Пример #44
0
 def test_targetlanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="zu", targetlanguage="af")
     xmltext = str(xlifffile)
     print xmltext
     assert xmltext.find('source-language="zu"') > 0
     assert xmltext.find('target-language="af"') > 0
Пример #45
0
 def test_sourcelanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="xh")
     xmltext = str(xlifffile)
     print xmltext
     assert xmltext.find('source-language="xh"') > 0
Пример #46
0
 def test_sourcelanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="xh")
     xmltext = str(xlifffile)
     print xmltext
     assert xmltext.find('source-language="xh"')> 0
Пример #47
0
 def setup_method(self, method):
     self.postore = po.pofile(PO_DOC)
     self.xliffstore = xliff.xlifffile(XLIFF_DOC)
Пример #48
0
def parse(path, source_path=None, locale=None):
    with open(path) as f:
        xliff_file = xliff.xlifffile(f)
    return XLIFFResource(path, xliff_file)
Пример #49
0
def parse(path, source_path=None):
    with open(path) as f:
        xliff_file = xliff.xlifffile(f)
    return XLIFFResource(path, xliff_file)
Пример #50
0
 def setup_method(self, method):
     self.postore = po.pofile(PO_DOC.encode('utf-8'))
     self.xliffstore = xliff.xlifffile(XLIFF_DOC.encode('utf-8'))
Пример #51
0
 def get_storage(self):
     return xlifffile()
Пример #52
0
 def xliff_parse(self, xliff_text):
     """helper that parses po source without requiring files"""
     dummyfile = wStringIO.StringIO(xliff_text)
     xliff_file = xliff.xlifffile(dummyfile)
     return xliff_file
Пример #53
0
 def test_sourcelanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="xh")
     xmltext = bytes(xlifffile).decode('utf-8')
     print(xmltext)
     assert xmltext.find('source-language="xh"') > 0
Пример #54
0
 def test_targetlanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="zu", targetlanguage="af")
     xmltext = bytes(xlifffile).decode('utf-8')
     print(xmltext)
     assert xmltext.find('source-language="zu"') > 0
     assert xmltext.find('target-language="af"') > 0
Пример #55
0
 def test_targetlanguage(self):
     xlifffile = xliff.xlifffile(sourcelanguage="zu", targetlanguage="af")
     xmltext = str(xlifffile)
     print xmltext
     assert xmltext.find('source-language="zu"')> 0
     assert xmltext.find('target-language="af"')> 0