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'])])])]
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'])])]) ]
def test_chunk_list(): left = StringElem([ 'a', G(id='foo[2]/bar[2]/baz[2]', sub=['b', X(id='foo[1]/bar[1]/baz[1]'), 'c']), 'é' ]) right = StringElem([ 'a', G(id='foo[2]/bar[2]/baz[2]', sub=['b', X(id='foo[1]/bar[1]/baz[1]'), 'c']), 'é' ]) assert left == right
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"])])]) ]
def test_chunk_list(): left = StringElem([ "a", G(id="foo[2]/bar[2]/baz[2]", sub=["b", X(id="foo[1]/bar[1]/baz[1]"), "c"]), "é", ]) right = StringElem([ "a", G(id="foo[2]/bar[2]/baz[2]", sub=["b", X(id="foo[1]/bar[1]/baz[1]"), "c"]), "é", ]) assert left == right
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"])])]) ]
def test_xml_to_strelem(): source = etree.fromstring('<source>a</source>') elem = lisa.xml_to_strelem(source) assert elem == StringElem('a') source = etree.fromstring( '<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [StringElem('a'), X(id='foo[1]/bar[1]/baz[1]')] source = etree.fromstring( '<source>a<x id="foo[1]/bar[1]/baz[1]"/>é</source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem('a'), X(id='foo[1]/bar[1]/baz[1]'), StringElem('é') ] source = etree.fromstring( '<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>é</source>' ) elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem('a'), G(id='foo[2]/bar[2]/baz[2]', sub=[StringElem('b'), X(id='foo[1]/bar[1]/baz[1]'), StringElem('c')]), StringElem('é') ]
def test_xml_to_strelem(): source = etree.fromstring("<source>a</source>") elem = lisa.xml_to_strelem(source) assert elem == StringElem("a") source = etree.fromstring( '<source>a<x id="foo[1]/bar[1]/baz[1]"/></source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [StringElem("a"), X(id="foo[1]/bar[1]/baz[1]")] source = etree.fromstring( '<source>a<x id="foo[1]/bar[1]/baz[1]"/>é</source>') elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem("a"), X(id="foo[1]/bar[1]/baz[1]"), StringElem("é") ] source = etree.fromstring( '<source>a<g id="foo[2]/bar[2]/baz[2]">b<x id="foo[1]/bar[1]/baz[1]"/>c</g>é</source>' ) elem = lisa.xml_to_strelem(source) assert elem.sub == [ StringElem("a"), G( id="foo[2]/bar[2]/baz[2]", sub=[ StringElem("b"), X(id="foo[1]/bar[1]/baz[1]"), StringElem("c") ], ), StringElem("é"), ]
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( """<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("Text "), G("g", id="_1_ski_040"), StringElem("TEXT"), UnknownXML( [ StringElem("bpt"), UnknownXML("sub", xml_node=copy(source[1][0])), StringElem("\n "), ], id="_1_ski_139", xml_node=copy(source[3]), ), StringElem("TEXT"), UnknownXML("ept", id="_1_ski_238", xml_node=copy(source[2])), StringElem("TEXT"), UnknownXML(id="_1_ski_337", xml_node=copy(source[3])), # ph-tag StringElem("TEXT"), UnknownXML("it", id="_1_ski_436", xml_node=copy(source[4])), StringElem("TEXT"), UnknownXML("mrk", xml_node=copy(source[5])), StringElem("\n "), X(id="_1_ski_535"), StringElem("TEXT"), Bx(id="_1_ski_634"), StringElem("TEXT"), Ex(id="_1_ski_733"), StringElem("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)
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>' )
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>'
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>'
def test_rich_target(): 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.target = "test plain target" xliffunit.set_rich_target( [ StringElem([ "foo", G(id="eek", sub=[G(id="ook", sub=["bar", "rab"])]) ]) ], "fr", ) 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( f"g_placeable.text: {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"])])]) ] print(bytes(xlifffile).decode()) assert (bytes(xlifffile).decode() == """<?xml version="1.0" encoding="UTF-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.1" version="1.1"> <file original="NoName" source-language="en" datatype="plaintext"> <body> <trans-unit xml:space="preserve" id="2" approved="yes"><source></source> <target state="translated">foobaz<g id="oof"><g id="zab">barrab</g></g></target> </trans-unit> </body> </file> </xliff> """)