[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:
Raphael Collet
2015-07-06 16:58:04 +02:00
parent 33c8aab971
commit 4e572e351e
2 changed files with 32 additions and 2 deletions
@@ -96,3 +96,28 @@ class TranslationToolsTestCase(unittest.TestCase):
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 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'])
+7 -2
View File
@@ -219,8 +219,13 @@ class XMLTranslator(object):
def process(self, node):
""" Process the given xml `node`: collect `todo` and `done` items. """
if isinstance(node, SKIPPED_ELEMENT_TYPES) or node.tag in SKIPPED_ELEMENTS:
# serialize the node as done, but keep its tail as todo
if (
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
self.done(etree.tostring(node))
self.todo(escape(tail or ""))