From 69b51ce05e855b2b95e75919ac97996a1ff5e136 Mon Sep 17 00:00:00 2001 From: "Lucas Perais (lpe)" Date: Mon, 24 Feb 2020 16:28:08 +0000 Subject: [PATCH] [FIX] translate: do not export OWL directive's attribute Have a static template of the form: ```xml
``` To be used by OWL (github.com/odoo/owl) In this context, the title in the attributes of ComponentB should not be considered as *the* title attribute of HTML but as a sort of variable declaration Before this commit, the string contained in the value of this title attribute was exported. It was not translated at runtime, but we want to export as less stuff as possible in PO files So, after this commit, none of the attributes held by a OWL directive are exported This commit relies on OWL Syntax, which makes mandatory for a component directive's first letter to be capitalized https://github.com/odoo/owl/blob/master/doc/reference/component.md#composition Also, it relies on the good practice and widespread convention to have every HTML node lower cased https://www.w3schools.com/html/html5_syntax.asp closes odoo/odoo#46191 Signed-off-by: Martin Trigaux (mat) --- .../test_translation_import/__manifest__.py | 3 +++ .../static/src/xml/js_templates.xml | 13 +++++++++++++ .../tests/test_term_count.py | 16 +++++++++++++++- odoo/tools/translate.py | 16 +++++++++++++--- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 odoo/addons/test_translation_import/static/src/xml/js_templates.xml diff --git a/odoo/addons/test_translation_import/__manifest__.py b/odoo/addons/test_translation_import/__manifest__.py index bc20d0da61d..5f7ff9a4427 100644 --- a/odoo/addons/test_translation_import/__manifest__.py +++ b/odoo/addons/test_translation_import/__manifest__.py @@ -11,4 +11,7 @@ ], 'installable': True, 'auto_install': False, + 'qweb': [ + 'static/src/xml/js_templates.xml', + ] } diff --git a/odoo/addons/test_translation_import/static/src/xml/js_templates.xml b/odoo/addons/test_translation_import/static/src/xml/js_templates.xml new file mode 100644 index 00000000000..6744603c510 --- /dev/null +++ b/odoo/addons/test_translation_import/static/src/xml/js_templates.xml @@ -0,0 +1,13 @@ + + diff --git a/odoo/addons/test_translation_import/tests/test_term_count.py b/odoo/addons/test_translation_import/tests/test_term_count.py index 4e6c9de0a52..9815e95a260 100644 --- a/odoo/addons/test_translation_import/tests/test_term_count.py +++ b/odoo/addons/test_translation_import/tests/test_term_count.py @@ -7,7 +7,7 @@ import io import odoo from odoo.tests import common, tagged from odoo.tools.misc import file_open, mute_logger -from odoo.tools.translate import _, _lt, TranslationFileReader +from odoo.tools.translate import _, _lt, TranslationFileReader, TranslationModuleReader TRANSLATED_TERM = _lt("Klingon") @@ -319,3 +319,17 @@ class TestTranslationFlow(common.TransactionCase): ]) self.assertEqual(init_translation_count, len(import_translation)) + + def test_export_static_templates(self): + trans_static = [] + po_reader = TranslationModuleReader(self.env.cr, ['test_translation_import']) + for line in po_reader: + module, ttype, name, res_id, source, value, comments = line + if name == "addons/test_translation_import/static/src/xml/js_templates.xml": + trans_static.append(source) + + self.assertNotIn('no export', trans_static) + self.assertIn('do export', trans_static) + self.assertIn('text node', trans_static) + self.assertIn('slot', trans_static) + self.assertIn('slot 2', trans_static) diff --git a/odoo/tools/translate.py b/odoo/tools/translate.py index 038b1431940..ed3b3e66bd1 100644 --- a/odoo/tools/translate.py +++ b/odoo/tools/translate.py @@ -842,10 +842,20 @@ def _extract_translatable_qweb_terms(element, callback): and "t-js" not in el.attrib and not ("t-jquery" in el.attrib and "t-operation" not in el.attrib) and el.get("t-translation", '').strip() != "off"): + _push(callback, el.text, el.sourceline) - for att in ('title', 'alt', 'label', 'placeholder', 'aria-label'): - if att in el.attrib: - _push(callback, el.attrib[att], el.sourceline) + # Do not export terms contained on the Component directive of OWL + # attributes in this context are most of the time variables, + # not real HTML attributes. + # Node tags starting with a capital letter are considered OWL Components + # and a widespread convention and good practice for DOM tags is to write + # them all lower case. + # https://www.w3schools.com/html/html5_syntax.asp + # https://github.com/odoo/owl/blob/master/doc/reference/component.md#composition + if not el.tag[0].isupper() and 't-component' not in el.attrib: + for att in ('title', 'alt', 'label', 'placeholder', 'aria-label'): + if att in el.attrib: + _push(callback, el.attrib[att], el.sourceline) _extract_translatable_qweb_terms(el, callback) _push(callback, el.tail, el.sourceline)