Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
4380 Lượt xem

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.

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 2 23
4466
Odoo 16 Đã xử lý
1
thg 12 22
3619
8
thg 1 25
5804
3
thg 3 24
2285
1
thg 2 24
1143