[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.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
});
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
is_superuser: <t t-esc="json.dumps(request.env.user._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'],
|
||||
};
|
||||
</script>
|
||||
<t t-if="not dont_load_assets">
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -132,6 +132,7 @@
|
||||
is_superuser: <t t-esc="json.dumps(request.env.user._is_superuser())"/>,
|
||||
is_system: <t t-esc="json.dumps(request.env.user._is_system())"/>,
|
||||
is_frontend: true,
|
||||
translationURL: '/website/translations',
|
||||
is_website_user: <t t-esc="json.dumps(request.env.user.id == request.website.user_id.id)" />,
|
||||
user_id: <t t-esc="json.dumps(request.env.user.id)" />
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user