Update i18next to 1.7.1 (non-AMD) and fix i18next loading issues

Change-Id: If90ab5eb966326d3679050951cf2f92eb35ecb02
Closes-Bug: 1249206
This commit is contained in:
Vitaly Kramskikh 2013-11-08 14:24:00 +04:00
parent 47c1df4690
commit b3596e2481
9 changed files with 125 additions and 65 deletions

View File

@ -1,20 +1,8 @@
// i18next, v1.6.3 // i18next, v1.7.1
// Copyright (c)2013 Jan Mühlemann (jamuhl). // Copyright (c)2013 Jan Mühlemann (jamuhl).
// Distributed under MIT license // Distributed under MIT license
// http://i18next.com // http://i18next.com
(function (root, factory) { (function() {
if (typeof exports === 'object') {
var jquery = require('jquery');
module.exports = factory(jquery);
} else if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
}
}(this, function ($) {
// add indexOf to non ECMA-262 standard compliant browsers // add indexOf to non ECMA-262 standard compliant browsers
if (!Array.prototype.indexOf) { if (!Array.prototype.indexOf) {
@ -81,12 +69,28 @@
}; };
} }
var i18n = {} var root = this
, resStore = {} , $ = root.jQuery || root.Zepto
, currentLng , i18n = {}
, replacementCounter = 0 , resStore = {}
, languages = []; , currentLng
, replacementCounter = 0
, languages = []
, initialized = false;
// Export the i18next object for **CommonJS**.
// If we're not in CommonJS, add `i18n` to the
// global object or to jquery.
if (typeof module !== 'undefined' && module.exports) {
module.exports = i18n;
} else {
if ($) {
$.i18n = $.i18n || i18n;
}
root.i18n = root.i18n || i18n;
}
// defaults // defaults
var o = { var o = {
lng: undefined, lng: undefined,
@ -135,9 +139,12 @@
cookieExpirationTime: undefined, cookieExpirationTime: undefined,
useCookie: true, useCookie: true,
cookieName: 'i18next', cookieName: 'i18next',
cookieDomain: undefined,
postProcess: undefined, postProcess: undefined,
parseMissingKey: undefined parseMissingKey: undefined,
shortcutFunction: 'sprintf' // or: defaultValue
}; };
function _extend(target, source) { function _extend(target, source) {
if (!source || typeof source === 'function') { if (!source || typeof source === 'function') {
@ -497,7 +504,7 @@
} }
var _cookie = { var _cookie = {
create: function(name,value,minutes) { create: function(name,value,minutes,domain) {
var expires; var expires;
if (minutes) { if (minutes) {
var date = new Date(); var date = new Date();
@ -505,7 +512,8 @@
expires = "; expires="+date.toGMTString(); expires = "; expires="+date.toGMTString();
} }
else expires = ""; else expires = "";
document.cookie = name+"="+value+expires+"; path=/"; domain = (domain)? "domain="+domain+";" : "";
document.cookie = name+"="+value+expires+";"+domain+"path=/";
}, },
read: function(name) { read: function(name) {
@ -525,7 +533,7 @@
}; };
var cookie_noop = { var cookie_noop = {
create: function(name,value,minutes) {}, create: function(name,value,minutes,domain) {},
read: function(name) { return null; }, read: function(name) { return null; },
remove: function(name) {} remove: function(name) {}
}; };
@ -577,6 +585,7 @@
// override defaults with passed in options // override defaults with passed in options
f.extend(o, options); f.extend(o, options);
delete o.fixLng; /* passed in each time */
// create namespace object if namespace is passed in as string // create namespace object if namespace is passed in as string
if (typeof o.ns == 'string') { if (typeof o.ns == 'string') {
@ -595,7 +604,7 @@
if (!o.lng) o.lng = f.detectLanguage(); if (!o.lng) o.lng = f.detectLanguage();
if (o.lng) { if (o.lng) {
// set cookie with lng set (as detectLanguage will set cookie on need) // set cookie with lng set (as detectLanguage will set cookie on need)
if (o.useCookie) f.cookie.create(o.cookieName, o.lng, o.cookieExpirationTime); if (o.useCookie) f.cookie.create(o.cookieName, o.lng, o.cookieExpirationTime, o.cookieDomain);
} else { } else {
o.lng = o.fallbackLng; o.lng = o.fallbackLng;
if (o.useCookie) f.cookie.remove(o.cookieName); if (o.useCookie) f.cookie.remove(o.cookieName);
@ -605,6 +614,16 @@
currentLng = languages[0]; currentLng = languages[0];
f.log('currentLng set to: ' + currentLng); f.log('currentLng set to: ' + currentLng);
var lngTranslate = translate;
if (options.fixLng) {
lngTranslate = function(key, options) {
options = options || {};
options.lng = options.lng || lngTranslate.lng;
return translate(key, options);
};
lngTranslate.lng = currentLng;
}
pluralExtensions.setCurrentLng(currentLng); pluralExtensions.setCurrentLng(currentLng);
// add JQuery extensions // add JQuery extensions
@ -619,8 +638,9 @@
// return immidiatly if res are passed in // return immidiatly if res are passed in
if (o.resStore) { if (o.resStore) {
resStore = o.resStore; resStore = o.resStore;
if (cb) cb(translate); initialized = true;
if (deferred) deferred.resolve(); if (cb) cb(lngTranslate);
if (deferred) deferred.resolve(lngTranslate);
if (deferred) return deferred.promise(); if (deferred) return deferred.promise();
return; return;
} }
@ -640,9 +660,10 @@
// else load them // else load them
i18n.sync.load(lngsToLoad, o, function(err, store) { i18n.sync.load(lngsToLoad, o, function(err, store) {
resStore = store; resStore = store;
initialized = true;
if (cb) cb(translate); if (cb) cb(lngTranslate);
if (deferred) deferred.resolve(); if (deferred) deferred.resolve(lngTranslate);
}); });
if (deferred) return deferred.promise(); if (deferred) return deferred.promise();
@ -745,8 +766,13 @@
} }
} }
function setLng(lng, cb) { function setLng(lng, options, cb) {
return init({lng: lng}, cb); if (typeof options === 'function') {
cb = options;
options = {};
}
options.lng = lng;
return init(options, cb);
} }
function lng() { function lng() {
@ -787,6 +813,7 @@
function localize(ele, options) { function localize(ele, options) {
var key = ele.attr(o.selectorAttr); var key = ele.attr(o.selectorAttr);
if (!key && typeof key !== 'undefined' && key !== false) key = ele.text() || ele.val();
if (!key) return; if (!key) return;
var target = ele var target = ele
@ -905,17 +932,25 @@
function exists(key, options) { function exists(key, options) {
options = options || {}; options = options || {};
var notFound = options.defaultValue || key var notFound = _getDefaultValue(key, options)
, found = _find(key, options); , found = _find(key, options);
return found !== undefined || found === notFound; return found !== undefined || found === notFound;
} }
function translate(key, options) { function translate(key, options) {
if (!initialized) {
f.log('i18next not finished initialization. you might have called t function before loading resources finished.')
return options.defaultValue || '';
};
replacementCounter = 0; replacementCounter = 0;
return _translate.apply(null, arguments); return _translate.apply(null, arguments);
} }
function _getDefaultValue(key, options) {
return (options.defaultValue !== undefined) ? options.defaultValue : key;
}
function _injectSprintfProcessor() { function _injectSprintfProcessor() {
var values = []; var values = [];
@ -931,16 +966,34 @@
}; };
} }
function _translate(key, options) { function _translate(potentialKeys, options) {
if (typeof options == 'string') { if (typeof options == 'string') {
// mh: gettext like sprintf syntax found, automatically create sprintf processor if (o.shortcutFunction === 'sprintf') {
options = _injectSprintfProcessor.apply(null, arguments); // mh: gettext like sprintf syntax found, automatically create sprintf processor
options = _injectSprintfProcessor.apply(null, arguments);
} else if (o.shortcutFunction === 'defaultValue') {
options = {
defaultValue: options
}
}
} else { } else {
options = options || {}; options = options || {};
} }
var notFound = options.defaultValue || key if (typeof potentialKeys == 'string') {
potentialKeys = [potentialKeys];
}
var key = null;
for (var i = 0; i < potentialKeys.length; i++) {
key = potentialKeys[i];
if (exists(key)) {
break;
}
}
var notFound = _getDefaultValue(key, options)
, found = _find(key, options) , found = _find(key, options)
, lngs = options.lng ? f.toLanguages(options.lng) : languages , lngs = options.lng ? f.toLanguages(options.lng) : languages
, ns = options.ns || o.ns.defaultNs , ns = options.ns || o.ns.defaultNs
@ -983,7 +1036,7 @@
notFound = applyReuse(notFound, options); notFound = applyReuse(notFound, options);
if (postProcessor && postProcessors[postProcessor]) { if (postProcessor && postProcessors[postProcessor]) {
var val = options.defaultValue || key; var val = _getDefaultValue(key, options);
found = postProcessors[postProcessor](val, key, options); found = postProcessors[postProcessor](val, key, options);
} }
} }
@ -995,7 +1048,7 @@
options = options || {}; options = options || {};
var optionWithoutCount, translated var optionWithoutCount, translated
, notFound = options.defaultValue || key , notFound = _getDefaultValue(key, options)
, lngs = languages; , lngs = languages;
if (!resStore) { return notFound; } // no resStore to translate from if (!resStore) { return notFound; } // no resStore to translate from
@ -1085,12 +1138,11 @@
value = 'key \'' + ns + ':' + key + ' (' + l + ')\' ' + value = 'key \'' + ns + ':' + key + ' (' + l + ')\' ' +
'returned a object instead of string.'; 'returned a object instead of string.';
f.log(value); f.log(value);
} else { } else if (typeof value !== 'number') {
var copy = {}; // apply child translation on a copy var copy = {}; // apply child translation on a copy
for (var m in value) { f.each(value, function(m) {
// apply translation on childs
copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options); copy[m] = _translate(ns + o.nsseparator + key + o.keyseparator + m, options);
} });
value = copy; value = copy;
} }
} }
@ -2607,9 +2659,4 @@
i18n.addPostProcessor = addPostProcessor; i18n.addPostProcessor = addPostProcessor;
i18n.options = o; i18n.options = o;
$.i18n = i18n; })();
$.t = i18n.t;
return i18n;
}));

