Created
February 15, 2022 18:51
-
-
Save em230418/dc0aa5dbae440cd253bb3053aa58f2ca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from odoo import api, fields, models | |
class ProductTemplate(models.Model): | |
_inherit = "product.template" | |
engrus_name = fields.Char(compute="_compute_translated_names") | |
vi_name = fields.Char(compute="_compute_translated_names") | |
en_name = fields.Char(compute="_compute_translated_names") | |
ru_name = fields.Char(compute="_compute_translated_names") | |
def _compute_translated_names(self): | |
translations_list = self.env["ir.translation"].search_read([ | |
("name", "=", "product.template,name"), | |
("res_id", "in", self.ids), | |
("lang", "in", ["en_US", "ru_RU", "vi_VN"]), | |
], ["lang", "res_id", "src", "value"]) | |
translations = { | |
"en_US": {}, | |
"ru_RU": {}, | |
"vi_VN": {}, | |
} | |
for t in translations_list: | |
translations[t["lang"]][t["res_id"]] = t["value"] or t["src"] | |
for record in self: | |
record.engrus_name = " / ".join(filter( | |
lambda x: x, | |
[ | |
translations["en_US"].get(record.id), | |
translations["ru_RU"].get(record.id), | |
] | |
)) or record.display_name | |
record.vi_name = translations["vi_VN"].get(record.id) or record.display_name | |
record.ru_name = translations["ru_RU"].get(record.id) or record.display_name | |
record.en_name = translations["en_US"].get(record.id) or record.display_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment