[FIX] website: keep existing website-specific views translations

Since [1] when the translations becames stored as JSONB, the
translations of website-specific views were lost when adding a new
language.
[2] did fix a similar problem when installing an App, but did not
solve this.

This commit makes sure to only change the requested translations
without impacting the existing ones when a language is added.

Steps to reproduce:
- Add French language to the website
- Add the number snippet on the homepage
- Translate "Useful options" -> "Options utiles"
- Go to setting -> Languages -> Activate any language (not even on a
website)
=> French translation disappeared.

[1]: https://github.com/odoo/odoo/commit/ef00294e7189359c47638c4a71626f1937395edb
[2]: https://github.com/odoo/odoo/commit/91a9c870eede96040ac49d2375fe0e18342342e1

opw-3120079

closes odoo/odoo#111378

X-original-commit: 52fd577de8e270f84e8ca23c9350f2fb3ab5a7d8
Signed-off-by: Quentin Smetz (qsm) <qsm@odoo.com>
This commit is contained in:
Benoit Socias
2023-01-30 22:27:21 +01:00
parent d4edf55949
commit 3414bebed0
2 changed files with 21 additions and 8 deletions
+9 -8
View File
@@ -501,13 +501,15 @@ class IrModuleModule(models.Model):
if not generic_arch_db:
continue
langs_update = (langs & generic_arch_db.keys()) - {'en_US'}
specific_langs = (langs & specific_arch_db.keys()) - {'en_US'}
generic_arch_db_en = generic_arch_db.pop('en_US')
specific_arch_db_en = specific_arch_db.pop('en_US')
generic_arch_db = {k: generic_arch_db[k] for k in langs_update}
specific_arch_db = {k: specific_arch_db.get(k, specific_arch_db_en) for k in specific_langs}
generic_translation_dictionary = field.get_translation_dictionary(generic_arch_db_en, generic_arch_db)
specific_translation_dictionary = field.get_translation_dictionary(specific_arch_db_en, specific_arch_db)
if not langs_update:
continue
# get dictionaries limited to the requested languages
generic_arch_db_en = generic_arch_db.get('en_US')
specific_arch_db_en = specific_arch_db.get('en_US')
generic_arch_db_update = {k: generic_arch_db[k] for k in langs_update}
specific_arch_db_update = {k: specific_arch_db.get(k, specific_arch_db_en) for k in langs_update}
generic_translation_dictionary = field.get_translation_dictionary(generic_arch_db_en, generic_arch_db_update)
specific_translation_dictionary = field.get_translation_dictionary(specific_arch_db_en, specific_arch_db_update)
# update specific_translation_dictionary
for term_en, specific_term_langs in specific_translation_dictionary.items():
if term_en not in generic_translation_dictionary:
@@ -518,7 +520,6 @@ class IrModuleModule(models.Model):
for lang in langs_update:
specific_arch_db[lang] = field.translate(
lambda term: specific_translation_dictionary.get(term, {lang: None})[lang], specific_arch_db_en)
specific_arch_db['en_US'] = specific_arch_db_en
cache.update_raw(View.browse(specific_id), field, [specific_arch_db], dirty=True)
default_menu = self.env.ref('website.main_menu', raise_if_not_found=False)
+12
View File
@@ -1025,6 +1025,18 @@ class TestCowViewSaving(TestViewSavingCommon):
self.assertEqual(specific_view.with_context(lang='es_ES').arch, '<div>hola</div>',
"loading module translation should not remove specific translations that are not available on base view")
# Make sure updating a short list of languages does not destroy existing translations.
self.env['res.lang']._activate_lang('nl_NL')
self.env['ir.module.module']._load_module_terms(['website'], ['nl_NL'], overwrite=True)
specific_view.invalidate_model(['arch_db', 'arch'])
self.assertEqual(specific_view.with_context(lang='fr_BE').arch, '<div>salut</div>',
"loading module translation for a specific language should not remove existing translations for other languages")
self.assertEqual(specific_view.with_context(lang='es_ES').arch, '<div>hola</div>',
"loading module translation for a specific language should not remove existing translations for other languages")
def test_soc_complete_flow(self):
"""
Check the creation of views from specific-website environments.