跳至内容
菜单
此问题已终结
2 回复
5375 查看

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.

形象
丢弃
编写者 最佳答案

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.

形象
丢弃
最佳答案

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.

形象
丢弃
相关帖文 回复 查看 活动
3
2月 23
5407
Odoo 16 已解决
1
12月 22
4179
8
1月 25
6753
3
3月 24
2920
1
2月 24
1766