diff --git a/client/assets/libs/i18n.js b/client/assets/libs/i18n.js new file mode 100644 index 000000000..f64283422 --- /dev/null +++ b/client/assets/libs/i18n.js @@ -0,0 +1,45 @@ +function I18n(locale) { + this.update_locale(locale); +} + +I18n.prototype.update_locale = function(locale) { + this.jed = new Jed(locale); +}; + +I18n.prototype.translate = function(key) { + return this.jed.translate.call(this.jed, key); +}; + +I18n.prototype.translateDOM = function(el) { + var that = this, + parent, + nextSibling, + fragment; + + if (el.parent) { + parent = el.parent; + nextSibling = el.nextSibling; + fragment = parent.removeChild(el); + } else { + fragment = el; + } + + _.each(fragment.querySelectorAll('[data-translate]'), function (insertion_point) { + insertion_point.textContent = that.translate(insertion_point.getAttribute('data-translate')).fetch(); + }); + _.each(fragment.querySelectorAll('[data-translate-attr]'), function (insertion_point) { + var attr = insertion_point.getAttribute('data-translate-attr'), + key = insertion_point.getAttribute('data-translate-attr-key'); + insertion_point.setAttribute(attr, that.translate(key).fetch()); + }); + + if (parent) { + if (nextSibling) { + el = parent.insertBefore(el, nextSibling); + } else { + el = parent.appendChild(el); + } + } + + return el; +}; \ No newline at end of file diff --git a/client/src/app.js b/client/src/app.js index 4aedec780..0e13260fb 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -206,7 +206,7 @@ _kiwi.global = { var locale_promise, theme_promise, that = this; - opts = opts || {}; + this.opts = opts = opts || {}; this.initUtils(); @@ -222,9 +222,9 @@ _kiwi.global = { var locale = _kiwi.global.settings.get('locale') || opts.locale || opts.server_settings.client.settings.locale || 'magic'; $.getJSON(opts.base_path + '/assets/locales/' + locale + '.json', function (locale) { if (locale) { - that.i18n = new Jed(locale); + that.i18n = new I18n(locale); } else { - that.i18n = new Jed(); + that.i18n = new I18n(); } resolve(); }); diff --git a/client/src/applets/chanlist.js b/client/src/applets/chanlist.js index 39c63a635..85e747721 100644 --- a/client/src/applets/chanlist.js +++ b/client/src/applets/chanlist.js @@ -10,12 +10,10 @@ initialize: function (options) { - var text = { - channel_name: _kiwi.global.i18n.translate('client_applets_chanlist_channelname').fetch(), - users: _kiwi.global.i18n.translate('client_applets_chanlist_users').fetch(), - topic: _kiwi.global.i18n.translate('client_applets_chanlist_topic').fetch() - }; - this.$el = $(_.template($('#tmpl_channel_list').html().trim(), text)); + var tmp_el = document.createElement('div'); + tmp_el.innerHTML = $('#tmpl_channel_list').html().trim(); + _kiwi.global.i18n.translateDOM(tmp_el); + this.setElement(tmp_el.removeChild(tmp_el.firstChild)); this.channels = []; diff --git a/client/src/applets/scripteditor.js b/client/src/applets/scripteditor.js index e427fb303..21d2dc071 100644 --- a/client/src/applets/scripteditor.js +++ b/client/src/applets/scripteditor.js @@ -5,11 +5,12 @@ }, initialize: function (options) { - var that = this, - text = { - save: _kiwi.global.i18n.translate('client_applets_scripteditor_save').fetch() - }; - this.$el = $(_.template($('#tmpl_script_editor').html().trim(), text)); + var tmp_el = document.createElement('div'), + that = this; + + tmp_el.innerHTML = $('#tmpl_script_editor').html().trim(); + _kiwi.global.i18n.translateDOM(tmp_el); + this.setElement(tmp_el.removeChild(tmp_el.firstChild)); this.model.on('applet_loaded', function () { that.$el.parent().css('height', '100%'); diff --git a/client/src/applets/settings.js b/client/src/applets/settings.js index 44179f005..2161a85c1 100644 --- a/client/src/applets/settings.js +++ b/client/src/applets/settings.js @@ -106,6 +106,15 @@ settings.set($setting.data('setting'), value); settings.save(); + if ($setting.data('setting') === 'locale') { + $.getJSON(_kiwi.global.opts.base_path + '/assets/locales/' + value + '.json', function (locale) { + if (locale) { + _kiwi.global.i18n.update_locale(locale); + _kiwi.global.i18n.translateDOM(_kiwi.app.get('container')[0]); + } + }); + } + // Continue listening for setting changes _kiwi.global.settings.on('change', this.loadSettings, this); }, diff --git a/client/src/index.html.tmpl b/client/src/index.html.tmpl index f939581ba..14980b9c8 100644 --- a/client/src/index.html.tmpl +++ b/client/src/index.html.tmpl @@ -118,22 +118,22 @@ @@ -155,67 +155,67 @@