Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
2 Odpowiedzi
4375 Widoki

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.

Awatar
Odrzuć
Autor Najlepsza odpowiedź

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.

Awatar
Odrzuć
Najlepsza odpowiedź

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.

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
3
lut 23
4465
Odoo 16 Rozwiązane
1
gru 22
3617
8
sty 25
5803
3
mar 24
2283
1
lut 24
1141