diff --git a/trytond/ir/__init__.py b/trytond/ir/__init__.py index bef2a35fa0..a1fe52e1c3 100644 --- a/trytond/ir/__init__.py +++ b/trytond/ir/__init__.py @@ -26,8 +26,8 @@ def register(): Pool.register( Configuration, Translation, - ReportTranslationSetStart, - ReportTranslationSetSucceed, + TranslationSetStart, + TranslationSetSucceed, TranslationCleanStart, TranslationCleanSucceed, TranslationUpdateStart, @@ -82,7 +82,7 @@ def register(): SessionWizard, module='ir', type_='model') Pool.register( - ReportTranslationSet, + TranslationSet, TranslationClean, TranslationUpdate, TranslationExport, diff --git a/trytond/ir/translation.py b/trytond/ir/translation.py index ccc5a9f8c3..6e851b99df 100644 --- a/trytond/ir/translation.py +++ b/trytond/ir/translation.py @@ -15,6 +15,7 @@ try: except ImportError: from md5 import md5 from functools import reduce +from lxml import etree from ..model import ModelView, ModelSQL, fields from ..wizard import Wizard, StateView, StateTransition, StateAction, \ @@ -27,8 +28,7 @@ from ..pool import Pool from ..cache import Cache __all__ = ['Translation', - 'ReportTranslationSetStart', 'ReportTranslationSetSucceed', - 'ReportTranslationSet', + 'TranslationSetStart', 'TranslationSetSucceed', 'TranslationSet', 'TranslationCleanStart', 'TranslationCleanSucceed', 'TranslationClean', 'TranslationUpdateStart', 'TranslationUpdate', 'TranslationExportStart', 'TranslationExportResult', 'TranslationExport', @@ -659,29 +659,28 @@ class Translation(ModelSQL, ModelView): return unicode(pofile).encode('utf-8') -class ReportTranslationSetStart(ModelView): - "Update Report Translation" - __name__ = 'ir.translation.set_report.start' +class TranslationSetStart(ModelView): + "Set Translation" + __name__ = 'ir.translation.set.start' -class ReportTranslationSetSucceed(ModelView): - "Update Report Translation" - __name__ = 'ir.translation.set_report.succeed' +class TranslationSetSucceed(ModelView): + "Set Translation" + __name__ = 'ir.translation.set.succeed' -class ReportTranslationSet(Wizard): - "Update report translation" - __name__ = "ir.translation.set_report" +class TranslationSet(Wizard): + "Set Translation" + __name__ = "ir.translation.set" - start = StateView('ir.translation.set_report.start', - 'ir.translation_set_report_start_view_form', [ + start = StateView('ir.translation.set.start', + 'ir.translation_set_start_view_form', [ Button('Cancel', 'end', 'tryton-cancel'), - Button('Start Update', 'set_report', 'tryton-ok', - default=True), + Button('Set', 'set_', 'tryton-ok', default=True), ]) - set_report = StateTransition() - succeed = StateView('ir.translation.set_report.succeed', - 'ir.translation_set_report_succeed_view_form', [ + set_ = StateTransition() + succeed = StateView('ir.translation.set.succeed', + 'ir.translation_set_succeed_view_form', [ Button('Ok', 'end', 'tryton-ok', default=True), ]) @@ -703,7 +702,7 @@ class ReportTranslationSet(Wizard): strings.extend(self._translate_report(child)) return strings - def transition_set_report(self): + def set_report(self): pool = Pool() Report = pool.get('ir.action.report') Translation = pool.get('ir.translation') @@ -712,7 +711,7 @@ class ReportTranslationSet(Wizard): reports = Report.search([]) if not reports: - return {} + return cursor = Transaction().cursor for report in reports: @@ -805,6 +804,106 @@ class ReportTranslationSet(Wizard): '(' + ','.join(('%s',) * len(strings)) + ')', (report.report_name, 'odt', report.module) + \ tuple(strings)) + + def _translate_view(self, element): + strings = [] + for attr in ('string', 'sum', 'confirm', 'help'): + if element.get(attr): + string = element.get(attr) + if string: + strings.append(string) + for child in element: + strings.extend(self._translate_view(child)) + return strings + + def set_view(self): + pool = Pool() + View = pool.get('ir.ui.view') + + with Transaction().set_context(active_test=False): + views = View.search([]) + + if not views: + return + cursor = Transaction().cursor + + for view in views: + cursor.execute('SELECT id, name, src FROM ir_translation ' + 'WHERE lang = %s ' + 'AND type = %s ' + 'AND name = %s ' + 'AND module = %s', + ('en_US', 'view', view.model, view.module)) + trans_views = {} + for trans in cursor.dictfetchall(): + trans_views[trans['src']] = trans + + xml = view.arch.strip() + if not xml: + continue + tree = etree.fromstring(xml) + root_element = tree.getroottree().getroot() + strings = self._translate_view(root_element) + with Transaction().set_context(active_test=False): + views2 = View.search([ + ('model', '=', view.model), + ('id', '!=', view.id), + ('module', '=', view.module), + ]) + for view2 in views2: + xml2 = view2.arch.strip() + if not xml2: + continue + tree2 = etree.fromstring(xml2) + root2_element = tree2.getroottree().getroot() + strings += self._translate_view(root2_element) + if not strings: + continue + for string in set(strings): + done = False + if string in trans_views: + del trans_views[string] + continue + string_md5 = Translation.get_src_md5(string) + for string_trans in trans_views: + if string_trans in strings: + continue + seqmatch = SequenceMatcher(lambda x: x == ' ', + string, string_trans) + if seqmatch.ratio() == 1.0: + del trans_views[string_trans] + done = True + break + if seqmatch.ratio() > 0.6: + cursor.execute('UPDATE ir_translation ' + 'SET src = %s, ' + 'src_md5 = %s, ' + 'fuzzy = %s ' + 'WHERE id = %s ', + (string, string_md5, True, + trans_views[string_trans]['id'])) + del trans_views[string_trans] + done = True + break + if not done: + cursor.execute('INSERT INTO ir_translation ' + '(name, lang, type, src, src_md5, value, module, ' + 'fuzzy) ' + 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', + (view.model, 'en_US', 'view', string, string_md5, '', + view.module, False)) + if strings: + cursor.execute('DELETE FROM ir_translation ' + 'WHERE name = %s ' + 'AND type = %s ' + 'AND module = %s ' + 'AND src NOT IN ' + '(' + ','.join(('%s',) * len(strings)) + ')', + (view.model, 'view', view.module) + tuple(strings)) + + def transition_set_(self): + self.set_report() + self.set_view() return 'succeed' diff --git a/trytond/ir/translation.xml b/trytond/ir/translation.xml index be81d67d0a..dc82cf1b8d 100644 --- a/trytond/ir/translation.xml +++ b/trytond/ir/translation.xml @@ -240,26 +240,26 @@ this repository contains the full copyright notices and license terms. --> - - ir.translation.set_report.start + + ir.translation.set.start form - translation_set_report_start_form + translation_set_start_form - - ir.translation.set_report.succeed + + ir.translation.set.succeed form - translation_set_report_succeed_form + translation_set_succeed_form - + Set Report Translations - ir.translation.set_report + ir.translation.set - diff --git a/trytond/ir/ui/view.py b/trytond/ir/ui/view.py index 359442174d..c150b895a3 100644 --- a/trytond/ir/ui/view.py +++ b/trytond/ir/ui/view.py @@ -3,7 +3,6 @@ import os import logging from lxml import etree -from difflib import SequenceMatcher from trytond.model import ModelView, ModelSQL, fields from trytond.backend import TableHandler from trytond.pyson import CONTEXT, Eval, Bool, PYSONDecoder @@ -112,19 +111,7 @@ class View(ModelSQL, ModelView): @classmethod def check_xml(cls, views): "Check XML" - pool = Pool() - Translation = pool.get('ir.translation') - cursor = Transaction().cursor for view in views: - cursor.execute('SELECT id, name, src FROM ir_translation ' \ - 'WHERE lang = %s ' \ - 'AND type = %s ' \ - 'AND name = %s '\ - 'AND module = %s', - ('en_US', 'view', view.model, view.module)) - trans_views = {} - for trans in cursor.dictfetchall(): - trans_views[trans['src']] = trans if not view.arch: continue xml = view.arch.strip() @@ -174,63 +161,6 @@ class View(ModelSQL, ModelView): if not encode(root_element): return False - strings = cls._translate_view(root_element) - with Transaction().set_user(0): - views2 = cls.search([ - ('model', '=', view.model), - ('id', '!=', view.id), - ('module', '=', view.module), - ]) - for view2 in views2: - xml2 = view2.arch.strip() - if not xml2: - continue - tree2 = etree.fromstring(xml2) - root2_element = tree2.getroottree().getroot() - strings += cls._translate_view(root2_element) - if not strings: - continue - for string in set(strings): - done = False - if string in trans_views: - del trans_views[string] - continue - string_md5 = Translation.get_src_md5(string) - for string_trans in trans_views: - if string_trans in strings: - continue - seqmatch = SequenceMatcher(lambda x: x == ' ', - string, string_trans) - if seqmatch.ratio() == 1.0: - del trans_views[string_trans] - done = True - break - if seqmatch.ratio() > 0.6: - cursor.execute('UPDATE ir_translation ' - 'SET src = %s, ' - 'src_md5 = %s, ' - 'fuzzy = %s ' - 'WHERE id = %s ', - (string, string_md5, True, - trans_views[string_trans]['id'])) - del trans_views[string_trans] - done = True - break - if not done: - cursor.execute('INSERT INTO ir_translation ' - '(name, lang, type, src, src_md5, value, module, ' - 'fuzzy) ' - 'VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', - (view.model, 'en_US', 'view', string, string_md5, '', - view.module, False)) - if strings: - cursor.execute('DELETE FROM ir_translation ' \ - 'WHERE name = %s ' \ - 'AND type = %s ' \ - 'AND module = %s ' \ - 'AND src NOT IN ' \ - '(' + ','.join(('%s',) * len(strings)) + ')', - (view.model, 'view', view.module) + tuple(strings)) return True def get_arch(self, name): @@ -269,18 +199,6 @@ class View(ModelSQL, ModelView): # Restart the cache ModelView._fields_view_get_cache.clear() - @classmethod - def _translate_view(cls, element): - strings = [] - for attr in ('string', 'sum', 'confirm', 'help'): - if element.get(attr): - string = element.get(attr) - if string: - strings.append(string) - for child in element: - strings.extend(cls._translate_view(child)) - return strings - class ShowViewStart(ModelView): 'Show view' diff --git a/trytond/ir/view/translation_set_report_succeed_form.xml b/trytond/ir/view/translation_set_start_form.xml similarity index 73% rename from trytond/ir/view/translation_set_report_succeed_form.xml rename to trytond/ir/view/translation_set_start_form.xml index 3a50c75571..1eedf03e52 100644 --- a/trytond/ir/view/translation_set_report_succeed_form.xml +++ b/trytond/ir/view/translation_set_start_form.xml @@ -1,8 +1,8 @@ -
+ -
- - + +