Move view translation into TranslationSet wizard
Since the possibility to have view XML in a separate file, the translation synchronization can not be done anymore in the check_xml. issue2926 review666003
This commit is contained in:
1 parent
00f7400e8c
commit
cab7a9778e
7 files changed
+139
-122
No files matched your search
@@ -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,
|
||||
|
||||
+119
-20
@@ -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'
|
||||
|
||||
|
||||
|
||||
+11
-11
@@ -240,26 +240,26 @@ this repository contains the full copyright notices and license terms. -->
|
||||
<field name="fuzzy" eval="False"/>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="translation_set_report_start_view_form">
|
||||
<field name="model">ir.translation.set_report.start</field>
|
||||
<record model="ir.ui.view" id="translation_set_start_view_form">
|
||||
<field name="model">ir.translation.set.start</field>
|
||||
<field name="type">form</field>
|
||||
<field name="name">translation_set_report_start_form</field>
|
||||
<field name="name">translation_set_start_form</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.ui.view" id="translation_set_report_succeed_view_form">
|
||||
<field name="model">ir.translation.set_report.succeed</field>
|
||||
<record model="ir.ui.view" id="translation_set_succeed_view_form">
|
||||
<field name="model">ir.translation.set.succeed</field>
|
||||
<field name="type">form</field>
|
||||
<field name="name">translation_set_report_succeed_form</field>
|
||||
<field name="name">translation_set_succeed_form</field>
|
||||
</record>
|
||||
|
||||
<record model="ir.action.wizard" id="act_translation_set_report">
|
||||
<record model="ir.action.wizard" id="act_translation_set">
|
||||
<field name="name">Set Report Translations</field>
|
||||
<field name="wiz_name">ir.translation.set_report</field>
|
||||
<field name="wiz_name">ir.translation.set</field>
|
||||
</record>
|
||||
<menuitem name="Set Report Translations"
|
||||
<menuitem name="Set Translations"
|
||||
parent="menu_localization"
|
||||
action="act_translation_set_report"
|
||||
id="menu_translation_set_report"
|
||||
action="act_translation_set"
|
||||
id="menu_translation_set"
|
||||
sequence="10"/>
|
||||
|
||||
<record model="ir.ui.view" id="translation_clean_start_view_form">
|
||||
|
||||
@@ -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'
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0"?>
|
||||
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
this repository contains the full copyright notices and license terms. -->
|
||||
<form string="Set Report Translations" col="2">
|
||||
<form string="Set Translations" col="2">
|
||||
<image name="tryton-dialog-information" xexpand="0" xfill="0"/>
|
||||
<label string="Set Translations Succeed!" id="succeed"
|
||||
<label string="Synchronize Translations?" id="synchronize"
|
||||
yalign="0.0" xalign="0.0" xexpand="1"/>
|
||||
</form>
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0"?>
|
||||
<!-- This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||
this repository contains the full copyright notices and license terms. -->
|
||||
<form string="Set Report Translations" col="2">
|
||||
<form string="Set Translations" col="2">
|
||||
<image name="tryton-dialog-information" xexpand="0" xfill="0"/>
|
||||
<label string="Synchronize Report Translations?" id="synchronize"
|
||||
<label string="Set Succeed!" id="succeed"
|
||||
yalign="0.0" xalign="0.0" xexpand="1"/>
|
||||
</form>
|
||||
+2
-2
@@ -580,8 +580,8 @@ this repository contains the full copyright notices and license terms. -->
|
||||
<field name="menu" ref="ir.menu_translation_form"/>
|
||||
<field name="group" ref="group_admin"/>
|
||||
</record>
|
||||
<record model="ir.ui.menu-res.group" id="menu_translation_set_report_group_admin">
|
||||
<field name="menu" ref="ir.menu_translation_set_report"/>
|
||||
<record model="ir.ui.menu-res.group" id="menu_translation_set_group_admin">
|
||||
<field name="menu" ref="ir.menu_translation_set"/>
|
||||
<field name="group" ref="group_admin"/>
|
||||
</record>
|
||||
<record model="ir.ui.menu-res.group" id="menu_translation_clean_group_admin">
|
||||
|
||||
Reference in new issue
Block a user