[FIX] base: fix translate in dev xml mode
Before this commit: in the --dev=xml mode, terms in ir_ui_view.arch cannot be correctly wrapped with translation tags in the edit_translation=True context After this commit: The logic of get_trans_func is moved to _compute_arch of model ir.ui.view, since ir_ui_view is its only use case and the hack with_context (edit_translation=None) makes the logic very confusing as a method for Fields. task-3225622 X-original-commit: 05ebdce14227b767bbddeab38e31f1eb747d9887 Part-of: odoo/odoo#117256
This commit is contained in:
committed by
Benoit Socias
parent
40b192dbbc
commit
7e19621fbc
@@ -303,6 +303,10 @@ actual arch.
|
||||
return m.group('prefix') + str(self.env['ir.model.data']._xmlid_to_res_id(xmlid))
|
||||
return re.sub(r'(?P<prefix>[^%])%\((?P<xmlid>.*?)\)[ds]', replacer, arch_fs)
|
||||
|
||||
lang = self.env.lang or 'en_US'
|
||||
env_en = self.with_context(edit_translations=None, lang='en_US').env
|
||||
env_lang = self.with_context(lang=lang).env
|
||||
field_arch_db = self._fields['arch_db']
|
||||
for view in self:
|
||||
arch_fs = None
|
||||
read_file = self._context.get('read_arch_from_file') or \
|
||||
@@ -316,9 +320,13 @@ actual arch.
|
||||
# replace %(xml_id)s, %(xml_id)d, %%(xml_id)s, %%(xml_id)d by the res_id
|
||||
if arch_fs:
|
||||
arch_fs = resolve_external_ids(arch_fs, xml_id).replace('%%', '%')
|
||||
if self.env.context.get('lang'):
|
||||
tr = self._fields['arch_db'].get_trans_func(view)
|
||||
arch_fs = tr(view.id, arch_fs)
|
||||
translation_dictionary = field_arch_db.get_translation_dictionary(
|
||||
view.with_env(env_en).arch_db, {lang: view.with_env(env_lang).arch_db}
|
||||
)
|
||||
arch_fs = field_arch_db.translate(
|
||||
lambda term: translation_dictionary[term][lang],
|
||||
arch_fs
|
||||
)
|
||||
else:
|
||||
_logger.warning("View %s: Full path [%s] cannot be found.", xml_id, view.arch_fs)
|
||||
arch_fs = False
|
||||
|
||||
@@ -1692,29 +1692,6 @@ class _String(Field):
|
||||
def convert_to_write(self, value, record):
|
||||
return value
|
||||
|
||||
def get_trans_func(self, records):
|
||||
""" Return a translation function `translate` for `self` on the given
|
||||
records; the function call `translate(record_id, value)` translates the
|
||||
field English value to the language given by the environment of `records`.
|
||||
"""
|
||||
lang = records.env.lang or 'en_US'
|
||||
if lang == 'en_US' or not self.translate:
|
||||
return lambda record_id, value: value
|
||||
# TODO: CWG: optimize it to one query
|
||||
vals_en2lang = zip(records.with_context(lang='en_US').mapped(self.name),
|
||||
records.with_context(lang=lang).mapped(self.name))
|
||||
translation_dictionaries = dict(
|
||||
zip(records.ids, [self.get_translation_dictionary(val_en, {lang: val_lang}) for val_en, val_lang in vals_en2lang]))
|
||||
if callable(self.translate):
|
||||
def translate(record_id, value):
|
||||
translation_dictionary = translation_dictionaries[record_id]
|
||||
# pylint: disable=not-callable
|
||||
return self.translate(lambda term: translation_dictionary[term][lang], value)
|
||||
else: # TODO CWG: TBD never used, useless?
|
||||
def translate(record_id, value):
|
||||
return translation_dictionaries.get(record_id).get(value, value)
|
||||
return translate
|
||||
|
||||
def get_translation_dictionary(self, from_lang_value, to_lang_values):
|
||||
""" Build a dictionary from terms in from_lang_value to terms in to_lang_values
|
||||
|
||||
|
||||
Reference in New Issue
Block a user