From 16f0e5031835765dcfed84a250119ea0f15eb30a Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Thu, 27 Jul 2017 11:02:28 +0200 Subject: [PATCH] [REF] web, web_editor, website: always let session load translations Before this commit, the session object was responsible for loading the translations in the backend only. There was then ugly code in web_editor (which is also loaded in backend...) which was doing "if frontend then load translations by myself". This code was also translating templates while this is the qweb object's job. Now session is always responsible for loading translation. Note: the session object should be refactored/split. --- addons/web/static/src/js/core/session.js | 15 ++++--- addons/web/static/src/js/core/translation.js | 5 ++- addons/web_editor/static/src/js/base.js | 46 ++------------------ addons/web_editor/views/iframe.xml | 1 + addons/website/controllers/main.py | 2 +- addons/website/static/src/js/website.js | 1 - addons/website/views/website_templates.xml | 1 + 7 files changed, 19 insertions(+), 52 deletions(-) diff --git a/addons/web/static/src/js/core/session.js b/addons/web/static/src/js/core/session.js index c98ff7fbe9a..c21a855bab5 100644 --- a/addons/web/static/src/js/core/session.js +++ b/addons/web/static/src/js/core/session.js @@ -87,14 +87,19 @@ var Session = core.Class.extend(mixins.EventDispatcherMixin, { * Init a session, reloads from cookie, if it exists */ session_init: function () { - var def = this.session_reload(); - if (this.is_frontend) return def; - var self = this; + var def = this.session_reload(); + + if (this.is_frontend) { + return def.then(function () { + return self.load_translations(); + }); + } + return def.then(function () { var modules = self.module_list.join(','); var deferred = self.load_qweb(modules); - if(self.session_is_valid()) { + if (self.session_is_valid()) { return deferred.then(function () { return self.load_modules(); }); } return $.when( @@ -217,7 +222,7 @@ var Session = core.Class.extend(mixins.EventDispatcherMixin, { }); }, load_translations: function () { - return _t.database.load_translations(this, this.module_list, this.user_context.lang); + return _t.database.load_translations(this, this.module_list, this.user_context.lang, this.translationURL); }, load_css: function (files) { var self = this; diff --git a/addons/web/static/src/js/core/translation.js b/addons/web/static/src/js/core/translation.js index f2bad0db4ee..fae44774a07 100644 --- a/addons/web/static/src/js/core/translation.js +++ b/addons/web/static/src/js/core/translation.js @@ -57,11 +57,12 @@ var TranslationDataBase = Class.extend(/** @lends instance.TranslationDataBase# it will default to all the modules installed in the current database. @param {Object} [lang] lang The language. If not specified it will default to the language of the current user. + @param {string} [url='/web/webclient/translations'] @returns {jQuery.Deferred} */ - load_translations: function(session, modules, lang) { + load_translations: function(session, modules, lang, url) { var self = this; - return session.rpc('/web/webclient/translations', { + return session.rpc(url || '/web/webclient/translations', { "mods": modules || null, "lang": lang || null }).done(function(trans) { diff --git a/addons/web_editor/static/src/js/base.js b/addons/web_editor/static/src/js/base.js index 4368f6de274..a241905c631 100644 --- a/addons/web_editor/static/src/js/base.js +++ b/addons/web_editor/static/src/js/base.js @@ -1,10 +1,8 @@ odoo.define('web_editor.base', function (require) { "use strict"; -var core = require('web.core'); var ajax = require('web.ajax'); -var qweb = core.qweb; -var _t = core._t; +var session = require('web.session'); var get_context = function (dict) { var html = document.documentElement; @@ -17,59 +15,21 @@ var get_context = function (dict) { var data = { 'get_context': get_context, - 'url_translations': '/web/webclient/translations', }; var dom_ready = $.Deferred(); $(document).ready(function () { dom_ready.resolve(data); // fix for ie - if($.fn.placeholder) $('input, textarea').placeholder(); + if ($.fn.placeholder) $('input, textarea').placeholder(); }); data.dom_ready = dom_ready; // todo: remove and load a bundle of translated templates -var all_ready; data.ready = function () { - if (!all_ready) { - all_ready = $.when(dom_ready, ajax.loadXML()).then(function () { - // TODO find a better way to find if we are in the backend or not - // We don't load translation if we are in the backend since it's already done by the webclient - if (!$(document).find('.o_web_client').length) { - return load_translations(); - } - }); - } else if(all_ready.state() === "resolved") { // can add async template - all_ready = $.when(dom_ready, ajax.loadXML()); - } - return all_ready; + return $.when(dom_ready, session.is_bound, ajax.loadXML()); }; -function load_translations() { - function translate_node(node) { - if(node.nodeType === 3) { // TEXT_NODE - if(node.nodeValue.match(/\S/)) { - var space = node.nodeValue.match(/^([\s]*)([\s\S]*?)([\s]*)$/); - node.nodeValue = space[1] + $.trim(_t(space[2])) + space[3]; - } - } - else if(node.nodeType === 1 && node.hasChildNodes()) { // ELEMENT_NODE - _.each(node.childNodes, translate_node); - } - } - return ajax.jsonRpc(data.url_translations, 'call', { - 'mods': ['web_editor'], - 'lang': data.get_context().lang - }) - .then(function (trans) { - _t.database.set_bundle(trans); - }).then(function () { - var keys = _.keys(qweb.templates); - for (var i = 0; i < keys.length; i++) { - translate_node(qweb.templates[keys[i]]); - } - }).promise(); -} return dom_ready; }); diff --git a/addons/web_editor/views/iframe.xml b/addons/web_editor/views/iframe.xml index b73611f545a..2b1c4209e58 100644 --- a/addons/web_editor/views/iframe.xml +++ b/addons/web_editor/views/iframe.xml @@ -20,6 +20,7 @@ is_superuser: , is_frontend: true, // this is not really the frontend here but this key is used to detect if xml has to be manually loaded }, + _modules: ['web_editor'], }; diff --git a/addons/website/controllers/main.py b/addons/website/controllers/main.py index e0eb3c34c3a..acc5e75a4d4 100644 --- a/addons/website/controllers/main.py +++ b/addons/website/controllers/main.py @@ -280,7 +280,7 @@ class Website(Home): def get_website_translations(self, lang, mods=None): Modules = request.env['ir.module.module'].sudo() modules = Modules.search([ - ('name', 'ilike', 'website'), + '|', ('name', 'ilike', 'website'), ('name', '=', 'web_editor'), ('state', '=', 'installed') ]).mapped('name') if mods: diff --git a/addons/website/static/src/js/website.js b/addons/website/static/src/js/website.js index 8324e7457ac..bf819b5664a 100644 --- a/addons/website/static/src/js/website.js +++ b/addons/website/static/src/js/website.js @@ -11,7 +11,6 @@ odoo.define('website.website', function (require) { var qweb = core.qweb; var _t = core._t; - base.url_translations = '/website/translations'; /* --- Set the browser into the dom for css selectors --- */ var browser; diff --git a/addons/website/views/website_templates.xml b/addons/website/views/website_templates.xml index cf5ba00df04..4bf165d3abe 100644 --- a/addons/website/views/website_templates.xml +++ b/addons/website/views/website_templates.xml @@ -132,6 +132,7 @@ is_superuser: , is_system: , is_frontend: true, + translationURL: '/website/translations', is_website_user: , user_id: };