[IMP] translate: do not translate XML elements without actual text

This commit is contained in:
Raphael Collet
2015-07-06 16:58:04 +02:00
parent 2eb257ff94
commit 33c8aab971
2 changed files with 26 additions and 3 deletions
@@ -67,6 +67,23 @@ class TranslationToolsTestCase(unittest.TestCase):
self.assertItemsEqual(terms,
['Form stuff', 'Blah <i>blah</i> blah', 'Put <em>some <b>more text</b></em> here'])
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_t(self):
""" Test xml_translate() with t-* attributes. """
terms = []
+9 -3
View File
@@ -181,9 +181,12 @@ class XMLTranslator(object):
self.callback = callback # callback function to translate terms
self._done = [] # translated strings
self._todo = [] # todo strings that come after _done
self.needs_trans = False # whether todo needs translation
def todo(self, text):
def todo(self, text, needs_trans=True):
self._todo.append(text)
if needs_trans and text.strip():
self.needs_trans = True
def all_todo(self):
return not self._done
@@ -194,8 +197,10 @@ class XMLTranslator(object):
def flush(self):
if self._todo:
todo = "".join(self._todo)
self._done.append(self.process_text(todo))
done = self.process_text(todo) if self.needs_trans else todo
self._done.append(done)
del self._todo[:]
self.needs_trans = False
def done(self, text):
self.flush()
@@ -231,7 +236,8 @@ class XMLTranslator(object):
node.tag in TRANSLATED_ELEMENTS and
not any(attr.startswith("t-") for attr in node.attrib)):
# serialize the node element as todo
self.todo(serialize(node.tag, node.attrib, child_trans.get_todo()))
self.todo(serialize(node.tag, node.attrib, child_trans.get_todo()),
child_trans.needs_trans)
else:
# complete translations and serialize result as done
for attr in TRANSLATED_ATTRS: