From 74da8e27c7e16fa92eb1547cec1a7ce82e15e0c6 Mon Sep 17 00:00:00 2001 From: nie Date: Thu, 1 Apr 2021 15:12:09 +0000 Subject: [FIX] mail: use template lang on layout and display name Steps: - Install sale - Go to Settings / Translations / Languages - Activate Dutch - Go to Settings / Users & Companies / Users - Edit demo - Language: Dutch - Go to Sales - Create a quotation: - Customer: demo - Go to Settings / Technical / Automation / Scheduled Actions - Create a new action: - Model: Sales Order - Python code: ```python last_id = model.search([], order='id desc', limit=1)[0].id env.ref('sale.email_template_edi_sale').send_mail(last_id, notif_layout='mail.mail_notification_paynow') ``` - Click "Run Manually" - Go to Settings / Technical / Email / Emails - Open the last email you just sent Bug: Parts of the email are not in Dutch Explanation: This commit makes a template pass the rendering language to the layout and the display name when calling `template.send_mail()`. opw:2467640 closes odoo/odoo#68656 Signed-off-by: Thibault Delavallee (tde) --- addons/mail/models/mail_template.py | 9 +++- addons/test_mail/tests/test_mail_template.py | 63 ++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/addons/mail/models/mail_template.py b/addons/mail/models/mail_template.py index d3363a8fd72..0e8e2cc855f 100644 --- a/addons/mail/models/mail_template.py +++ b/addons/mail/models/mail_template.py @@ -261,9 +261,16 @@ class MailTemplate(models.Model): _logger.warning('QWeb template %s not found when sending template %s. Sending without layouting.' % (notif_layout, self.name)) else: record = self.env[self.model].browse(res_id) + model = self.env['ir.model']._get(record._name) + + if self.lang: + lang = self._render_lang([res_id])[res_id] + template = template.with_context(lang=lang) + model = model.with_context(lang=lang) + template_ctx = { 'message': self.env['mail.message'].sudo().new(dict(body=values['body_html'], record_name=record.display_name)), - 'model_description': self.env['ir.model']._get(record._name).display_name, + 'model_description': model.display_name, 'company': 'company_id' in record and record['company_id'] or self.env.company, 'record': record, } diff --git a/addons/test_mail/tests/test_mail_template.py b/addons/test_mail/tests/test_mail_template.py index a845ffc0a79..5bf72f3da7c 100644 --- a/addons/test_mail/tests/test_mail_template.py +++ b/addons/test_mail/tests/test_mail_template.py @@ -54,6 +54,69 @@ class TestMailTemplate(TestMailCommon, TestRecipients): self.assertEqual(mail.email_cc, self.email_template.email_cc) self.assertEqual(mail.recipient_ids, self.partner_2 | self.user_admin.partner_id) + @mute_logger('odoo.addons.mail.models.mail_mail') + def test_template_translation(self): + self.env['res.lang']._activate_lang('es_ES') + self.env.ref('base.module_base')._update_translations(['es_ES']) + + partner = self.env['res.partner'].create({'name': "test", 'lang': 'es_ES'}) + email_template = self.env['mail.template'].create({ + 'name': 'TestTemplate', + 'subject': 'English Subject', + 'body_html': '

English Body

', + 'model_id': self.env['ir.model']._get(partner._name).id, + 'lang': '${object.lang}' + }) + # Make sure Spanish translations have not been altered + description_translations = self.env['ir.translation'].search([('module', '=', 'base'), ('src', '=', partner._description), ('lang', '=', 'es_ES')]) + description_translations.update({'value': 'Spanish description'}) + + self.env['ir.translation'].create({ + 'type': 'model', + 'name': 'mail.template,subject', + 'module': 'mail', + 'lang': 'es_ES', + 'res_id': email_template.id, + 'value': 'Spanish Subject', + 'state': 'translated', + }) + self.env['ir.translation'].create({ + 'type': 'model', + 'name': 'mail.template,body_html', + 'module': 'mail', + 'lang': 'es_ES', + 'res_id': email_template.id, + 'value': '

Spanish Body

', + 'state': 'translated', + }) + view = self.env['ir.ui.view'].create({ + 'name': 'test_layout', + 'key': 'test_layout', + 'type': 'qweb', + 'arch_db': ' English Layout ' + }) + self.env['ir.model.data'].create({ + 'name': 'test_layout', + 'module': 'test_mail', + 'model': 'ir.ui.view', + 'res_id': view.id + }) + self.env['ir.translation'].create({ + 'type': 'model_terms', + 'name': 'ir.ui.view,arch_db', + 'module': 'test_mail', + 'lang': 'es_ES', + 'res_id': view.id, + 'src': 'English Layout', + 'value': 'Spanish Layout', + 'state': 'translated', + }) + + mail_id = email_template.send_mail(partner.id, notif_layout='test_mail.test_layout') + mail = self.env['mail.mail'].sudo().browse(mail_id) + self.assertEqual(mail.subject, 'Spanish Subject') + self.assertEqual(mail.body_html, '

Spanish Body

Spanish Layout Spanish description') + def test_template_add_context_action(self): self.email_template.create_action() -- cgit v1.2.3