
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($) {
window.I18N = function(options) {
this.options = $.extend({
bundle: undefined,
url: undefined,
caching: true,
supportedLocales: [],
defaultLocale: navigator.language ? navigator.language : navigator.userLanguage,
onKeyNotFound: function(key, locale) {
return '[' + key + ']';
},
onKeyFound: function(elem, key, value) {
elem.html(value);
}
}, 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.logger = new Logger('I18N');
this._bundles = {};
this.options.defaultLocale = this._normalize(this.options.defaultLocale);
for (var i in this.options.supportedLocales) {
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) {
this.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';
this.logger.debug('_loadBundleAndRun: getting ' + path);
$.ajax({
url: path,
dataType: 'json',
cache: this.options.caching,
ifModified: this.options.caching
}).success(function(data) {
self.logger.debug('_loadBundleAndRun: merging to {loc}', {loc: locale});
$.extend(true, self._bundles[locale], data);
self._loadBundleAndRun(locale, tries, index + 1, func);
});
}
},
_isSupported: function(locale) {
return this.options.supportedLocales.length == 0 || this.options.supportedLocales.indexOf(locale) != -1;
},
_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) {
this.logger.debug('withBundle - loading bundles {tries} for locale {loc}', {tries: tries, loc: locale});
this._loadBundleAndRun(locale, tries, 0, func);
}
}
},
localize: function(expr, locale) {
this.logger.debug('localize {expr} to {loc}', {expr: expr, loc: locale});
var self = this;
this.withBundle(function(loc) {
$(expr).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);
})
}, 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 {
this.logger.debug('value {key} not found in bundle {loc}', {key: key, loc: locale});
return this.options.onKeyNotFound(key, locale);
}
} else {
this.logger.debug('Bundle {loc} not found', {loc: locale});
return this.options.onKeyNotFound(key, locale);
}
}
};
})(jQuery);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy