2

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?

forvas
  • 9,801
  • 7
  • 62
  • 158

1 Answers1

1

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:

value returned by _() call

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).

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • 1
    +1 for your help since finally I've done what you recommended me (creating new templates and modifying the context defaults of each `mail.compose.message` wizard call). But I've left the question open, just in case someone can explain this behavior. Thank you! – forvas Sep 30 '19 at 11:04
  • @forvas I went out on the subject here, I edited my answer to explain what is happening and I hope you agree – Charif DZ Sep 30 '19 at 11:22
  • @forvas as you can see in the picture using debugger the code is only executed when I upgrade the module, but will not stop at this point when sending the email because this part of code it will not be called. and what you are doing here really is just saving the translation of `

    Hello customer.

    ` in SUPERUSER language.
    – Charif DZ Sep 30 '19 at 11:48
  • Finally you've found out the problem. So due to that behavior, when we want to update an email template which was defined inside a ``, it 's better to avoid Python to modify it (because the possible translations made by `_()` won't be called when loading the template), just create a new email template and call this instead, isn't it? – forvas Oct 03 '19 at 11:13
  • In this case yes, `_() ` return the translation when it's called. The problem that I noticed even when you update a record by xml the terms in xml where not exported I don't know if this is just in this case or for any other record. So If that is true if we need to update a record that have some field need to be translated we have to create a new one – Charif DZ Oct 03 '19 at 11:30