Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] translate: inline elements with translatable attributes only #29432

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions odoo/addons/base/tests/test_translate.py
Expand Up @@ -114,6 +114,20 @@ def test_translate_xml_inline3(self):
self.assertItemsEqual(terms,
['Form stuff', 'Blah blah blah'])

def test_translate_xml_inline4(self):
""" Test xml_translate() with inline elements with translated attrs only. """
terms = []
source = """<form string="Form stuff">
<div>
<label for="stuff"/>
<span class="fa fa-globe" title="Title stuff"/>
</div>
</form>"""
result = xml_translate(terms.append, source)
self.assertEquals(result, source)
self.assertItemsEqual(terms,
['Form stuff', '<span class="fa fa-globe" title="Title stuff"/>'])

def test_translate_xml_t(self):
""" Test xml_translate() with t-* attributes. """
terms = []
Expand Down
15 changes: 5 additions & 10 deletions odoo/tools/translate.py
Expand Up @@ -159,7 +159,7 @@ def translate_xml_node(node, callback, parse, serialize):
"""

def nonspace(text):
return bool(text) and not text.isspace()
return bool(text) and len(re.sub(r'\W+', '', text)) > 1

def concat(text1, text2):
return text2 if text1 is None else text1 + (text2 or "")
Expand Down Expand Up @@ -251,7 +251,10 @@ def process(node):
# complete result and return it
append_content(result, todo)
result.tail = node.tail
has_text = todo_has_text or nonspace(result.text) or nonspace(result.tail)
has_text = (
todo_has_text or nonspace(result.text) or nonspace(result.tail)
or any(name in TRANSLATED_ATTRS for name in result.attrib)
)
return (has_text, result)

# translate the content of todo and append it to result
Expand Down Expand Up @@ -807,14 +810,6 @@ def push_translation(module, type, name, id, source, comments=None):
# empty and one-letter terms are ignored, they probably are not meant to be
# translated, and would be very hard to translate anyway.
sanitized_term = (source or '').strip()
try:
# verify the minimal size without eventual xml tags
# wrap to make sure html content like '<a>b</a><c>d</c>' is accepted by lxml
wrapped = u"<div>%s</div>" % sanitized_term
node = etree.fromstring(wrapped)
sanitized_term = etree.tostring(node, encoding='unicode', method='text')
except etree.ParseError:
pass
# remove non-alphanumeric chars
sanitized_term = re.sub(r'\W+', '', sanitized_term)
if not sanitized_term or len(sanitized_term) <= 1:
Expand Down