Esempio n. 1
0
    def set_rich_source(self, value, sourcelang='en'):
        sourcelanguageNode = self.get_source_dom()
        if sourcelanguageNode is None:
            sourcelanguageNode = self.createlanguageNode(sourcelang, u'', "source")
            self.set_source_dom(sourcelanguageNode)

        # Clear sourcelanguageNode first
        for i in range(len(sourcelanguageNode)):
            del sourcelanguageNode[0]
        sourcelanguageNode.text = None

        strelem_to_xml(sourcelanguageNode, value[0])
Esempio n. 2
0
    def set_rich_target(self, value, lang='xx', append=False):
        if value is None:
            self.set_target_dom(self.createlanguageNode(lang, u'', "target"))
            return

        languageNode = self.get_target_dom()
        if languageNode is None:
            languageNode = self.createlanguageNode(lang, u'', "target")
            self.set_target_dom(languageNode, append)

        # Clear languageNode first
        for i in range(len(languageNode)):
            del languageNode[0]
        languageNode.text = None

        strelem_to_xml(languageNode, value[0])
Esempio n. 3
0
def rich_to_xliff_string(string_elements):
    """StringElement to XLIFF string

    Transform rich content (StringElement) into
    a string with placeholder kept as XML
    """

    result = ''
    for string_element in string_elements:
        xml = etree.Element(u'e')
        strelem_to_xml(xml, string_element)
        string_xml = etree.tostring(xml, encoding="unicode")
        string_without_wrapping_element = string_xml[3:][:-4]
        result += string_without_wrapping_element

    return result
Esempio n. 4
0
def test_unknown_xml_placeable():
    # The XML below is (modified) from the official XLIFF example file Sample_AlmostEverything_1.2_strict.xlf
    source = etree.fromstring(
        u"""<source xml:lang="en-us">Text <g id="_1_ski_040">g</g>TEXT<bpt id="_1_ski_139">bpt<sub>sub</sub>
               </bpt>TEXT<ept id="_1_ski_238">ept</ept>TEXT<ph id="_1_ski_337"/>TEXT<it id="_1_ski_436" pos="open">it</it>TEXT<mrk mtype="x-test">mrk</mrk>
               <x id="_1_ski_535"/>TEXT<bx id="_1_ski_634"/>TEXT<ex id="_1_ski_733"/>TEXT.</source>"""
    )
    elem = lisa.xml_to_strelem(source)

    from copy import copy

    custom = StringElem(
        [
            StringElem(u"Text "),
            G(u"g", id="_1_ski_040"),
            StringElem(u"TEXT"),
            UnknownXML(
                [StringElem(u"bpt"), UnknownXML(u"sub", xml_node=copy(source[1][0])), StringElem(u"\n               ")],
                id="_1_ski_139",
                xml_node=copy(source[3]),
            ),
            StringElem(u"TEXT"),
            UnknownXML(u"ept", id=u"_1_ski_238", xml_node=copy(source[2])),
            StringElem(u"TEXT"),
            UnknownXML(id="_1_ski_337", xml_node=copy(source[3])),  # ph-tag
            StringElem(u"TEXT"),
            UnknownXML(u"it", id="_1_ski_436", xml_node=copy(source[4])),
            StringElem(u"TEXT"),
            UnknownXML(u"mrk", xml_node=copy(source[5])),
            StringElem(u"\n               "),
            X(id="_1_ski_535"),
            StringElem(u"TEXT"),
            Bx(id="_1_ski_634"),
            StringElem(u"TEXT"),
            Ex(id="_1_ski_733"),
            StringElem(u"TEXT."),
        ]
    )
    assert elem == custom

    xml = copy(source)
    for i in range(len(xml)):
        del xml[0]
    xml.text = None
    xml.tail = None
    lisa.strelem_to_xml(xml, elem)
    assert etree.tostring(xml) == etree.tostring(source)
Esempio n. 5
0
def rich_to_xliff_string(string_elements):
    """Convert StringElement to XLIFF string.

    Transform rich content (StringElement) into a string with placeholder kept as XML
    """
    # Create dummy root element
    xml = etree.Element("e")
    for string_element in string_elements:
        # Inject placeable from translate-toolkit
        strelem_to_xml(xml, string_element)

    # Remove any possible namespace
    for child in xml:
        if child.tag.startswith("{"):
            child.tag = child.tag[child.tag.index("}") + 1:]
    etree.cleanup_namespaces(xml)

    # Convert to string
    string_xml = etree.tostring(xml, encoding="unicode")

    # Strip dummy root element
    return string_xml[3:][:-4]
Esempio n. 6
0
def rich_to_xliff_string(string_elements):
    """StringElement to XLIFF string

    Transform rich content (StringElement) into
    a string with placeholder kept as XML
    """
    # Create dummy root element
    xml = etree.Element(u'e')
    for string_element in string_elements:
        # Inject placeable from translate-toolkit
        strelem_to_xml(xml, string_element)

    # Remove any possible namespace
    for child in xml:
        if child.tag.startswith('{'):
            child.tag = child.tag[child.tag.index('}') + 1:]
    etree.cleanup_namespaces(xml)

    # Convert to string
    string_xml = etree.tostring(xml, encoding="unicode")

    # Strip dummy root element
    return string_xml[3:][:-4]
Esempio n. 7
0
def test_set_strelem_to_xml():
    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem(u'a'))
    assert etree.tostring(source, encoding='UTF-8') == '<source>a</source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', u'é']))
    assert etree.tostring(source, encoding='UTF-8') == '<source>aé</source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem(X(id='foo[1]/bar[1]/baz[1]')))
    assert etree.tostring(source, encoding='UTF-8') == '<source><x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', X(id='foo[1]/bar[1]/baz[1]')]))
    assert etree.tostring(source, encoding='UTF-8') == '<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', X(id='foo[1]/bar[1]/baz[1]'), u'é']))
    assert etree.tostring(source, encoding='UTF-8') == '<source>a<x id="foo[1]/bar[1]/baz[1]"/>é</source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', G(id='foo[2]/bar[2]/baz[2]', sub=[u'b', X(id='foo[1]/bar[1]/baz[1]'), u'c']), u'é']))
    assert etree.tostring(source, encoding='UTF-8') == '<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>é</source>'
Esempio n. 8
0
def test_set_strelem_to_xml():
    source = etree.Element(u"source")
    lisa.strelem_to_xml(source, StringElem(u"a"))
    assert etree.tostring(source, encoding="UTF-8") == "<source>a</source>"

    source = etree.Element(u"source")
    lisa.strelem_to_xml(source, StringElem([u"a", u"é"]))
    assert etree.tostring(source, encoding="UTF-8") == "<source>aé</source>"

    source = etree.Element(u"source")
    lisa.strelem_to_xml(source, StringElem(X(id="foo[1]/bar[1]/baz[1]")))
    assert etree.tostring(source, encoding="UTF-8") == '<source><x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element(u"source")
    lisa.strelem_to_xml(source, StringElem([u"a", X(id="foo[1]/bar[1]/baz[1]")]))
    assert etree.tostring(source, encoding="UTF-8") == '<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element(u"source")
    lisa.strelem_to_xml(source, StringElem([u"a", X(id="foo[1]/bar[1]/baz[1]"), u"é"]))
    assert etree.tostring(source, encoding="UTF-8") == '<source>a<x id="foo[1]/bar[1]/baz[1]"/>é</source>'

    source = etree.Element(u"source")
    lisa.strelem_to_xml(
        source, StringElem([u"a", G(id="foo[2]/bar[2]/baz[2]", sub=[u"b", X(id="foo[1]/bar[1]/baz[1]"), u"c"]), u"é"])
    )
    assert (
        etree.tostring(source, encoding="UTF-8")
        == '<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>é</source>'
    )
