Skip to Content
Menu
This question has been flagged
2 Replies
4276 Views

Please to not send me links to .po files or to documentation on how to do it through the interface etc. This is only the question: how to do it through python code in Odoo 16? I would love the following to work, but it does not work in Odoo 16, or it does not work with HTML fields. Whatever I do, all previously translated terms are replaced when I set a new value of translatable field in python code.


en_term = "invoice"
es_term = "factura"

record.with_context(lang="en_US").name = en_term
record.with_context(lang="es_ES").name = es_term


It's weird, I can read a term in a language (name = record.with_context(lang="es_ES").name), replace a letter (e.i. "a" by "o"), write it back, and it will override/remove *all* other translations and replace the term just with the newly modified one!


Is there any way to work around this? Any help appreciated.

Avatar
Discard
Author Best Answer

I found an answer myself. Here is how to achieve this in Odoo 16:

field_name = {the field name in question}
lang = {the language you want to update}
new_content = {the new content you want to set}

field = record._fields[field_name]
translations = field._get_stored_translations(record)
translations[lang] = new_content

record.env.cache.update_raw(
record, field, [translations], dirty=True
)
record.modified([field_name])

Keep in mind (for HTML-Fields): this will not update single terms, but the whole content. Exactly what we were looking for.

If there is a more straight forward solution, please, do not hesitate to share it.

Avatar
Discard
Best Answer

Bro, I had the same thing, I didn't know what to do while I was solving this problem, I used custom translations, and I want to tell you that it's not a bad thing. It takes the same amount of time as the translation I did myself, but I don't have to worry about it crashing and not responding, and I don't have to worry about them doing everything accurately. So try it and see how you like it.

Avatar
Discard
Related Posts Replies Views Activity
3
Feb 23
4383
Odoo 16 Solved
1
Dec 22
3581
8
Jan 25
5728
3
Mar 24
2253
1
Feb 24
1099