View File

@ -13,6 +13,7 @@
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
**/ **/
'use strict';
requirejs.config({ requirejs.config({
baseUrl: 'static', baseUrl: 'static',
urlArgs: '_=' + (new Date()).getTime(), urlArgs: '_=' + (new Date()).getTime(),
@ -23,7 +24,7 @@ requirejs.config({
'jquery-timeout': 'js/libs/jquery.timeout', 'jquery-timeout': 'js/libs/jquery.timeout',
'jquery-ui': 'js/libs/jquery-ui-1.10.2.custom', 'jquery-ui': 'js/libs/jquery-ui-1.10.2.custom',
'jquery-autoNumeric': 'js/libs/autoNumeric', 'jquery-autoNumeric': 'js/libs/autoNumeric',
i18next: 'js/libs/i18next.amd.withJQuery-1.6.3', i18next: 'js/libs/i18next-1.7.1',
utils: 'js/utils', utils: 'js/utils',
underscore: 'js/libs/lodash', underscore: 'js/libs/lodash',
backbone: 'js/libs/backbone', backbone: 'js/libs/backbone',
@ -60,7 +61,10 @@ requirejs.config({
deps: ['jquery'] deps: ['jquery']
}, },
i18next: { i18next: {
deps: ['jquery'] deps: ['text!i18n/translation.json', 'jquery'],
init: function(translation, $) {
$.i18n.init({resStore: JSON.parse(translation)});
}
}, },
'jquery-checkbox': { 'jquery-checkbox': {
deps: ['jquery'] deps: ['jquery']
@ -78,13 +82,8 @@ requirejs.config({
}); });
require([ require([
'text!i18n/translation.json',
'jquery', 'underscore', 'backbone', 'stickit', 'deepModel', 'coccyx', 'i18next', 'bootstrap', 'retina', 'jquery-checkbox', 'jquery-timeout', 'jquery-ui', 'jquery-autoNumeric', 'jquery', 'underscore', 'backbone', 'stickit', 'deepModel', 'coccyx', 'i18next', 'bootstrap', 'retina', 'jquery-checkbox', 'jquery-timeout', 'jquery-ui', 'jquery-autoNumeric',
'app' 'app'
], function(translation) { ], function() {
'use strict'; require('app').initialize();
var app = _.last(arguments);
$.i18n.init({
resStore: JSON.parse(translation)
}).always(app.initialize);
}); });

View File

@ -25,7 +25,9 @@ function(commonViews, models, capacityPageTemplate) {
var CapacityPage = commonViews.Page.extend({ var CapacityPage = commonViews.Page.extend({
navbarActiveElement: 'support', navbarActiveElement: 'support',
breadcrumbsPath: [['home', '#'], ['support', '#support'], 'capacity'], breadcrumbsPath: [['home', '#'], ['support', '#support'], 'capacity'],
title: $.t('capacity_page.title'), title: function() {
return $.t('capacity_page.title');
},
updateInterval: 2000, updateInterval: 2000,
template: _.template(capacityPageTemplate), template: _.template(capacityPageTemplate),
events: { events: {

View File

@ -30,7 +30,9 @@ function(models, utils, commonViews, dialogViews, clustersPageTemplate, clusterT
ClustersPage = commonViews.Page.extend({ ClustersPage = commonViews.Page.extend({
navbarActiveElement: 'clusters', navbarActiveElement: 'clusters',
breadcrumbsPath: [['home', '#'], 'environments'], breadcrumbsPath: [['home', '#'], 'environments'],
title: $.t('clusters_page.title'), title: function() {
return $.t('clusters_page.title');
},
template: _.template(clustersPageTemplate), template: _.template(clustersPageTemplate),
render: function() { render: function() {
this.$el.html(this.template({clusters: this.collection})).i18n(); this.$el.html(this.template({clusters: this.collection})).i18n();

View File

@ -255,7 +255,7 @@ function(utils, models, dialogViews, navbarTemplate, nodesStatsTemplate, notific
], ],
setLocale: function(e) { setLocale: function(e) {
var newLocale = _.find(this.locales, {locale: $(e.currentTarget).data('locale')}); var newLocale = _.find(this.locales, {locale: $(e.currentTarget).data('locale')});
$.i18n.setLng(newLocale.locale); $.i18n.setLng(newLocale.locale, {});
window.location.reload(); window.location.reload();
}, },
getCurrentLocale: function() { getCurrentLocale: function() {
@ -264,7 +264,7 @@ function(utils, models, dialogViews, navbarTemplate, nodesStatsTemplate, notific
setDefaultLocale: function() { setDefaultLocale: function() {
var currentLocale = this.getCurrentLocale(); var currentLocale = this.getCurrentLocale();
if (!currentLocale) { if (!currentLocale) {
$.i18n.setLng(this.locales[0].locale); $.i18n.setLng(this.locales[0].locale, {});
} }
}, },
initialize: function(options) { initialize: function(options) {

View File

@ -27,7 +27,9 @@ function(utils, models, commonViews, dialogViews, notificationsListTemplate) {
var NotificationsPage = commonViews.Page.extend({ var NotificationsPage = commonViews.Page.extend({
navbarActiveElement: null, navbarActiveElement: null,
breadcrumbsPath: [['home', '#'], 'notifications'], breadcrumbsPath: [['home', '#'], 'notifications'],
title: $.t('notifications_page.title'), title: function() {
return $.t('notifications_page.title');
},
template: _.template(notificationsListTemplate), template: _.template(notificationsListTemplate),
templateHelpers: _.pick(utils, 'urlify'), templateHelpers: _.pick(utils, 'urlify'),
events: { events: {

View File

@ -29,7 +29,9 @@ function(utils, commonViews, dialogViews, releasesListTemplate, releaseTemplate)
ReleasesPage = commonViews.Page.extend({ ReleasesPage = commonViews.Page.extend({
navbarActiveElement: 'releases', navbarActiveElement: 'releases',
breadcrumbsPath: [['home', '#'], 'releases'], breadcrumbsPath: [['home', '#'], 'releases'],
title: $.t('release_page.title'), title: function() {
return $.t('release_page.title');
},
updateInterval: 5000, updateInterval: 5000,
template: _.template(releasesListTemplate), template: _.template(releasesListTemplate),
scheduleUpdate: function() { scheduleUpdate: function() {

View File

@ -25,7 +25,9 @@ function(commonViews, models, supportPageTemplate) {
var SupportPage = commonViews.Page.extend({ var SupportPage = commonViews.Page.extend({
navbarActiveElement: 'support', navbarActiveElement: 'support',
breadcrumbsPath: [['home', '#'], 'support'], breadcrumbsPath: [['home', '#'], 'support'],
title: $.t('support_page.title'), title: function() {
return $.t('support_page.title');
},
updateInterval: 2000, updateInterval: 2000,
template: _.template(supportPageTemplate), template: _.template(supportPageTemplate),
events: { events: {

View File

@ -15,6 +15,10 @@
**/ **/
var baseUrl = 'http://127.0.0.1:5544/'; var baseUrl = 'http://127.0.0.1:5544/';
casper.on('page.error', function(msg) {
casper.echo(msg, 'ERROR');
});
casper.loadPage = function(page) { casper.loadPage = function(page) {
return this.thenOpen(baseUrl + page).waitWhileSelector('#content > .loading'); return this.thenOpen(baseUrl + page).waitWhileSelector('#content > .loading');
} }