From 5257721bef578d6a1121a0a0c9e3d4119da8def4 Mon Sep 17 00:00:00 2001
From: Christophe Matthieu <chm@odoo.com>
Date: Wed, 9 Dec 2015 17:00:17 +0100
Subject: [PATCH] [FIX] web_editor: translation feature for all string fields
 on website

* add the branding in translation mode
* change javascript to add the lang in the context (automatically removed in the rte => allow to create record and edit when use an other default language of en_US)
* avoid double saved
---
 addons/web_editor/models/ir_qweb.py           |  7 ++++
 addons/web_editor/static/src/js/translator.js | 34 ++++++++++++++-----
 openerp/addons/base/ir/ir_qweb.py             |  4 ++-
 3 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/addons/web_editor/models/ir_qweb.py b/addons/web_editor/models/ir_qweb.py
index e45167e0f173..5fdbd4ae2093 100644
--- a/addons/web_editor/models/ir_qweb.py
+++ b/addons/web_editor/models/ir_qweb.py
@@ -28,6 +28,7 @@ from openerp.osv import orm, fields
 from openerp.tools import ustr, DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT
 from openerp.tools import html_escape as escape
 from openerp.addons.base.ir import ir_qweb
+from openerp.tools.translate import translate
 
 REMOTE_CONNECTION_TIMEOUT = 2.5
 
@@ -82,6 +83,12 @@ class Field(orm.AbstractModel):
         if placeholder:
             attrs.append(('placeholder', placeholder))
 
+        if context and context.get('edit_translations') and context.get('translatable') and field.type in ('char', 'text') and field.translate:
+            name = "%s,%s" % (record._model._name, field_name)
+            domain = [('name', '=', name), ('res_id', '=', record.id), ('type', '=', 'model'), ('lang', '=', context.get('lang'))]
+            translation = record.env['ir.translation'].search(domain, limit=1)
+            attrs.append(('data-oe-translation-state', translation and translation.state or 'to_translate'))
+
         return itertools.chain(
             super(Field, self).attributes(cr, uid, field_name, record, options,
                                           source_element, g_att, t_att,
diff --git a/addons/web_editor/static/src/js/translator.js b/addons/web_editor/static/src/js/translator.js
index 0d41699c3f30..e170b1fddea8 100644
--- a/addons/web_editor/static/src/js/translator.js
+++ b/addons/web_editor/static/src/js/translator.js
@@ -37,10 +37,12 @@ $.fn.extend({
 var RTE_Translate = rte.Class.extend({
     saveElement: function ($el, context) {
         // remove multi edition
+        var key = $el.data('oe-translation-id') ? 'translation:'+$el.data('oe-translation-id') :
+                ($el.data('oe-model') ? $el.data('oe-model')+":"+$el.data('oe-id')+":"+$el.data('oe-field')+":"+$el.data('oe-type')+":"+$el.data('oe-expression') : false);
+        if (!key || this.__saved[key]) return true;
+        this.__saved[key] = true;
+
         if ($el.data('oe-translation-id')) {
-            var key =  'translation:'+$el.data('oe-translation-id');
-            if (this.__saved[key]) return true;
-            this.__saved[key] = true;
             var translation_content = this.getEscapedElement($el).html();
 
             return ajax.jsonRpc('/web/dataset/call', 'call', {
@@ -48,13 +50,24 @@ var RTE_Translate = rte.Class.extend({
                 method: 'write',
                 args: [
                     [+$el.data('oe-translation-id')],
-                    {value: translation_content, state: 'translated'},
+                    {'value': translation_content, 'state': 'translated'},
                     context || base.get_context()
                 ],
             });
-        }
+        } else {
+            var markup = this.getEscapedElement($el).prop('outerHTML');
 
-        return this._super.apply(this, arguments);
+            return ajax.jsonRpc('/web/dataset/call', 'call', {
+                model: 'ir.ui.view',
+                method: 'save',
+                args: [
+                    $el.data('oe-id'),
+                    markup,
+                    $el.data('oe-xpath') || null,
+                    context || base.get_context()
+                ],
+            });
+        }
     },
 });
 
@@ -107,7 +120,10 @@ var Translate = Widget.extend({
         return this.edit();
     },
     setTarget: function ($target) {
-        this.$target = $target.find('[data-oe-translation-id], [data-oe-model][data-oe-id][data-oe-field]');
+        var $edit = $target.find('[data-oe-translation-id], [data-oe-model][data-oe-id][data-oe-field]');
+        $edit.filter(':has([data-oe-translation-id], [data-oe-model][data-oe-id][data-oe-field])').attr('data-oe-readonly', true);
+
+        this.$target = $edit.not('[data-oe-readonly]');
 
         // attributes
 
@@ -192,7 +208,7 @@ var Translate = Widget.extend({
         }
         event.preventDefault();
         event.stopPropagation();
-        if (event.type !== 'click') {
+        if (event.type !== 'mousedown') {
             return;
         }
 
@@ -302,6 +318,8 @@ if (edit_translations) {
         $('form[action*=edit_translations]').each(function () {
             this.action = this.action.replace(/[$?]edit_translations[^&?]+/, '');
         });
+
+        $('title').html($('title').html().replace(/&lt;span data-oe-translation-id.+?&gt;(.+?)&lt;\/span&gt;/, '\$1'));
     });
 }
 
diff --git a/openerp/addons/base/ir/ir_qweb.py b/openerp/addons/base/ir/ir_qweb.py
index 939af0c5286f..c71ad97d3135 100644
--- a/openerp/addons/base/ir/ir_qweb.py
+++ b/openerp/addons/base/ir/ir_qweb.py
@@ -635,7 +635,9 @@ class FieldConverter(osv.AbstractModel):
         if not inherit_branding and context and context.get('inherit_branding_auto'):
             inherit_branding = self.pool['ir.model.access'].check(cr, uid, record._name, 'write', False, context=context)
 
-        if inherit_branding:
+        translate = context and context.get('edit_translations') and context.get('translatable') and record._columns[field_name].translate
+
+        if inherit_branding or translate:
             # add branding attributes
             g_att += ''.join(
                 _build_attribute(name, value)
-- 
GitLab