diff --git a/bin/addons/base/ir/ir_translation.py b/bin/addons/base/ir/ir_translation.py index 631e42eb39b..6f8a4affbe9 100644 --- a/bin/addons/base/ir/ir_translation.py +++ b/bin/addons/base/ir/ir_translation.py @@ -25,7 +25,8 @@ import tools TRANSLATION_TYPE = [ ('field', 'Field'), ('model', 'Object'), - ('rml', 'RML'), + ('rml', 'RML (deprecated - use Report)'), # Pending deprecation - to be replaced by report! + ('report', 'Report/Template'), ('selection', 'Selection'), ('view', 'View'), ('wizard_button', 'Wizard Button'), @@ -131,14 +132,14 @@ class ir_translation(osv.osv): return len(ids) @tools.cache(skiparg=3) - def _get_source(self, cr, uid, name, tt, lang, source=None): + def _get_source(self, cr, uid, name, types, lang, source=None): """ Returns the translation for the given combination of name, type, language and source. All values passed to this method should be unicode (not byte strings), especially ``source``. :param name: identification of the term to translate, such as field name - :param type: type of term to translate (see ``type`` field on ir.translation) + :param types: single string defining type of term to translate (see ``type`` field on ir.translation), or sequence of allowed types (strings) :param lang: language code of the desired translation :param source: optional source term to translate (should be unicode) :rtype: unicode @@ -147,24 +148,25 @@ class ir_translation(osv.osv): """ # FIXME: should assert that `source` is unicode and fix all callers to always pass unicode # so we can remove the string encoding/decoding. - if not lang: return u'' + if isinstance(types, basestring): + types = (types,) if source: cr.execute('select value ' \ 'from ir_translation ' \ 'where lang=%s ' \ - 'and type=%s ' \ + 'and type in %s ' \ 'and name=%s ' \ 'and src=%s', - (lang or '', tt, tools.ustr(name), source)) + (lang or '', types, tools.ustr(name), source)) else: cr.execute('select value ' \ 'from ir_translation ' \ 'where lang=%s ' \ - 'and type=%s ' \ + 'and type in %s ' \ 'and name=%s', - (lang or '', tt, tools.ustr(name))) + (lang or '', types, tools.ustr(name))) res = cr.fetchone() trad = res and res[0] or u'' if source and not trad: diff --git a/bin/tools/translate.py b/bin/tools/translate.py index 64f742924f3..b9ce95cdaaa 100644 --- a/bin/tools/translate.py +++ b/bin/tools/translate.py @@ -23,6 +23,7 @@ import codecs import csv import fnmatch import inspect +import itertools import locale import os import re @@ -34,8 +35,7 @@ import logging from datetime import datetime from lxml import etree -import osv, tools, pooler -import ir +import tools, pooler import netsvc from tools.misc import UpdateableStr @@ -472,7 +472,6 @@ def trans_generate(lang, modules, dbname=None): query = 'SELECT name, model, res_id, module' \ ' FROM ir_model_data' - query_param = None if 'all_installed' in modules: query += ' WHERE module IN ( SELECT name FROM ir_module_module WHERE state = \'installed\') ' query_param = None @@ -589,7 +588,7 @@ def trans_generate(lang, modules, dbname=None): push_translation(module, 'model', name, 0, encode(obj_value[field_name])) if hasattr(field_def, 'selection') and isinstance(field_def.selection, (list, tuple)): - for key, val in field_def.selection: + for dummy, val in field_def.selection: push_translation(module, 'selection', name, 0, encode(val)) elif model=='ir.actions.report.xml': @@ -598,19 +597,18 @@ def trans_generate(lang, modules, dbname=None): if obj.report_rml: fname = obj.report_rml parse_func = trans_parse_rml - report_type = "rml" + report_type = "report" elif obj.report_xsl: fname = obj.report_xsl parse_func = trans_parse_xsl report_type = "xsl" - try: - xmlstr = tools.file_open(fname).read() - d = etree.XML(xmlstr) - for t in parse_func(d): - push_translation(module, report_type, name, 0, t) - except IOError, etree.XMLSyntaxError: - if fname: - logger.notifyChannel("i18n", netsvc.LOG_ERROR, "couldn't export translation for report %s %s %s" % (name, report_type, fname)) + if fname and obj.report_type in ('pdf', 'xsl'): + try: + d = etree.parse(tools.file_open(fname)) + for t in parse_func(d): + push_translation(module, report_type, name, 0, t) + except (IOError, etree.XMLSyntaxError): + logging.getLogger("i18n").exception("couldn't export translation for report %s %s %s", name, report_type, fname) for constraint in pool.get(model)._constraints: msg = constraint[1] @@ -666,22 +664,25 @@ def trans_generate(lang, modules, dbname=None): else : path_list = [root_path,tools.config['addons_path']] - for path in path_list: - for root, dirs, files in tools.osutil.walksymlinks(path): - for fname in fnmatch.filter(files, '*.py'): - fabsolutepath = join(root, fname) - frelativepath = fabsolutepath[len(path):] - module = get_module_from_path(frelativepath) - is_mod_installed = module in installed_modules - if (('all' in modules) or (module in modules)) and is_mod_installed: - code_string = tools.file_open(fabsolutepath, subdir='').read() - iter = re.finditer('[^a-zA-Z0-9_]_\([\s]*["\'](.+?)["\'][\s]*\)', - code_string, re.S) + def export_code_terms_from_file(fname, path, root, terms_type): + fabsolutepath = join(root, fname) + frelativepath = fabsolutepath[len(path):] + module = get_module_from_path(frelativepath) + is_mod_installed = module in installed_modules + if (('all' in modules) or (module in modules)) and is_mod_installed: + code_string = tools.file_open(fabsolutepath, subdir='').read() + iter = re.finditer('[^a-zA-Z0-9_]_\([\s]*["\'](.+?)["\'][\s]*\)', code_string, re.S) + if module in installed_modules: + frelativepath = str("addons" + frelativepath) + for i in iter: + push_translation(module, terms_type, frelativepath, 0, encode(i.group(1))) - if module in installed_modules : - frelativepath =str("addons"+frelativepath) - for i in iter: - push_translation(module, 'code', frelativepath, 0, encode(i.group(1))) + for path in path_list: + for root, dummy, files in tools.osutil.walksymlinks(path): + for fname in itertools.chain(fnmatch.filter(files, '*.py')): + export_code_terms_from_file(fname, path, root, 'code') + for fname in itertools.chain(fnmatch.filter(files, '*.mako')): + export_code_terms_from_file(fname, path, root, 'report') out = [["module","type","name","res_id","src","value"]] # header