[FIX] fields, test_translate: multilanguage without en_US

Before, en_US was almost always the default in case of 'no language'.
Now, that should be superseded by lang=None;
yet still, en_US plays the role of source language.

In a multilanguage database where en_US was not installed,
writing a translation with lang=None would crash.
This could prevent module installation.

A test is added to cover that case.

In the case where we write a record with lang=None, the source is the
same as the record; in that case the cache contains the value for the
field.name under the context keys en_US and None, so we need to
invalidate the cache to avoid getting back the old value.

co-authored with mart-e

opw 2088487
opw 2083710

closes odoo/odoo#39130

X-original-commit: 1a5a999c2dd69c9a6a2dacb59e8bf45b09c6bc13
Signed-off-by: Nans Lefebvre (len) <len@odoo.com>
This commit is contained in:
Nans Lefebvre
2019-10-21 14:31:10 +00:00
committed by fw-bot
parent 84a89e1e1e
commit 4dcae232f3
2 changed files with 28 additions and 4 deletions
+23 -1
View File
@@ -479,7 +479,29 @@ class TestTranslationWrite(TransactionCase):
{'src': 'English Name', 'value': 'English Name', 'lang': 'en_US'},
{'src': 'English Name', 'value': 'French Name', 'lang': 'fr_FR'}
])
def test_04_fr_multi_no_en(self):
self.env['res.lang'].load_lang('fr_FR')
self.env['res.users'].with_context(active_test=False).search([]).write({'lang': 'fr_FR'})
self.env['res.lang'].load_lang('es_ES')
self.env.ref('base.lang_en').active = False
langs = self.env['res.lang'].get_installed()
self.assertEqual([('fr_FR', 'French / Français'), ('es_ES', 'Spanish / Español')], langs,
"Test did not start with the expected languages")
self.category.with_context(lang='fr_FR').write({'name': 'French Name'})
self.category.with_context(lang='es_ES').write({'name': 'Spanish Name'})
self.category.with_context(lang=None).write({'name': 'None Name'})
translations = self.env['ir.translation'].search([
('name', '=', 'res.partner.category,name'),
('res_id', '=', self.category.id),
], order='lang')
self.assertRecordValues(translations, [
{'src': 'None Name', 'value': 'Spanish Name', 'lang': 'es_ES'},
{'src': 'None Name', 'value': 'French Name', 'lang': 'fr_FR'},
])
def test_05_remove_multi(self):
self.env['res.lang'].load_lang('fr_FR')
+5 -3
View File
@@ -1439,7 +1439,7 @@ class _String(Field):
update_trans = False
single_lang = len(records.env['res.lang'].get_installed()) <= 1
if self.translate:
lang = records.env.lang or 'en_US'
lang = records.env.lang or None # used in _update_translations below
if single_lang:
# a single language is installed
update_trans = True
@@ -1447,10 +1447,11 @@ class _String(Field):
# update the source and synchronize translations
update_column = True
update_trans = True
elif lang != 'en_US':
elif lang != 'en_US' and lang is not None:
# update the translations only except if emptying
update_column = cache_value is None
update_trans = True
# else: lang = None
# update towrite if modifying the source
if update_column:
@@ -1461,6 +1462,7 @@ class _String(Field):
if self.translate is True and cache_value is not None:
tname = "%s,%s" % (records._name, self.name)
records.env['ir.translation']._set_source(tname, real_recs._ids, value)
records.invalidate_cache(fnames=[self.name], ids=records.ids)
if update_trans:
if callable(self.translate):
@@ -1471,7 +1473,7 @@ class _String(Field):
else:
# update translations
value = self.convert_to_column(value, records)
source_recs = real_recs.with_context(lang='en_US')
source_recs = real_recs.with_context(lang=None)
source_value = first(source_recs)[self.name]
if not source_value:
source_recs[self.name] = value