I want to modify the body of an email template, for example email_template_edi_sale
in the module sale
.
Its declaration is inside a <data no update="1">
tag, which means that I have to do a workaround to modify it. So I have decided to make a Python method which does the trick and call it from XML:
XML
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="0">
<function model="mail.template" name="_update_existing_mail_templates"/>
</data>
</odoo>
Python
class MailTemplate(models.Model):
_inherit = 'mail.template'
@api.model
def _update_existing_mail_templates(self):
edi_sale = self.env.ref('sale.email_template_edi_sale', False)
if edi_sale:
body_html = _("""<p>Hello customer.</p>""")
edi_sale.write({
'body_html': body_html,
})
This solution works as expected, since when the template is loaded, I only see Hello customer. Now I want to export its translation. The xx.po file is rightly exported since I have available to translate the term <p>Hello customer.</p>
. I translate it to Spanish for example, so I generate the es.po file. Then I update the module and load the translation in Spanish overwriting terms. To check that this worked, I look for the term in the Translated terms menu, and I find it rightly translated. But then, I send by email a sale order of a customer (who has Spanish language set), and the template is loaded with the source text, in English.
I was not able to figure out what is happening.
There is a similar question here: Email template translation Odoo 10
But the guy who asks solved the problem translating the template by hand through the interface. In my case I cannot even apply that solution since the term is rightly translated in the interface.
Any idea to do this OK?
this code will not be called when the template
is rendered. the _()
method return the translation of the given argument when it's called for example when you raise
an exception
at that moment you call _(msg)
the method will return the translation of the message, but here this code is only executed when you install
or upgrade
the module (when the XML file is loading)
.
To demonstrate change the Language of the SUPERUSER
to Spanish then upgrade the module, inspect the value in the database you will find that it's the translation of """<p>Hello customer.</p>"""
. Because as you know when you install or upgrade a module all operation are done by super user not matter the user who clicked the button(Like: self = self.sudo()
)
I changed the Language of my SUPERUSER
to French then I upgraded the module:
So actually what you are doing here is just translating the value that will be passed to the write method and saved in database.
and even you force the update by changing the noupdate
flag in ir.model.data
of this record
you cannot export it to PO, because Odoo ignore it, and I really don't know why they decided to do this(or may be didn't work just with my version don't really know).