2008-10-03 02:46:07 +02:00
|
|
|
if(typeof(ss) == 'undefined') ss = {};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lightweight clientside i18n implementation.
|
|
|
|
* Caution: Only available after DOM loaded because we need to detect the language
|
|
|
|
*
|
2012-10-05 06:00:39 +02:00
|
|
|
* For non-i18n stub implementation, see framework/javascript/i18nx.js
|
2009-03-10 23:08:52 +01:00
|
|
|
*
|
2008-10-03 02:46:07 +02:00
|
|
|
* Based on jQuery i18n plugin: 1.0.0 Feb-10-2008
|
2009-03-10 23:08:52 +01:00
|
|
|
*
|
2008-10-03 02:46:07 +02:00
|
|
|
* Dual licensed under the MIT and GPL licenses:
|
|
|
|
* http://www.opensource.org/licenses/mit-license.php
|
|
|
|
* http://www.gnu.org/licenses/gpl.html
|
|
|
|
*
|
|
|
|
* Based on 'javascript i18n that almost doesn't suck' by markos
|
|
|
|
* http://markos.gaivo.net/blog/?p=100
|
2007-10-25 04:47:45 +02:00
|
|
|
*/
|
2008-10-03 02:46:07 +02:00
|
|
|
ss.i18n = {
|
2007-10-25 04:47:45 +02:00
|
|
|
|
2008-10-03 02:46:07 +02:00
|
|
|
currentLocale: null,
|
|
|
|
|
|
|
|
defaultLocale: 'en_US',
|
|
|
|
|
|
|
|
lang: {},
|
2012-08-06 10:59:18 +02:00
|
|
|
|
|
|
|
inited: false,
|
2008-10-03 02:46:07 +02:00
|
|
|
|
|
|
|
init: function() {
|
2012-08-06 10:59:18 +02:00
|
|
|
if(this.inited) return;
|
|
|
|
|
2008-10-03 02:46:07 +02:00
|
|
|
this.currentLocale = this.detectLocale();
|
2012-08-06 10:59:18 +02:00
|
|
|
this.inited = true;
|
2008-10-03 02:46:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set_locale()
|
|
|
|
* Set locale in long format, e.g. "de_AT" for Austrian German.
|
|
|
|
* @param string locale
|
|
|
|
*/
|
|
|
|
setLocale: function(locale) {
|
|
|
|
this.currentLocale = locale;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getLocale()
|
|
|
|
* Get locale in long format. Falls back to i18n.defaut_locale.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
getLocale: function() {
|
|
|
|
return (this.currentLocale) ? this.currentLocale : this.defaultLocale;
|
|
|
|
},
|
2007-10-25 04:47:45 +02:00
|
|
|
|
2008-10-03 02:46:07 +02:00
|
|
|
/**
|
|
|
|
* _()
|
|
|
|
* The actual translation function. Looks the given string up in the
|
|
|
|
* dictionary and returns the translation if one exists. If a translation
|
|
|
|
* is not found, returns the original word
|
|
|
|
*
|
2009-03-10 23:08:52 +01:00
|
|
|
* @param string entity A "long" locale format, e.g. "de_DE" (Required)
|
|
|
|
* @param string fallbackString (Required)
|
2008-10-03 02:46:07 +02:00
|
|
|
* @param int priority (not used)
|
|
|
|
* @param string context Give translators context for the string
|
|
|
|
* @return string : Translated word
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
_t: function (entity, fallbackString, priority, context) {
|
2012-08-06 10:59:18 +02:00
|
|
|
this.init();
|
|
|
|
|
2013-10-23 17:53:08 +02:00
|
|
|
var langName = this.getLocale().replace(/_[\w]+/i, '');
|
2013-12-01 17:48:59 +01:00
|
|
|
var defaultlangName = this.defaultLocale.replace(/_[\w]+/i, '');
|
|
|
|
|
2008-10-03 02:46:07 +02:00
|
|
|
if (this.lang && this.lang[this.getLocale()] && this.lang[this.getLocale()][entity]) {
|
|
|
|
return this.lang[this.getLocale()][entity];
|
2013-10-23 17:53:08 +02:00
|
|
|
} else if (this.lang && this.lang[langName] && this.lang[langName][entity]) {
|
|
|
|
return this.lang[langName][entity];
|
2008-10-03 02:46:07 +02:00
|
|
|
} else if (this.lang && this.lang[this.defaultLocale] && this.lang[this.defaultLocale][entity]) {
|
|
|
|
return this.lang[this.defaultLocale][entity];
|
2013-12-01 17:48:59 +01:00
|
|
|
} else if (this.lang && this.lang[defaultlangName] && this.lang[defaultlangName][entity]) {
|
|
|
|
return this.lang[defaultlangName][entity];
|
2008-10-03 02:46:07 +02:00
|
|
|
} else if(fallbackString) {
|
|
|
|
return fallbackString;
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-10-29 14:20:38 +01:00
|
|
|
/**
|
|
|
|
* Add entities to a dictionary. If a dictionary doesn't
|
|
|
|
* exist for this locale, its automatically created.
|
|
|
|
* Existing entities are overwritten.
|
|
|
|
*
|
|
|
|
* @param string locale
|
|
|
|
* @param Object dict
|
|
|
|
*/
|
2008-10-03 02:46:07 +02:00
|
|
|
addDictionary: function(locale, dict) {
|
2008-10-29 14:20:38 +01:00
|
|
|
if(!this.lang[locale]) this.lang[locale] = {};
|
|
|
|
for(entity in dict) {
|
|
|
|
this.lang[locale][entity] = dict[entity];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get dictionary for a specific locale.
|
|
|
|
*
|
|
|
|
* @param string locale
|
|
|
|
*/
|
|
|
|
getDictionary: function(locale) {
|
|
|
|
return this.lang[locale];
|
2008-10-03 02:46:07 +02:00
|
|
|
},
|
2007-10-25 04:47:45 +02:00
|
|
|
|
2008-10-03 02:46:07 +02:00
|
|
|
/**
|
|
|
|
* stripStr()
|
|
|
|
*
|
|
|
|
* @param string str : The string to strip
|
|
|
|
* @return string result : Stripped string
|
|
|
|
*
|
|
|
|
*/
|
2012-12-08 12:20:20 +01:00
|
|
|
stripStr: function(str) {
|
2008-10-03 02:46:07 +02:00
|
|
|
return str.replace(/^\s*/, "").replace(/\s*$/, "");
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* stripStrML()
|
|
|
|
*
|
|
|
|
* @param string str : The multi-line string to strip
|
|
|
|
* @return string result : Stripped string
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
stripStrML: function(str) {
|
|
|
|
// Split because m flag doesn't exist before JS1.5 and we need to
|
|
|
|
// strip newlines anyway
|
|
|
|
var parts = str.split('\n');
|
|
|
|
for (var i=0; i<parts.length; i++)
|
|
|
|
parts[i] = stripStr(parts[i]);
|
|
|
|
|
|
|
|
// Don't join with empty strings, because it "concats" words
|
|
|
|
// And strip again
|
|
|
|
return stripStr(parts.join(" "));
|
|
|
|
},
|
|
|
|
|
2014-06-01 15:15:52 +02:00
|
|
|
/**
|
|
|
|
* Substitutes %s with parameters
|
|
|
|
* given in list. %%s is used to escape %s.
|
|
|
|
*
|
|
|
|
* @param string S : The string to perform the substitutions on.
|
|
|
|
* @return string The new string with substitutions made
|
|
|
|
*/
|
2008-10-10 23:06:59 +02:00
|
|
|
sprintf: function(S) {
|
2008-10-03 02:46:07 +02:00
|
|
|
if (arguments.length == 1) return S;
|
|
|
|
|
2014-06-01 15:15:52 +02:00
|
|
|
var args = [],
|
|
|
|
len = arguments.length,
|
|
|
|
index = 0,
|
|
|
|
regx = new RegExp('(.?)(%s)', 'g'),
|
|
|
|
result;
|
|
|
|
|
|
|
|
for (var i=1; i<len; ++i) {
|
2008-10-03 02:46:07 +02:00
|
|
|
args.push(arguments[i]);
|
|
|
|
};
|
|
|
|
|
2014-06-01 15:15:52 +02:00
|
|
|
result = S.replace(regx, function(match, subMatch1, subMatch2, offset, string){
|
|
|
|
if (subMatch1 == '%') return match; // skip %%s
|
|
|
|
return subMatch1 + args[index++];
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Substitutes variables with a list of injections.
|
|
|
|
*
|
|
|
|
* @param string S : The string to perform the substitutions on.
|
|
|
|
* @param object map : An object with the substitions map e.g. {var: value}
|
|
|
|
* @return string The new string with substitutions made
|
|
|
|
*/
|
|
|
|
inject: function(S, map) {
|
|
|
|
var regx = new RegExp("\{([A-Za-z0-9_]*)\}", "g"),
|
|
|
|
result;
|
|
|
|
|
|
|
|
result = S.replace(regx, function(match, key, offset, string){
|
|
|
|
return (map[key]) ? map[key] : match;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
2008-10-03 02:46:07 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect document language settings by looking at <meta> tags.
|
|
|
|
* If no match is found, returns this.defaultLocale.
|
|
|
|
*
|
|
|
|
* @todo get by <html lang=''> - needs modification of SSViewer
|
|
|
|
*
|
|
|
|
* @return string Locale in mixed lowercase/uppercase format suitable
|
|
|
|
* for usage in ss.i18n.lang arrays (e.g. 'en_US').
|
|
|
|
*/
|
|
|
|
detectLocale: function() {
|
|
|
|
var rawLocale;
|
|
|
|
var detectedLocale;
|
2012-08-06 10:59:18 +02:00
|
|
|
|
|
|
|
// get by container tag
|
2015-12-22 15:12:50 +01:00
|
|
|
rawLocale = jQuery('html').attr('lang') || jQuery('body').attr('lang');
|
|
|
|
|
2008-10-03 02:46:07 +02:00
|
|
|
// get by meta
|
2012-08-06 10:59:18 +02:00
|
|
|
if(!rawLocale) {
|
|
|
|
var metas = document.getElementsByTagName('meta');
|
|
|
|
for(var i=0; i<metas.length; i++) {
|
|
|
|
if(metas[i].attributes['http-equiv'] && metas[i].attributes['http-equiv'].nodeValue.toLowerCase() == 'content-language') {
|
|
|
|
rawLocale = metas[i].attributes['content'].nodeValue;
|
|
|
|
}
|
2008-10-03 02:46:07 +02:00
|
|
|
}
|
2008-10-09 19:58:56 +02:00
|
|
|
}
|
2012-08-06 10:59:18 +02:00
|
|
|
|
2008-10-03 02:46:07 +02:00
|
|
|
// fallback to default locale
|
|
|
|
if(!rawLocale) rawLocale = this.defaultLocale;
|
|
|
|
|
|
|
|
var rawLocaleParts = rawLocale.match(/([^-|_]*)[-|_](.*)/);
|
|
|
|
// get locale (e.g. 'en_US') from common name (e.g. 'en')
|
|
|
|
// by looking at ss.i18n.lang tables
|
|
|
|
if(rawLocale.length == 2) {
|
|
|
|
for(compareLocale in ss.i18n.lang) {
|
|
|
|
if(compareLocale.substr(0,2).toLowerCase() == rawLocale.toLowerCase()) {
|
|
|
|
detectedLocale = compareLocale;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(rawLocaleParts) {
|
|
|
|
detectedLocale = rawLocaleParts[1].toLowerCase() + '_' + rawLocaleParts[2].toUpperCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
return detectedLocale;
|
2008-10-09 19:58:56 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attach an event listener to the given object.
|
|
|
|
* Modeled after behaviour.js, but externalized
|
|
|
|
* to keep the i18n library standalone for now.
|
|
|
|
*/
|
|
|
|
addEvent: function(obj, evType, fn, useCapture){
|
|
|
|
if (obj.addEventListener){
|
|
|
|
obj.addEventListener(evType, fn, useCapture);
|
|
|
|
return true;
|
|
|
|
} else if (obj.attachEvent){
|
|
|
|
var r = obj.attachEvent("on"+evType, fn);
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
alert("Handler could not be attached");
|
|
|
|
}
|
2008-10-03 02:46:07 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-10-09 19:58:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
ss.i18n.addEvent(window, "load", function() {
|
2008-10-03 02:46:07 +02:00
|
|
|
ss.i18n.init();
|
2012-12-08 12:20:20 +01:00
|
|
|
});
|