[FIX] tools: extract module from po file

When a single po file is processed (i.e. direct po import, not read from a i18n
directory inside a module), there was no module information on code translations

The module information is important for web translations where the list of
modules is given in the call to the route '/website/translations' and
'/web/webclient/translations' and only the web translations of these specific
modules is retrieved and used.

To manually import the translations of a custom module (e.g. openerp_enterprise),
the base.language.import wizard is typically used and the give .po file is
directly imported.
For model and model_terms, the module information is easily deducted from the
the record external id (<module>.<reference>) but for code translation, we
relied on the global variable module_name.

The .po file do contain the module information but this information was ignored.
Real example:

  #. module: openerp_enterprise
  #. openerp-web
  #: code:addons/openerp_enterprise/static/src/js/odoo_enterprise_start_trial.js:249
  #, python-format
  msgid "Please choose your domain name"
  msgstr "Veuillez choisir votre nom de domaine"

Before this commit, the above translation was imported without the module and
was not retrieved in the '/website/translations' call on the /trial page

The global module_name is still used when set for backward compatibility.

Co-authored-by: Raphael Collet <rco@odoo.com>
This commit is contained in:
Martin Trigaux
2019-03-29 14:48:57 +00:00
co-authored by Raphael Collet
parent 76264db40b
commit 257ccdf83c
2 changed files with 18 additions and 4 deletions
@@ -23,6 +23,18 @@ class TestTermCount(common.TransactionCase):
])
self.assertEqual(len(ids), 2)
def test_count_term_module(self):
"""
Just make sure we have as many translation entries as we wanted and module deducted from file content
"""
odoo.tools.trans_load(self.cr, 'test_translation_import/i18n/fr.po', 'fr_FR', verbose=False)
translations = self.env['ir.translation'].search([
('lang', '=', 'fr_FR'),
('src', '=', 'Ijkl'),
('module', '=', 'test_translation_import'),
])
self.assertEqual(len(translations), 1)
def test_noupdate(self):
"""
Make sure no update do not overwrite translations
+6 -4
View File
@@ -509,7 +509,7 @@ class PoFile(object):
return self.lines_count - len(self.lines)
def next(self):
trans_type = name = res_id = source = trad = None
trans_type = name = res_id = source = trad = module = None
if self.extra_lines:
trans_type, name, res_id, source, trad, comments = self.extra_lines.pop(0)
if not res_id:
@@ -530,6 +530,8 @@ class PoFile(object):
line = line[2:].strip()
if not line.startswith('module:'):
comments.append(line)
else:
module = line[7:].strip()
elif line.startswith('#:'):
# Process the `reference` comments. Each line can specify
# multiple targets (e.g. model, view, code, selection,
@@ -603,7 +605,7 @@ class PoFile(object):
_logger.warning('Missing "#:" formatted comment at line %d for the following source:\n\t%s',
self.cur_line(), source[:30])
return next(self)
return trans_type, name, res_id, source, trad, '\n'.join(comments)
return trans_type, name, res_id, source, trad, '\n'.join(comments), module
__next__ = next
def write_infos(self, modules):
@@ -1045,7 +1047,7 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
elif fileformat == 'po':
reader = PoFile(fileobj)
fields = ['type', 'name', 'res_id', 'src', 'value', 'comments']
fields = ['type', 'name', 'res_id', 'src', 'value', 'comments', 'module']
# Make a reader for the POT file and be somewhat defensive for the
# stable branch.
@@ -1079,7 +1081,7 @@ def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True,
self.comments = None
pot_targets = defaultdict(Target)
for type, name, res_id, src, _ignored, comments in pot_reader:
for type, name, res_id, src, _ignored, comments, module in pot_reader:
if type is not None:
target = pot_targets[src]
target.targets.add((type, name, type != 'code' and res_id or 0))