com.ovea.i18n.jquery.jquery-i18n.js Maven / Gradle / Ivy
/*
* Copyright (C) 2011 Ovea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
if (window.I18N == undefined) {
(function($) {
var logger = new Logger('I18N');
window.I18N = function(options) {
this.options = $.extend({
bundle: undefined,
url: undefined,
supportedLocales: [],
defaultLocale: navigator.language ? navigator.language : navigator.userLanguage,
onKeyNotFound: function(key, locale) {
return '[' + key + ']';
},
onKeyFound: function(elem, key, value) {
elem.html(value);
},
onLocalized: $.noop(),
onError: $.noop(),
cache: true
}, options);
if (this.options.bundle == undefined) {
throw new Error('[I18N] "bundle" option is required (bundle name)')
}
if (this.options.url == undefined) {
throw new Error('[I18N] "url" option is required (where to get localization files)')
}
this._bundles = {};
this.options.defaultLocale = this._normalize(this.options.defaultLocale);
for (var i = 0; i < this.options.supportedLocales.length; i++) {
this.options.supportedLocales[i] = this._normalize(this.options.supportedLocales[i]);
}
};
window.I18N.prototype = {
_loadBundleAndRun: function(locale, tries, index, func) {
var self = this;
if (tries.length <= index) {
logger.debug('_loadBundleAndRun: calling callback');
func.call(null, locale, this._bundles[locale]);
} else {
var path = this.options.url + '/' + this.options.bundle;
if (tries[index].length > 0)
path += '_' + tries[index];
path += '.json';
logger.debug('_loadBundleAndRun: getting ' + path);
var jqxhr = $.ajax({
url: path,
dataType: 'json',
cache: this.options.cache
}).success(
function(data) {
logger.debug('_loadBundleAndRun: merging to ' + locale);
$.extend(true, self._bundles[locale], data);
self._loadBundleAndRun(locale, tries, index + 1, func);
}).error(this.options.onError || $.noop());
}
},
_isSupported: function(locale) {
if (this.options.supportedLocales.length === 0) {
return true;
}
for (var i = 0; i < this.options.supportedLocales.length; i++) {
if (this.options.supportedLocales[i] == locale) {
return true;
}
}
return false;
},
_normalize: function(locale) {
if (locale) {
locale = locale.replace(/-/, '_').toLowerCase();
if (locale.length > 3) {
locale = locale.substring(0, 3) + locale.substring(3).toUpperCase();
}
return locale;
} else {
return this.options.defaultLocale;
}
},
withBundle: function(func, locale) {
locale = this._normalize(locale);
if (this._bundles[locale]) {
func.call(null, locale, this._bundles[locale]);
} else {
this._bundles[locale] = {};
var tries = [];
// first load base (root bundle)
if (this._isSupported('')) {
tries.push('');
}
// load per language (fr, en, ...)
if (locale.length >= 2) {
var v = locale.substring(0, 2);
if (this._isSupported(v)) {
tries.push(v);
}
}
// load per region (CA, US, ...)
if (locale.length >= 5) {
var v = locale.substring(0, 5);
if (this._isSupported(v)) {
tries.push(v);
}
}
// aggregate possibilities
if (tries.length) {
logger.debug('withBundle - loading bundles ' + tries + ' for locale ' + locale);
this._loadBundleAndRun(locale, tries, 0, func);
}
}
},
localize: function(expr, locale) {
logger.debug('Localizing to locale ' + locale);
var self = this;
this.withBundle(function(loc) {
var e = typeof expr == 'string' ? $(expr) : expr;
e.each(function() {
var elem = $(this);
var key = elem.attr("rel").match(/localize\[(.*?)\]/)[1];
var value = self.value(key, loc);
self.options.onKeyFound(elem, key, value);
});
if (self.options.onLocalized) {
self.options.onLocalized.call(undefined);
}
}, locale);
},
value: function(key, locale) {
locale = this._normalize(locale);
if (this._bundles[locale]) {
var value = this._bundles[locale];
var keys = key.split(/\./);
while (keys.length && value) {
value = value[keys.shift()];
}
if (value) {
return value;
} else {
logger.debug('value ' + key + ' not found in bundle ' + locale);
return this.options.onKeyNotFound(key, locale);
}
} else {
logger.debug('Bundle ' + locale + ' not found');
return this.options.onKeyNotFound(key, locale);
}
}
};
})(jQuery);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy