1、Model not found: ir.translation
以前的历史版本做了一个小工具对翻译数据库在前端查找修改翻译,但在升级时,才发现odoo17去除了ir.translation表,查看分析了下原码,原来在这部分变化很大,在菜单中删除了以前的在设置中加载翻译的功能。17中显示如下:
代码:
class BaseLanguageImport(models.TransientModel):
_name = "base.language.import"
_description = "Language Import"
name = fields.Char('Language Name', required=True)
code = fields.Char('ISO Code', size=6, required=True,
help="ISO Language and Country code, e.g. en_US")
data = fields.Binary('File', required=True, attachment=False)
filename = fields.Char('File Name', required=True)
overwrite = fields.Boolean('Overwrite Existing Terms',
default=True,
help="If you enable this option, existing translations (including custom ones) "
"will be overwritten and replaced by those in this file")
def import_lang(self):
Lang = self.env["res.lang"]
for overwrite, base_lang_imports in tools.groupby(self, operator.itemgetter('overwrite')):
translation_importer = TranslationImporter(self.env.cr)
for base_lang_import in base_lang_imports:
if not Lang._activate_lang(base_lang_import.code):
Lang._create_lang(base_lang_import.code, lang_name=base_lang_import.name)
try:
with TemporaryFile('wb+') as buf:
buf.write(base64.decodebytes(base_lang_import.data))
fileformat = splitext(base_lang_import.filename)[-1][1:].lower()
translation_importer.load(buf, fileformat, base_lang_import.code)
except Exception as e:
_logger.warning('Could not import the file due to a format mismatch or it being malformed.')
raise UserError(
_('File %r not imported due to format mismatch or a malformed file.'
' (Valid formats are .csv, .po)\n\nTechnical Details:\n%s',
base_lang_import.filename, tools.ustr(e))
)
translation_importer.save(overwrite=overwrite)
return True
主要翻译模块代码:
def _load(self, reader, lang, xmlids=None):
if xmlids and not isinstance(xmlids, set):
xmlids = set(xmlids)
for row in reader:
if not row.get('value') or not row.get('src'): # ignore empty translations
continue
if row.get('type') == 'code': # ignore code translations
continue
model_name = row.get('imd_model')
module_name = row['module']
if model_name not in self.env:
continue
field_name = row['name'].split(',')[1]
field = self.env[model_name]._fields.get(field_name)
if not field or not field.translate or not field.store:
continue
xmlid = module_name + '.' + row['imd_name']
if xmlids and xmlid not in xmlids:
continue
if row.get('type') == 'model' and field.translate is True:
self.model_translations[model_name][field_name][xmlid][lang] = row['value']
elif row.get('type') == 'model_terms' and callable(field.translate):
self.model_terms_translations[model_name][field_name][xmlid][row['src']][lang] = row['value']
翻译字段主要放在了Ir_mode_相关数据库表的josnb字段中