[FIX] Update translations

bzr revid: fme@openerp.com-20130924190637-0qgzp58lzan8j1oc
This commit is contained in:
Fabien Meghazi 2013-09-24 21:06:37 +02:00
parent bcd3f471ad
commit f79d27e9a3
5 changed files with 57 additions and 33 deletions

View File

@ -161,10 +161,13 @@ class Website(openerp.addons.web.controllers.main.Home):
return result
@website.route('/website/get_view_translations', type='json', auth='admin')
def get_view_translations(self, xml_id, optional=False):
view = request.registry.get("ir.ui.view")
views = view._views_get(request.cr, request.uid, xml_id, request.context)
return []
def get_view_translations(self, xml_id, lang=None):
lang = lang or request.context.get('lang')
views = self.customize_template_get(xml_id, optional=False)
views_ids = [view.get('id') for view in views if view.get('active')]
domain = [('type', '=', 'view'), ('res_id', 'in', views_ids), ('lang', '=', lang)]
irt = request.registry.get('ir.translation')
return irt.search_read(request.cr, request.uid, domain, ['id', 'res_id', 'value'], context=request.context)
@website.route('/website/set_translations', type='json', auth='admin')
def set_translations(self, data, lang):
@ -174,17 +177,21 @@ class Website(openerp.addons.web.controllers.main.Home):
for t in trans:
initial_content = t['initial_content'].strip()
new_content = t['new_content'].strip()
old_trans = irt.search_read(
request.cr, request.uid,
[
('type', '=', 'view'),
('res_id', '=', view_id),
('lang', '=', lang),
('src', '=', initial_content),
])
if old_trans:
tid = t['translation_id']
if not tid:
old_trans = irt.search_read(
request.cr, request.uid,
[
('type', '=', 'view'),
('res_id', '=', view_id),
('lang', '=', lang),
('src', '=', initial_content),
])
if old_trans:
tid = old_trans[0]['id']
if tid:
vals = {'value': new_content}
irt.write(request.cr, request.uid, [old_trans[0]['id']], vals)
irt.write(request.cr, request.uid, [tid], vals)
else:
new_trans = {
'name': 'website',

View File

@ -124,6 +124,10 @@ table.editorbar-panel td.selected {
outline-color: red;
}
.oe_translatable_text.oe_dirty:empty {
padding: 0 10px;
}
.oe_translatable_todo {
background: #ffffb6;
}

View File

@ -103,6 +103,8 @@ table.editorbar-panel
outline: 1px dashed black
.oe_translatable_text.oe_dirty, .oe_translatable_field.oe_dirty
outline-color: red
.oe_translatable_text.oe_dirty:empty
padding: 0 10px
.oe_translatable_todo
background: rgb(255, 255, 182)
// }}}

View File

@ -40,7 +40,7 @@
this.translations = null;
openerp.jsonRpc('/website/get_view_translations', 'call', {
'xml_id': $(document.documentElement).data('view-xmlid'),
'context': website.get_context(),
'lang': website.get_context().lang,
}).then(function (translations) {
self.translations = translations;
self.processTranslatableNodes();
@ -48,12 +48,13 @@
},
processTranslatableNodes: function () {
var self = this;
var $editables = $('[data-oe-model]')
var $editables = $('[data-oe-model="ir.ui.view"]')
.not('link, script')
.not('.oe_snippets,.oe_snippet, .oe_snippet *')
.not('[data-oe-type]');
$editables.each(function () {
self.transNode(this);
self.transNode(this, $(this).attr('data-oe-id')|0);
});
$('.oe_translatable_text').prop('contenteditable', true).on('paste', function () {
var node = $(this);
@ -64,7 +65,7 @@
$(document).on('blur keyup paste', '.oe_translatable_text[contenteditable]', function(ev) {
var $node = $(this);
setTimeout(function () {
// Doing stuff next tick because past and keyup are
// Doing stuff next tick because paste and keyup events are
// fired before the content is changed
if (ev.type == 'paste') {
self.sanitizeNode($node[0]);
@ -84,26 +85,35 @@
isTranslatable: function (text) {
return text && _.str.trim(text) !== '';
},
markTranslatableNode: function (node) {
markTranslatableNode: function (node, view_id) {
// TODO: link nodes with same content
node.className += ' oe_translatable_text oe_translatable_todo';
node.className += ' oe_translatable_text';
node.setAttribute('data-oe-translation-view-id', view_id);
var content = node.childNodes[0].data.trim();
var trans = this.translations.filter(function (t) {
return t.res_id === view_id && t.value === content;
});
if (trans.length) {
node.setAttribute('data-oe-translation-id', trans[0].id);
} else {
node.className += ' oe_translatable_todo';
}
node.contentEditable = true;
$(node).data('initial_content', node.childNodes[0].data);
$(node).data('initial_content', content);
},
save: function () {
var trans = {};
// this._super.apply(this, arguments);
$('.oe_translatable_text.oe_dirty').each(function () {
var $node = $(this);
var text = $node.text();
var view_id = $(this).parents('[data-oe-model="ir.ui.view"]')
.not('[data-oe-type]').first().data().oeId;
if (!trans[view_id]) {
trans[view_id] = [];
var data = $node.data();
if (!trans[data.oeTranslationViewId]) {
trans[data.oeTranslationViewId] = [];
}
trans[view_id].push({
initial_content: $node.data('initial_content'),
new_content: text
trans[data.oeTranslationViewId].push({
initial_content: data.initial_content,
new_content: $node.text(),
translation_id: data.oeTranslationId || null
});
});
openerp.jsonRpc('/website/set_translations', 'call', {
@ -116,11 +126,11 @@
alert("Could not save translation");
});
},
transNode: function (node, context) {
transNode: function (node, view_id) {
if (node.childNodes.length === 1
&& this.isTextNode(node.childNodes[0])
&& !node.getAttribute('data-oe-model')) {
this.markTranslatableNode(node);
this.markTranslatableNode(node, view_id);
} else {
for (var i = 0, l = node.childNodes.length; i < l; i ++) {
var n = node.childNodes[i];
@ -129,10 +139,10 @@
var container = document.createElement('span');
node.insertBefore(container, n);
container.appendChild(n);
this.markTranslatableNode(container);
this.markTranslatableNode(container, view_id);
}
} else {
this.transNode(n);
this.transNode(n, view_id);
}
}
}

View File

@ -22,6 +22,7 @@
<meta name="openerp.company" t-att-value="res_company.name" />
<meta name="description" value="" />
<meta name="keywords" value="" />
<script type="text/javascript" src="/web/static/lib/es5-shim/es5-shim.min.js"></script>
<script type="text/javascript" src="/web/static/lib/underscore/underscore.js"></script>
<script type="text/javascript" src="/web/static/lib/underscore.string/lib/underscore.string.js"></script>
<script type="text/javascript" src="/web/static/lib/jquery/jquery.js"></script>