Esempio n. 9
0
def test_set_strelem_to_xml():
    source = etree.Element('source')
    lisa.strelem_to_xml(source, StringElem('a'))
    assert etree.tostring(source, encoding='UTF-8') == b'<source>a</source>'

    source = etree.Element('source')
    lisa.strelem_to_xml(source, StringElem(['a', 'é']))
    assert etree.tostring(source,
                          encoding='UTF-8') == b'<source>a\xc3\xa9</source>'

    source = etree.Element('source')
    lisa.strelem_to_xml(source, StringElem(X(id='foo[1]/bar[1]/baz[1]')))
    assert etree.tostring(
        source,
        encoding='UTF-8') == b'<source><x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element('source')
    lisa.strelem_to_xml(source, StringElem(['a',
                                            X(id='foo[1]/bar[1]/baz[1]')]))
    assert etree.tostring(
        source, encoding='UTF-8'
    ) == b'<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element('source')
    lisa.strelem_to_xml(source,
                        StringElem(['a',
                                    X(id='foo[1]/bar[1]/baz[1]'), 'é']))
    assert etree.tostring(
        source, encoding='UTF-8'
    ) == b'<source>a<x id="foo[1]/bar[1]/baz[1]"/>\xc3\xa9</source>'

    source = etree.Element('source')
    lisa.strelem_to_xml(
        source,
        StringElem([
            'a',
            G(id='foo[2]/bar[2]/baz[2]',
              sub=['b', X(id='foo[1]/bar[1]/baz[1]'), 'c']), 'é'
        ]))
    assert etree.tostring(
        source, encoding='UTF-8'
    ) == b'<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>\xc3\xa9</source>'
Esempio n. 10
0
def test_set_strelem_to_xml():
    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem(u'a'))
    assert etree.tostring(source, encoding = 'UTF-8') == '<source>a</source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', u'é']))
    assert etree.tostring(source, encoding = 'UTF-8') == '<source>aé</source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem(X(id='foo[1]/bar[1]/baz[1]')))
    assert etree.tostring(source, encoding = 'UTF-8') == '<source><x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', X(id='foo[1]/bar[1]/baz[1]')]))
    assert etree.tostring(source, encoding = 'UTF-8') == '<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', X(id='foo[1]/bar[1]/baz[1]'), u'é']))
    assert etree.tostring(source, encoding = 'UTF-8') == '<source>a<x id="foo[1]/bar[1]/baz[1]"/>é</source>'

    source = etree.Element(u'source')
    lisa.strelem_to_xml(source, StringElem([u'a', G(id='foo[2]/bar[2]/baz[2]', sub=[u'b', X(id='foo[1]/bar[1]/baz[1]'), u'c']), u'é']))
    assert etree.tostring(source, encoding = 'UTF-8') == '<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>é</source>'
Esempio n. 11
0
def test_set_strelem_to_xml():
    source = etree.Element("source")
    lisa.strelem_to_xml(source, StringElem("a"))
    assert etree.tostring(source, encoding="UTF-8") == b"<source>a</source>"

    source = etree.Element("source")
    lisa.strelem_to_xml(source, StringElem(["a", "é"]))
    assert etree.tostring(source,
                          encoding="UTF-8") == b"<source>a\xc3\xa9</source>"

    source = etree.Element("source")
    lisa.strelem_to_xml(source, StringElem(X(id="foo[1]/bar[1]/baz[1]")))
    assert (etree.tostring(
        source,
        encoding="UTF-8") == b'<source><x id="foo[1]/bar[1]/baz[1]"/></source>'
            )

    source = etree.Element("source")
    lisa.strelem_to_xml(source, StringElem(["a",
                                            X(id="foo[1]/bar[1]/baz[1]")]))
    assert (etree.tostring(source, encoding="UTF-8") ==
            b'<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>')

    source = etree.Element("source")
    lisa.strelem_to_xml(source,
                        StringElem(["a",
                                    X(id="foo[1]/bar[1]/baz[1]"), "é"]))
    assert (etree.tostring(source, encoding="UTF-8") ==
            b'<source>a<x id="foo[1]/bar[1]/baz[1]"/>\xc3\xa9</source>')

    source = etree.Element("source")
    lisa.strelem_to_xml(
        source,
        StringElem([
            "a",
            G(
                id="foo[2]/bar[2]/baz[2]",
                sub=["b", X(id="foo[1]/bar[1]/baz[1]"), "c"],
            ),
            "é",
        ]),
    )
    assert (
        etree.tostring(source, encoding="UTF-8") ==
        b'<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>\xc3\xa9</source>'
    )