def import_(ctx, language, db_name, overwrite): context = { 'overwrite': overwrite } from odoo.modules.registry import RegistryManager from odoo.api import Environment from odoo.tools import trans_load with tempfile.NamedTemporaryFile(suffix='.po', delete=False) as t: registry = RegistryManager.get(db_name) # Read from stdin while True: chunk = sys.stdin.read(CHUNK_SIZE) if not chunk: break t.write(chunk) t.close() with Environment.manage(): with registry.cursor() as cr: trans_load(cr, t.name, language, context=context) os.unlink(t.name)
def load_custom_module_terms(self, mods, langs): # 该操作耗时较长,将流程与系统原有流程分离。 # res = super(IrTranslation, self).load_module_terms(modules, langs) mod_dict = { mod.name: mod.dependencies_id.mapped('name') for mod in mods } modules = topological_sort(mod_dict) res_lang = self.env['res.lang'].sudo() for lang in langs: res_lang.load_lang(lang) for module_name in modules: modpath = get_module_path(module_name) if not modpath: continue for lang in langs: context = dict(self._context) lang_code = tools.get_iso_codes(lang) lmch_extra_file = get_lmch_extra_file(module_name, lang_code) if lmch_extra_file: _logger.info( u'模块 %s: loading lmch extra translation file (%s) for language %s', module_name, lang_code, lang) tools.trans_load(self._cr, lmch_extra_file, lang, verbose=False, module_name=module_name, context=context) return True
def update_language(self): context = dict(self._context) context['overwrite'] = True path_module = get_module_path('vietnam_translate') print(path_module) if path_module: path_module += '/i18n' files = os.listdir(path_module) if files: for f in files: module_name = f.split('.')[0] modules = self.env['ir.module.module'].sudo().search( [('name', '=', module_name)]) if modules: tools.trans_load(self._cr, path_module + '/' + f, 'vi_VN', verbose=False, module_name=modules[0].name, context=context) return {'type': 'ir.actions.act_window_close'}
def _load_module_terms(self, modules, langs): """ Load PO files of the given modules for the given languages. """ # make sure the given languages are active res_lang = self.env['res.lang'].sudo() for lang in langs: res_lang.load_lang(lang) # load i18n files for module_name in modules: modpath = get_module_path(module_name) if not modpath: continue for lang in langs: context = dict(self._context) lang_code = tools.get_iso_codes(lang) base_lang_code = None if '_' in lang_code: base_lang_code = lang_code.split('_')[0] # Step 1: for sub-languages, load base language first (e.g. es_CL.po is loaded over es.po) if base_lang_code: base_trans_file = get_module_resource(module_name, 'i18n', base_lang_code + '.po') if base_trans_file: _logger.info('module %s: loading base translation file %s for language %s', module_name, base_lang_code, lang) tools.trans_load(self._cr, base_trans_file, lang, verbose=False, module_name=module_name, context=context) context['overwrite'] = True # make sure the requested translation will override the base terms later # i18n_extra folder is for additional translations handle manually (eg: for l10n_be) base_trans_extra_file = get_module_resource(module_name, 'i18n_extra', base_lang_code + '.po') if base_trans_extra_file: _logger.info('module %s: loading extra base translation file %s for language %s', module_name, base_lang_code, lang) tools.trans_load(self._cr, base_trans_extra_file, lang, verbose=False, module_name=module_name, context=context) context['overwrite'] = True # make sure the requested translation will override the base terms later # Step 2: then load the main translation file, possibly overriding the terms coming from the base language trans_file = get_module_resource(module_name, 'i18n', lang_code + '.po') if trans_file: _logger.info('module %s: loading translation file (%s) for language %s', module_name, lang_code, lang) tools.trans_load(self._cr, trans_file, lang, verbose=False, module_name=module_name, context=context) elif lang_code != 'en_US': _logger.info('module %s: no translation for language %s', module_name, lang_code) trans_extra_file = get_module_resource(module_name, 'i18n_extra', lang_code + '.po') if trans_extra_file: _logger.info('module %s: loading extra translation file (%s) for language %s', module_name, lang_code, lang) tools.trans_load(self._cr, trans_extra_file, lang, verbose=False, module_name=module_name, context=context) return True
def load_module_terms(self, modules, langs): """ Load PO files of the given modules for the given languages. """ # make sure the given languages are active res_lang = self.env['res.lang'].sudo() for lang in langs: res_lang.load_lang(lang) # load i18n files for module_name in modules: modpath = get_module_path(module_name) if not modpath: continue for lang in langs: context = dict(self._context) lang_code = tools.get_iso_codes(lang) base_lang_code = None if '_' in lang_code: base_lang_code = lang_code.split('_')[0] # Step 1: for sub-languages, load base language first (e.g. es_CL.po is loaded over es.po) if base_lang_code: base_trans_file = get_module_resource(module_name, 'i18n', base_lang_code + '.po') if base_trans_file: _logger.info('module %s: loading base translation file %s for language %s', module_name, base_lang_code, lang) tools.trans_load(self._cr, base_trans_file, lang, verbose=False, module_name=module_name, context=context) context['overwrite'] = True # make sure the requested translation will override the base terms later # i18n_extra folder is for additional translations handle manually (eg: for l10n_be) base_trans_extra_file = get_module_resource(module_name, 'i18n_extra', base_lang_code + '.po') if base_trans_extra_file: _logger.info('module %s: loading extra base translation file %s for language %s', module_name, base_lang_code, lang) tools.trans_load(self._cr, base_trans_extra_file, lang, verbose=False, module_name=module_name, context=context) context['overwrite'] = True # make sure the requested translation will override the base terms later # Step 2: then load the main translation file, possibly overriding the terms coming from the base language trans_file = get_module_resource(module_name, 'i18n', lang_code + '.po') if trans_file: _logger.info('module %s: loading translation file (%s) for language %s', module_name, lang_code, lang) tools.trans_load(self._cr, trans_file, lang, verbose=False, module_name=module_name, context=context) elif lang_code != 'en_US': _logger.info('module %s: no translation for language %s', module_name, lang_code) trans_extra_file = get_module_resource(module_name, 'i18n_extra', lang_code + '.po') if trans_extra_file: _logger.info('module %s: loading extra translation file (%s) for language %s', module_name, lang_code, lang) tools.trans_load(self._cr, trans_extra_file, lang, verbose=False, module_name=module_name, context=context) return True
def update_language(self): context = dict(self._context) context['overwrite'] = True path_modules_list = self.get_path_module_translate() module_name_list = [] module_name_dict = {} if path_modules_list: if not self.module_upgrade: for path_module in path_modules_list: path, file = os.path.split(path_module) module_name = file.replace('.po', '') module_name_list.append(module_name) module_name_dict[module_name] = path_module # modules = self.env['ir.module.module'].sudo().search( # [ # ('name', '=', module_name), # ('state', 'in', ['installed', 'to upgrade', 'to remove']) # ]) modules = self.env['ir.module.module'].sudo().search_read([ ('name', 'in', module_name_list), ('state', 'in', ['installed', 'to upgrade', 'to remove']) ], ['name']) if modules: self.env.cr.execute( "delete from ir_translation where module in %s", (tuple(module_name_list), )) for module in modules: path_module = module_name_dict.get(module['name']) tools.trans_load(self._cr, path_module, 'vi_VN', verbose=False, module_name=module['name'], context=context) else: for path_module in self.module_upgrade: # path, file = os.path.split(path_module) # module_name = file.replace('.po', '') module_name_list.append(path_module.name) module_name_dict[path_module.name] = path_module modules = self.env['ir.module.module'].sudo().search_read([ ('name', 'in', module_name_list), ('state', 'in', ['installed', 'to upgrade', 'to remove']) ], ['name']) if modules: self.env.cr.execute( "delete from ir_translation where module in %s", (tuple(module_name_list), )) for module in modules: path_vn = get_module_path('vietnam_translate') path_modules = os.path.join(path_vn, 'i18n') files_list = os.listdir(path_modules) exist_module = list( filter(lambda x: x == (module['name'] + '.po'), files_list)) if exist_module: # path_module = module_name_dict.get(module['name']) tools.trans_load( self._cr, (path_modules + '\\' + exist_module[0]), 'vi_VN', verbose=False, module_name=module['name'], context=context) else: module_dir = get_module_path(module['name']) module_dir = os.path.join(module_dir, 'i18n') files_dir = os.listdir(module_dir) module_directory = os.path.join( module_dir, files_dir[0]) tools.trans_load(self._cr, module_directory, 'vi_VN', verbose=False, module_name=module['name'], context=context) return {'type': 'ir.actions.act_window_close'}