[FIX] translate: do not export OWL directive's attribute

Have a static template of the form:

```xml
<div t-name="ComponentA">
 <ComponentB title="no export" />
</div>
```
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) <mat@odoo.com>
This commit is contained in:
Lucas Perais (lpe)
2020-04-17 12:55:32 +00:00
parent bf97152ce9
commit 69b51ce05e
4 changed files with 44 additions and 4 deletions
@@ -11,4 +11,7 @@
],
'installable': True,
'auto_install': False,
'qweb': [
'static/src/xml/js_templates.xml',
]
}
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<template xml:space="preserve">
<div t-name="ComponentA">
<div>text node</div>
<ComponentB title="no export" t-att-title="no export">
<div>slot</div>
</ComponentB>
<t t-component="ComponentB" title="no export" t-att-title="no export">
<div>slot 2</div>
</t>
<img title="do export" />
</div>
</template>
@@ -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)
+13 -3
View File
@@ -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)