def act_getfile(self, cr, uid, ids, context=None): this = self.browse(cr, uid, ids, context=context)[0] lang = this.lang if this.lang != NEW_LANG_KEY else False mods = sorted(map(lambda m: m.name, this.modules)) or ['all'] with contextlib.closing(cStringIO.StringIO()) as buf: tools.trans_export(lang, mods, buf, this.format, cr) out = base64.encodestring(buf.getvalue()) filename = 'new' if lang: filename = get_iso_codes(lang) elif len(mods) == 1: filename = mods[0] extension = this.format if not lang and extension == 'po': extension = 'pot' name = "%s.%s" % (filename, extension) this.write({ 'state': 'get', 'data': out, 'name': name }) return { 'type': 'ir.actions.act_window', 'res_model': 'base.language.export', 'view_mode': 'form', 'view_type': 'form', 'res_id': this.id, 'views': [(False, 'form')], 'target': 'new', }
def act_getfile(self, cr, uid, ids, context=None): this = self.browse(cr, uid, ids)[0] lang = this.lang if this.lang != NEW_LANG_KEY else False mods = map(lambda m: m.name, this.modules) or ['all'] mods.sort() buf = cStringIO.StringIO() tools.trans_export(lang, mods, buf, this.format, cr) filename = 'new' if lang: filename = get_iso_codes(lang) elif len(mods) == 1: filename = mods[0] this.name = "%s.%s" % (filename, this.format) out = base64.encodestring(buf.getvalue()) buf.close() self.write(cr, uid, ids, {'state': 'get', 'data': out, 'name':this.name}, context=context) return { 'type': 'ir.actions.act_window', 'res_model': 'base.language.export', 'view_mode': 'form', 'view_type': 'form', 'res_id': this.id, 'views': [(False, 'form')], 'target': 'new', }
def act_getfile(self, cr, uid, ids, context=None): this = self.browse(cr, uid, ids)[0] lang = this.lang if this.lang != NEW_LANG_KEY else False mods = map(lambda m: m.name, this.modules) or ['all'] mods.sort() buf = cStringIO.StringIO() trans_export( lang, mods, buf, this.format, cr, context={'export_type': this.export_type}) filename = 'new' if lang: filename = get_iso_codes(lang) elif len(mods) == 1: filename = mods[0] this.name = "%s.%s" % (filename, this.format) out = base64.encodestring(buf.getvalue()) buf.close() self.write(cr, uid, ids, {'state': 'get', 'data': out, 'name': this.name}, context=context) return { 'type': 'ir.actions.act_window', 'res_model': 'base.language.export', 'view_mode': 'form', 'view_type': 'form', 'res_id': this.id, 'views': [(False, 'form')], 'target': 'new', }
def button_save_translation(self): format_ = 'po' i18n_path = os.path.join(get_module_path(self.name), 'i18n') if not os.path.isdir(i18n_path): os.mkdir(i18n_path) lang_obj = self.env['res.lang'] condition = [('translatable', '=', True), ('code', '!=', 'en_US')] langs = lang_obj.search(condition) files = [('%s.pot' % self.name, False)] for lang in langs: iso_code = get_iso_codes(lang.code) filename = '%s.%s' % (iso_code, format_) files.append((filename, lang.code)) for filename, lang in files: path = os.path.join(i18n_path, filename) with open(path, 'w') as buf: tools.trans_export(lang, [self.name], buf, format_, self.env.cr) return True
def act_getfile(self, cr, uid, ids, context=None): this = self.browse(cr, uid, ids, context=context)[0] lang = this.lang if this.lang != NEW_LANG_KEY else False mods = sorted(map(lambda m: m.name, this.modules)) or ['all'] with contextlib.closing(cStringIO.StringIO()) as buf: tools.trans_export(lang, mods, buf, this.format, cr) out = base64.encodestring(buf.getvalue()) filename = 'new' if lang: filename = get_iso_codes(lang) elif len(mods) == 1: filename = mods[0] extension = this.format if not lang and extension == 'po': extension = 'pot' name = "%s.%s" % (filename, extension) this.write({'state': 'get', 'data': out, 'name': name}) return { 'type': 'ir.actions.act_window', 'res_model': 'base.language.export', 'view_mode': 'form', 'view_type': 'form', 'res_id': this.id, 'views': [(False, 'form')], 'target': 'new', }
def test_button_save_translation(self): load_language(self.cr, 'fr_FR') condition = [('name', '=', 'save_translation_file')] record = self.env['ir.module.module'].search(condition) record.button_save_translation() i18n_path = os.path.join(get_module_path(record.name), 'i18n') lang_obj = self.env['res.lang'] condition = [('translatable', '=', True), ('code', '!=', 'en_US')] langs = lang_obj.search(condition) for lang in langs: iso_code = get_iso_codes(lang.code) path = os.path.join(i18n_path, '%s.po' % iso_code) self.assertEqual(os.path.isfile(path), True, '%s.po must exists' % iso_code)
def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True, module_name=None, context=None): """Populates the ir_translation table.""" if verbose: _logger.info('loading translation file for language %s', lang) env = openerp.api.Environment(cr, SUPERUSER_ID, context or {}) Lang = env['res.lang'] Translation = env['ir.translation'] try: if not Lang.search_count([('code', '=', lang)]): # lets create the language with locale information Lang.load_lang(lang=lang, lang_name=lang_name) # Parse also the POT: it will possibly provide additional targets. # (Because the POT comments are correct on Launchpad but not the # PO comments due to a Launchpad limitation. See LP bug 933496.) pot_reader = [] # now, the serious things: we read the language file fileobj.seek(0) if fileformat == 'csv': reader = csv.reader(fileobj, quotechar='"', delimiter=',') # read the first line of the file (it contains columns titles) for row in reader: fields = row break elif fileformat == 'po': reader = PoFile(fileobj) fields = ['type', 'name', 'res_id', 'src', 'value', 'comments'] # Make a reader for the POT file and be somewhat defensive for the # stable branch. if fileobj.name.endswith('.po'): try: # Normally the path looks like /path/to/xxx/i18n/lang.po # and we try to find the corresponding # /path/to/xxx/i18n/xxx.pot file. # (Sometimes we have 'i18n_extra' instead of just 'i18n') addons_module_i18n, _ignored = os.path.split(fileobj.name) addons_module, i18n_dir = os.path.split(addons_module_i18n) addons, module = os.path.split(addons_module) pot_handle = file_open(os.path.join( addons, module, i18n_dir, module + '.pot')) pot_reader = PoFile(pot_handle) except: pass else: _logger.info('Bad file format: %s', fileformat) raise Exception(_('Bad file format: %s') % fileformat) # Read the POT references, and keep them indexed by source string. class Target(object): def __init__(self): self.value = None self.targets = set() # set of (type, name, res_id) self.comments = None pot_targets = defaultdict(Target) for type, name, res_id, src, _ignored, comments in pot_reader: if type is not None: target = pot_targets[src] target.targets.add((type, name, res_id)) target.comments = comments # read the rest of the file irt_cursor = Translation._get_import_cursor() def process_row(row): """Process a single PO (or POT) entry.""" # dictionary which holds values for this line of the csv file # {'lang': ..., 'type': ..., 'name': ..., 'res_id': ..., # 'src': ..., 'value': ..., 'module':...} dic = dict.fromkeys(('type', 'name', 'res_id', 'src', 'value', 'comments', 'imd_model', 'imd_name', 'module')) dic['lang'] = lang dic.update(zip(fields, row)) # discard the target from the POT targets. src = dic['src'] if src in pot_targets: target = pot_targets[src] target.value = dic['value'] target.targets.discard((dic['type'], dic['name'], dic['res_id'])) # This would skip terms that fail to specify a res_id res_id = dic['res_id'] if not res_id: return if isinstance(res_id, (int, long)) or \ (isinstance(res_id, basestring) and res_id.isdigit()): dic['res_id'] = int(res_id) if module_name: dic['module'] = module_name else: # res_id is an xml id dic['res_id'] = None dic['imd_model'] = dic['name'].split(',')[0] if '.' in res_id: dic['module'], dic['imd_name'] = res_id.split('.', 1) else: dic['module'], dic['imd_name'] = module_name, res_id irt_cursor.push(dic) # First process the entries from the PO file (doing so also fills/removes # the entries from the POT file). for row in reader: process_row(row) # Then process the entries implied by the POT file (which is more # correct w.r.t. the targets) if some of them remain. pot_rows = [] for src, target in pot_targets.iteritems(): if target.value: for type, name, res_id in target.targets: pot_rows.append((type, name, res_id, src, target.value, target.comments)) pot_targets.clear() for row in pot_rows: process_row(row) irt_cursor.finish() Translation.clear_caches() if verbose: _logger.info("translation file loaded succesfully") except IOError: iso_lang = get_iso_codes(lang) filename = '[lang: %s][format: %s]' % (iso_lang or 'new', fileformat) _logger.exception("couldn't read translation file %s", filename)
def trans_load_data(cr, fileobj, fileformat, lang, lang_name=None, verbose=True, module_name=None, context=None): """Populates the ir_translation table.""" if verbose: _logger.info('loading translation file for language %s', lang) env = openerp.api.Environment(cr, SUPERUSER_ID, context or {}) Lang = env['res.lang'] Translation = env['ir.translation'] try: if not Lang.search_count([('code', '=', lang)]): # lets create the language with locale information Lang.load_lang(lang=lang, lang_name=lang_name) # Parse also the POT: it will possibly provide additional targets. # (Because the POT comments are correct on Launchpad but not the # PO comments due to a Launchpad limitation. See LP bug 933496.) pot_reader = [] # now, the serious things: we read the language file fileobj.seek(0) if fileformat == 'csv': reader = csv.reader(fileobj, quotechar='"', delimiter=',') # read the first line of the file (it contains columns titles) for row in reader: fields = row break elif fileformat == 'po': reader = PoFile(fileobj) fields = ['type', 'name', 'res_id', 'src', 'value', 'comments'] # Make a reader for the POT file and be somewhat defensive for the # stable branch. if fileobj.name.endswith('.po'): try: # Normally the path looks like /path/to/xxx/i18n/lang.po # and we try to find the corresponding # /path/to/xxx/i18n/xxx.pot file. # (Sometimes we have 'i18n_extra' instead of just 'i18n') addons_module_i18n, _ignored = os.path.split(fileobj.name) addons_module, i18n_dir = os.path.split(addons_module_i18n) addons, module = os.path.split(addons_module) pot_handle = file_open(os.path.join( addons, module, i18n_dir, module + '.pot')) pot_reader = PoFile(pot_handle) except: pass else: _logger.info('Bad file format: %s', fileformat) raise Exception(_('Bad file format: %s') % fileformat) # Read the POT references, and keep them indexed by source string. class Target(object): def __init__(self): self.value = None self.targets = set() # set of (type, name, res_id) self.comments = None pot_targets = defaultdict(Target) for type, name, res_id, src, _ignored, comments in pot_reader: if type is not None: target = pot_targets[src] target.targets.add((type, name, res_id)) target.comments = comments # read the rest of the file irt_cursor = Translation._get_import_cursor() def process_row(row): """Process a single PO (or POT) entry.""" # dictionary which holds values for this line of the csv file # {'lang': ..., 'type': ..., 'name': ..., 'res_id': ..., # 'src': ..., 'value': ..., 'module':...} dic = dict.fromkeys(('type', 'name', 'res_id', 'src', 'value', 'comments', 'imd_model', 'imd_name', 'module')) dic['lang'] = lang dic.update(zip(fields, row)) # discard the target from the POT targets. src = dic['src'] if src in pot_targets: target = pot_targets[src] target.value = dic['value'] target.targets.discard((dic['type'], dic['name'], dic['res_id'])) # This would skip terms that fail to specify a res_id res_id = dic['res_id'] if not res_id: return if isinstance(res_id, (int, long)) or \ (isinstance(res_id, basestring) and res_id.isdigit()): dic['res_id'] = int(res_id) if module_name: dic['module'] = module_name else: # res_id is an xml id dic['res_id'] = None dic['imd_model'] = dic['name'].split(',')[0] if '.' in res_id: dic['module'], dic['imd_name'] = res_id.split('.', 1) else: dic['module'], dic['imd_name'] = module_name, res_id irt_cursor.push(dic) # First process the entries from the PO file (doing so also fills/removes # the entries from the POT file). for row in reader: process_row(row) # Then process the entries implied by the POT file (which is more # correct w.r.t. the targets) if some of them remain. pot_rows = [] for src, target in pot_targets.iteritems(): if target.value: for type, name, res_id in target.targets: pot_rows.append((type, name, res_id, src, target.value, target.comments)) pot_targets.clear() for row in pot_rows: process_row(row) irt_cursor.finish() Translation.clear_caches() if verbose: _logger.info("translation file loaded succesfully") except IOError: iso_lang = get_iso_codes(lang) filename = '[lang: %s][format: %s]' % (iso_lang or 'new', fileformat) _logger.exception("couldn't read translation file %s", filename)