Files
web/web_translate_dialog/models/base.py
Akim Juillerat aef961add1 [FIX] web_translate_dialog: Search using '=' to target right field
Without this commit, ir.translation are searched using a LIKE,
meaning that for a field called 'title', we'd get the translations
of all the fields beginning with title on this model.
Therefore, the returned value for field 'title' that is displayed
in the widget will be the value of the last result returned by
search_read, what could be anything beginning with title, and not
necessarily the field title itself.
2020-06-02 15:21:01 +02:00

37 lines
1.2 KiB
Python

# Copyright 2019 Camptocamp SA
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl)
from odoo import models, api
class BaseModel(models.BaseModel):
_inherit = 'base'
@api.multi
def get_field_translations(self, field_names):
"""Get only the existing translations for specified field
:param field_name: Name of the field
:return: dict of
{self.id: {'lang_code': {'field_name':ir.translation,value}}
"""
read_res = self.with_context(lang='en_US').read(fields=field_names)
res = {}
for rec in read_res:
rec_id = rec.get('id')
del rec['id']
res[rec_id] = {'en_US': rec}
for rec_id, values in res.items():
for name in field_names:
tr_read_res = self.env['ir.translation'].search_read([
('name', '=', '%s,%s' % (self._name, name)),
('res_id', '=', rec_id),
('lang', '!=', 'en_US')
])
for tr_res in tr_read_res:
if not tr_res.get('lang') in values:
values[tr_res.get('lang')] = {}
values[tr_res.get('lang')][name] = tr_res.get('value')
return res