From 9e71d228edb4a4405c6c17fbeabc8f7969c26dc5 Mon Sep 17 00:00:00 2001 From: Nicolas Martinelli Date: Fri, 7 Dec 2018 12:37:32 +0000 Subject: [PATCH] [FIX] mail: translation of custom layout - Create a PO for a French customer - Send by email The body of the email is in French, but some parts (such as the View button or the footer) are not translated. The body is translated following the field `lang` of the email template, in this case `${object.partner_id.lang}`. This is done when opening the mail composer (so the message appears in French). However, other parts are rendered only when sending: at this point, the language to be used in the template is lost forever in the darkness. To avoid this, we keep a specific context key `custom_layout_lang` that we retrieve when rendering the custom layout. opw-1908420 --- addons/mail/models/res_partner.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/addons/mail/models/res_partner.py b/addons/mail/models/res_partner.py index 32a2f4014bd..7ce819f799d 100644 --- a/addons/mail/models/res_partner.py +++ b/addons/mail/models/res_partner.py @@ -52,8 +52,16 @@ class Partner(models.Model): else: website_url = False + # Retrieve the language in which the template was rendered, in order to render the custom + # layout in the same language. + lang = self.env.context.get('lang') + if {'default_template_id', 'default_model', 'default_res_id'} <= self.env.context.keys(): + template = self.env['mail.template'].browse(self.env.context['default_template_id']) + if template and template.lang: + lang = template._render_template(template.lang, self.env.context['default_model'], self.env.context['default_res_id']) + if not model_description and message.model: - model_description = self.env['ir.model']._get(message.model).display_name + model_description = self.env['ir.model'].with_context(lang=lang)._get(message.model).display_name tracking = [] for tracking_value in self.env['mail.tracking.value'].sudo().search([('mail_message_id', '=', message.id)]): @@ -74,6 +82,7 @@ class Partner(models.Model): 'tracking_values': tracking, 'is_discussion': is_discussion, 'subtype': message.subtype_id, + 'lang': lang, } @api.model @@ -95,15 +104,14 @@ class Partner(models.Model): if not rdata: return True + base_template_ctx = self._notify_prepare_template_context(message, record, model_description=model_description) template_xmlid = message.layout if message.layout else 'mail.message_notification_email' try: - base_template = self.env.ref(template_xmlid, raise_if_not_found=True) + base_template = self.env.ref(template_xmlid, raise_if_not_found=True).with_context(lang=base_template_ctx['lang']) except ValueError: _logger.warning('QWeb template %s not found when sending notification emails. Sending without layouting.' % (template_xmlid)) base_template = False - base_template_ctx = self._notify_prepare_template_context(message, record, model_description=model_description) - # prepare notification mail values base_mail_values = { 'mail_message_id': message.id,