def test_translate_xml_with_namespace(self): """ Test xml_translate() on elements with namespaces. """ terms = [] # do not slit the long line below, otherwise the result will not match source = """<Invoice xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"> <cbc:UBLVersionID t-esc="version_id"/> <t t-foreach="[1, 2, 3, 4]" t-as="value"> Oasis <cac:Test t-esc="value"/> </t> </Invoice>""" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, ['Oasis']) result = xml_translate(lambda term: term, source) self.assertEquals(result, source)
def test_translate_xml_text_entity(self): """ Test xml_translate() on plain text with HTML escaped entities. """ terms = [] source = "Blah&nbsp;blah&nbsp;blah" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, [source])
def test_translate_xml_unicode(self): """ Test xml_translate() on plain text with unicode characters. """ terms = [] source = u"Un heureux évènement" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, [source])
def test_translate_xml_text(self): """ Test xml_translate() on plain text. """ terms = [] source = "Blah blah blah" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, [source])
def test_translate_html_i(self): """ Test xml_translate() and html_translate() with <i> elements. """ source = """<p>A <i class="fa-check"></i> B</p>""" result = xml_translate(lambda term: term, source) self.assertEquals(result, """<p>A <i class="fa-check"/> B</p>""") result = html_translate(lambda term: term, source) self.assertEquals(result, source)
def test_translate_xml_attribute(self): """ Test xml_translate() with <attribute> elements. """ terms = [] source = """<field name="foo" position="attributes"> <attribute name="string">Translate this</attribute> <attribute name="option">Do not translate this</attribute> </field>""" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, ['Translate this'])
def test_translate_xml_t(self): """ Test xml_translate() with t-* attributes. """ terms = [] source = """<t t-name="stuff"> stuff before <span t-field="o.name"/> stuff after </t>""" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, ['stuff before', 'stuff after'])
def test_translate_xml_off(self): """ Test xml_translate() with attribute translate="off". """ terms = [] source = """<div> stuff before <div t-translation="off">Do not translate this</div> stuff after </div>""" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, ['stuff before', 'stuff after'])
def test_translate_xml_base(self): """ Test xml_translate() without formatting elements. """ terms = [] source = """<form string="Form stuff"> <h1>Blah blah blah</h1> Put some more text here <field name="foo"/> </form>""" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual( terms, ['Form stuff', 'Blah blah blah', 'Put some more text here'])
def test_translate_xml_inline2(self): """ Test xml_translate() with formatting elements embedding other elements. """ terms = [] source = """<form string="Form stuff"> <b><h1>Blah <i>blah</i> blah</h1></b> Put <em>some <b>more text</b></em> here <field name="foo"/> </form>""" result = xml_translate(terms.append, source) self.assertEqual(result, source) self.assertItemsEqual(terms, ['Form stuff', 'Blah <i>blah</i> blah', 'Put <em>some <b>more text</b></em> here'])
def test_translate_xml_inline5(self): """ Test xml_translate() with inline elements with empty translated attrs only. """ terms = [] source = """<form string="Form stuff"> <div> <label for="stuff"/> <span class="fa fa-globe" title=""/> </div> </form>""" result = xml_translate(terms.append, source) self.assertEqual(result, source) self.assertItemsEqual(terms, ['Form stuff'])
def test_translate_xml_inline3(self): """ Test xml_translate() with formatting elements without actual text. """ terms = [] source = """<form string="Form stuff"> <div> <span class="before"/> <h1>Blah blah blah</h1> <span class="after"> <i class="hack"/> </span> </div> </form>""" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, ['Form stuff', 'Blah blah blah'])
def test_translate_xml_a(self): """ Test xml_translate() with <a> elements. """ terms = [] source = """<t t-name="stuff"> <ul class="nav navbar-nav"> <li class="nav-item"> <a class="nav-link oe_menu_leaf" href="/web#menu_id=42&action=54"> <span class="oe_menu_text">Blah</span> </a> </li> </ul> </t>""" result = xml_translate(terms.append, source) self.assertEqual(result, source) self.assertItemsEqual(terms, ['<span class="oe_menu_text">Blah</span>'])
def test_translate_xml_invalid_translations(self): """ Test xml_translate() with invalid translations. """ source = """<form string="Form stuff"> <h1>Blah <i>blah</i> blah</h1> Put some <b>more text</b> here <field name="foo"/> </form>""" translations = { "Put some <b>more text</b> here": "Mettre <b>plus de texte</i> ici", } expect = """<form string="Form stuff"> <h1>Blah <i>blah</i> blah</h1> Mettre <b>plus de texte</i> ici <field name="foo"/> </form>""" result = xml_translate(translations.get, source) self.assertEqual(result, expect)
def test_translate_xml_a(self): """ Test xml_translate() with <a> elements. """ terms = [] source = """<t t-name="stuff"> <ul class="nav navbar-nav"> <li> <a class="oe_menu_leaf" href="/web#menu_id=42&action=54"> <span class="oe_menu_text">Blah</span> </a> </li> <li class="dropdown" id="menu_more_container" style="display: none;"> <a class="dropdown-toggle" data-toggle="dropdown" href="#">More <b class="caret"/></a> <ul class="dropdown-menu" id="menu_more"/> </li> </ul> </t>""" result = xml_translate(terms.append, source) self.assertEquals(result, source) self.assertItemsEqual(terms, [ '<span class="oe_menu_text">Blah</span>', 'More <b class="caret"/>' ])