summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/framework/translation.js
diff options
context:
space:
mode:
authorGéry Debongnie <ged@odoo.com>2015-03-09 16:34:27 +0100
committerGéry Debongnie <ged@odoo.com>2015-03-18 09:01:49 +0100
commite8a00bc50da435d3859118ca127d20a2eb9a0cab (patch)
tree039f6ec40b37392e76c8830a8e518ac40f7e7fec /addons/web/static/src/js/framework/translation.js
parent9036084f065c7aca3b2199903f4da67db3501ba7 (diff)
[REF] web: add module system to the web client
This commit introduces a new module system for the javascript code. Instead of using global (namespaced) variables everywhere (like openerp.web.whatever, ...), each file declare one (or more) module with its dependencies. Each module will be loaded after its dependencies, and will be given a reference to the return values. This change is a massive departure from the way the client worked previously. It will sadly break some/most history, but I am sure that the future git ecosystem will allow you to easily browse the history past this revision (one can dream). This commit feature: * main namespace is now odoo instead of openerp * a way to define modules and dependencies (inspired by requirejs) * many files have been splitted in simpler subfiles * basic support for debugging in odoo.__DEBUG__ * no global variable (i.e. nothing in window.openerp) * many bug fixes, many new bugs Future vision: this modularization should simplify future code development, simplify debugging and will allow a serious testing effort
Diffstat (limited to 'addons/web/static/src/js/framework/translation.js')
-rw-r--r--addons/web/static/src/js/framework/translation.js98
1 files changed, 98 insertions, 0 deletions
diff --git a/addons/web/static/src/js/framework/translation.js b/addons/web/static/src/js/framework/translation.js
new file mode 100644
index 00000000000..6f44a57b275
--- /dev/null
+++ b/addons/web/static/src/js/framework/translation.js
@@ -0,0 +1,98 @@
+
+odoo.define('web.translation', ['web.Class', 'qweb'], function (require) {
+"use strict";
+
+var Class = require('web.Class');
+
+var qweb = require('qweb');
+
+
+var TranslationDataBase = Class.extend(/** @lends instance.TranslationDataBase# */{
+ init: function() {
+ this.db = {};
+ this.parameters = {"direction": 'ltr',
+ "date_format": '%m/%d/%Y',
+ "time_format": '%H:%M:%S',
+ "grouping": [],
+ "decimal_point": ".",
+ "thousands_sep": ","};
+ },
+ set_bundle: function(translation_bundle) {
+ var self = this;
+ this.db = {};
+ var modules = _.keys(translation_bundle.modules);
+ modules.sort();
+ if (_.include(modules, "web")) {
+ modules = ["web"].concat(_.without(modules, "web"));
+ }
+ _.each(modules, function(name) {
+ self.add_module_translation(translation_bundle.modules[name]);
+ });
+ if (translation_bundle.lang_parameters) {
+ this.parameters = translation_bundle.lang_parameters;
+ if (typeof(py) !== "undefined") {
+ this.parameters.grouping = py.eval(this.parameters.grouping);
+ }
+ }
+ },
+ add_module_translation: function(mod) {
+ var self = this;
+ _.each(mod.messages, function(message) {
+ self.db[message.id] = message.string;
+ });
+ },
+ build_translation_function: function() {
+ var self = this;
+ var fcnt = function(str) {
+ var tmp = self.get(str);
+ return tmp === undefined ? str : tmp;
+ };
+ fcnt.database = this;
+ return fcnt;
+ },
+ get: function(key) {
+ return this.db[key];
+ },
+ /**
+ Loads the translations from an OpenERP server.
+
+ @param {openerp.Session} session The session object to contact the server.
+ @param {Array} [modules] The list of modules to load the translation. If not specified,
+ 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.
+ @returns {jQuery.Deferred}
+ */
+ load_translations: function(session, modules, lang) {
+ var self = this;
+ return session.rpc('/web/webclient/translations', {
+ "mods": modules || null,
+ "lang": lang || null
+ }).done(function(trans) {
+ self.set_bundle(trans);
+ });
+ }
+});
+
+var _t = new TranslationDataBase().build_translation_function();
+var _lt = function (s) {
+ return {toString: function () { return _t(s); }};
+};
+
+// provide timeago.js with our own translator method
+$.timeago.settings.translator = _t;
+
+qweb.default_dict = {
+ '_' : _,
+ 'JSON': JSON,
+ '_t' : _t,
+};
+
+return {
+ _t: _t,
+ _lt: _lt,
+ TranslationDataBase: TranslationDataBase,
+};
+
+});
+