[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
This commit is contained in:
Nicolas Martinelli
2018-12-10 13:21:21 +00:00
parent 0687f17622
commit 9e71d228ed
+12 -4
View File
@@ -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,