From 3414bebed0da185d8229ead5dd9ef37f8830dbca Mon Sep 17 00:00:00 2001 From: Benoit Socias Date: Thu, 26 Jan 2023 12:08:31 +0000 Subject: [PATCH] [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) --- addons/website/models/ir_module_module.py | 17 +++++++++-------- addons/website/tests/test_views.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/addons/website/models/ir_module_module.py b/addons/website/models/ir_module_module.py index 019519ce71a..b94aa3fff1e 100644 --- a/addons/website/models/ir_module_module.py +++ b/addons/website/models/ir_module_module.py @@ -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) diff --git a/addons/website/tests/test_views.py b/addons/website/tests/test_views.py index 30689189db5..802f1e479b4 100644 --- a/addons/website/tests/test_views.py +++ b/addons/website/tests/test_views.py @@ -1025,6 +1025,18 @@ class TestCowViewSaving(TestViewSavingCommon): self.assertEqual(specific_view.with_context(lang='es_ES').arch, '
hola
', "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, '
salut
', + "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, '
hola
', + "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.