[IMP] translate: add cases where the content of XML elements is not translated
- do not translate the content of elements with translate="off" - translate `<attribute name="X">` only for some values of X
This commit is contained in:
@@ -96,3 +96,28 @@ class TranslationToolsTestCase(unittest.TestCase):
|
|||||||
self.assertEquals(result, source)
|
self.assertEquals(result, source)
|
||||||
self.assertItemsEqual(terms,
|
self.assertItemsEqual(terms,
|
||||||
['stuff before', 'stuff after'])
|
['stuff before', 'stuff after'])
|
||||||
|
|
||||||
|
def test_translate_xml_off(self):
|
||||||
|
""" Test xml_translate() with attribute translate="off". """
|
||||||
|
terms = []
|
||||||
|
source = """<div>
|
||||||
|
stuff before
|
||||||
|
<div 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_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'])
|
||||||
|
|||||||
@@ -219,8 +219,13 @@ class XMLTranslator(object):
|
|||||||
|
|
||||||
def process(self, node):
|
def process(self, node):
|
||||||
""" Process the given xml `node`: collect `todo` and `done` items. """
|
""" Process the given xml `node`: collect `todo` and `done` items. """
|
||||||
if isinstance(node, SKIPPED_ELEMENT_TYPES) or node.tag in SKIPPED_ELEMENTS:
|
if (
|
||||||
# serialize the node as done, but keep its tail as todo
|
isinstance(node, SKIPPED_ELEMENT_TYPES) or
|
||||||
|
node.tag in SKIPPED_ELEMENTS or
|
||||||
|
node.get("translation", "").strip() == "off" or
|
||||||
|
node.tag == "attribute" and node.get("name") not in TRANSLATED_ATTRS
|
||||||
|
):
|
||||||
|
# do not translate the contents of the node
|
||||||
tail, node.tail = node.tail, None
|
tail, node.tail = node.tail, None
|
||||||
self.done(etree.tostring(node))
|
self.done(etree.tostring(node))
|
||||||
self.todo(escape(tail or ""))
|
self.todo(escape(tail or ""))
|
||||||
|
|||||||
Reference in New Issue
Block a user