jquery.i18n.properties.min.js
(function(h){function v(a){a.debug&&(f("callbackIfComplete()"),f("totalFiles: "+a.totalFiles),f("filesLoaded: "+a.filesLoaded));a.async&&a.filesLoaded===a.totalFiles&&a.callback&&a.callback()}function n(a,c){c.debug&&f("loadAndParseFiles");null!==a&&0<a.length?w(a[0],c,function(){a.shift();n(a,c)}):v(c)}function w(a,c,m){c.debug&&(f("loadAndParseFile('"+a+"')"),f("totalFiles: "+c.totalFiles),f("filesLoaded: "+c.filesLoaded));null!==a&&"undefined"!==typeof a&&h.ajax({url:a,async:c.async,cache:c.cache,
dataType:"text",success:function(e,b){c.debug&&(f("Succeeded in downloading "+a+"."),f(e));x(e,c);m()},error:function(e,b,d){c.debug&&f("Failed to download or parse "+a+". errorThrown: "+d);404===e.status&&--c.totalFiles;m()}})}function x(a,c){for(var m="",e=a.split(/\n/),b=/(\{\d+})/g,d=/\{(\d+)}/g,k=/(\\u.{4})/ig,f=0,n=e.length;f<n;f++){var l=e[f],l=l.trim();if(0<l.length&&"#"!=l.match("^#")){var p=l.split("=");if(0<p.length){for(var l=decodeURI(p[0]).trim(),g=1==p.length?"":p[1];-1!=g.search(/\\$/);)g=
g.substring(0,g.length-1),g+=e[++f].trimRight();for(var q=2;q<p.length;q++)g+="="+p[q];g=g.trim();if("map"==c.mode||"both"==c.mode)(p=g.match(k))&&p.forEach(function(a){g=g.replace(a,y(a))}),c.namespace?h.i18n.map[c.namespace][l]=g:h.i18n.map[l]=g;if("vars"==c.mode||"both"==c.mode)if(g=g.replace(/"/g,'\\"'),z(l),b.test(g)){var u=!0,r="",t=[];g.split(b).forEach(function(a){!b.test(a)||0!==t.length&&-1!=t.indexOf(a)||(u||(r+=","),r+=a.replace(d,"v$1"),t.push(a),u=!1)});m+=l+"=function("+r+"){";l='"'+
g.replace(d,'"+v$1+"')+'"';m+="return "+l+";};"}else m+=l+'="'+g+'";'}}}eval(m);c.filesLoaded+=1}function z(a){if(/\./.test(a)){var c="";a=a.split(/\./);for(var m=0,e=a.length;m<e;m++){var b=a[m];0<m&&(c+=".");c+=b;eval("typeof "+c+' == "undefined"')&&eval(c+"={};")}}}function y(a){var c=[];a=parseInt(a.substr(2),16);0<=a&&a<Math.pow(2,16)&&c.push(a);return c.reduce(function(a,c){return a+String.fromCharCode(c)},"")}h.i18n={};h.i18n.map={};var f=function(a){window.console&&console.log("i18n::"+a)};
h.i18n.properties=function(a){a=h.extend({name:"Messages",language:"",path:"",namespace:null,mode:"vars",cache:!1,debug:!1,encoding:"UTF-8",async:!1,callback:null},a);a.namespace&&"string"==typeof a.namespace&&(a.namespace.match(/^[a-z]*$/)?h.i18n.map[a.namespace]={}:(f("Namespaces can only be lower case letters, a - z"),a.namespace=null));a.path.match(/\/$/)||(a.path+="/");a.language=this.normaliseLanguageCode(a);var c=a.name&&a.name.constructor===Array?a.name:[a.name];a.totalFiles=2*c.length+(5<=
a.language.length?c.length:0);a.debug&&f("totalFiles: "+a.totalFiles);a.filesLoaded=0;c.forEach(function(c){var e=a.path+c+".properties";var b=a.language.substring(0,2);b=a.path+c+"_"+b+".properties";if(5<=a.language.length){var d=a.language.substring(0,5);c=a.path+c+"_"+d+".properties";e=[e,b,c]}else e=[e,b];n(e,a)});a.callback&&!a.async&&a.callback()};h.i18n.prop=function(a){var c=[].slice.call(arguments);if(2==c.length)if(h.isArray(c[1]))var f=c[1];else if("object"===typeof c[1]){var e=c[1].namespace;
var b=c[1].replacements;c.splice(-1,1);b&&Array.prototype.push.apply(c,b)}b=e?h.i18n.map[e][a]:h.i18n.map[a];if(null===b)return"["+(e?e+"#"+a:a)+"]";var d;if("string"==typeof b){for(d=0;-1!=(d=b.indexOf("\\",d));)b="t"==b.charAt(d+1)?b.substring(0,d)+"\t"+b.substring(d++ +2):"r"==b.charAt(d+1)?b.substring(0,d)+"\r"+b.substring(d++ +2):"n"==b.charAt(d+1)?b.substring(0,d)+"\n"+b.substring(d++ +2):"f"==b.charAt(d+1)?b.substring(0,d)+"\f"+b.substring(d++ +2):"\\"==b.charAt(d+1)?b.substring(0,d)+"\\"+
b.substring(d++ +2):b.substring(0,d)+b.substring(d+1);var k=[];for(d=0;d<b.length;)if("'"==b.charAt(d))if(d==b.length-1)b=b.substring(0,d);else if("'"==b.charAt(d+1))b=b.substring(0,d)+b.substring(++d);else{for(e=d+2;-1!=(e=b.indexOf("'",e));)if(e==b.length-1||"'"!=b.charAt(e+1)){b=b.substring(0,d)+b.substring(d+1,e)+b.substring(e+1);d=e-1;break}else b=b.substring(0,e)+b.substring(++e);-1==e&&(b=b.substring(0,d)+b.substring(d+1))}else if("{"==b.charAt(d))if(e=b.indexOf("}",d+1),-1==e)d++;else{var n=
parseInt(b.substring(d+1,e));!isNaN(n)&&0<=n?(d=b.substring(0,d),""!==d&&k.push(d),k.push(n),d=0,b=b.substring(e+1)):d=e+1}else d++;""!==b&&k.push(b);b=k;settings.namespace?h.i18n.map[settings.namespace][a]=k:h.i18n.map[a]=k}if(0===b.length)return"";if(1==b.length&&"string"==typeof b[0])return b[0];k="";d=0;for(e=b.length;d<e;d++)k="string"==typeof b[d]?k+b[d]:f&&b[d]<f.length?k+f[b[d]]:!f&&b[d]+1<c.length?k+c[b[d]+1]:k+("{"+b[d]+"}");return k};h.i18n.normaliseLanguageCode=function(a){var c=a.language;
if(!c||2>c.length)a.debug&&f("No language supplied. Pulling it from the browser ..."),c=navigator.languages&&0<navigator.languages.length?navigator.languages[0]:navigator.language||navigator.userLanguage||"en",a.debug&&f("Language from browser: "+c);c=c.toLowerCase();c=c.replace(/-/,"_");3<c.length&&(c=c.substring(0,3)+c.substring(3).toUpperCase());return c}})(jQuery);
jquery.i18n.properties.js
(function ($) {
$.i18n = {};
$.i18n.map = {};
var debug = function (message) {
window.console && console.log('i18n::' + message);
};
$.i18n.properties = function (settings) {
var defaults = {
name: 'Messages',
language: '',
path: '',
namespace: null,
mode: 'vars',
cache: false,
debug: false,
encoding: 'UTF-8',
async: false,
callback: null
};
settings = $.extend(defaults, settings);
if (settings.namespace && typeof settings.namespace == 'string') {
if (settings.namespace.match(/^[a-z]*$/)) {
$.i18n.map[settings.namespace] = {};
} else {
debug('Namespaces can only be lower case letters, a - z');
settings.namespace = null;
}
}
if (!settings.path.match(/\/$/)) settings.path += '/';
settings.language = this.normaliseLanguageCode(settings);
var files = (settings.name && settings.name.constructor === Array) ? settings.name : [settings.name];
settings.totalFiles = (files.length * 2) + ((settings.language.length >= 5) ? files.length : 0);
if (settings.debug) {
debug('totalFiles: ' + settings.totalFiles);
}
settings.filesLoaded = 0;
files.forEach(function (file) {
var defaultFileName, shortFileName, longFileName, fileNames;
defaultFileName = settings.path + file + '.properties';
var shortCode = settings.language.substring(0, 2);
shortFileName = settings.path + file + '_' + shortCode + '.properties';
if (settings.language.length >= 5) {
var longCode = settings.language.substring(0, 5);
longFileName = settings.path + file + '_' + longCode + '.properties';
fileNames = [defaultFileName, shortFileName, longFileName];
} else {
fileNames = [defaultFileName, shortFileName];
}
loadAndParseFiles(fileNames, settings);
});
if (settings.callback && !settings.async) {
settings.callback();
}
};
$.i18n.prop = function (key ) {
var args = [].slice.call(arguments);
var phvList, namespace;
if (args.length == 2) {
if ($.isArray(args[1])) {
phvList = args[1];
} else if (typeof args[1] === 'object') {
namespace = args[1].namespace;
var replacements = args[1].replacements;
args.splice(-1, 1);
if (replacements) {
Array.prototype.push.apply(args, replacements);
}
}
}
var value = (namespace) ? $.i18n.map[namespace][key] : $.i18n.map[key];
if (value === null) {
return '[' + ((namespace) ? namespace + '#' + key : key) + ']';
}
var i;
if (typeof(value) == 'string') {
i = 0;
while ((i = value.indexOf('\\', i)) != -1) {
if (value.charAt(i + 1) == 't') {
value = value.substring(0, i) + '\t' + value.substring((i++) + 2);
} else if (value.charAt(i + 1) == 'r') {
value = value.substring(0, i) + '\r' + value.substring((i++) + 2);
} else if (value.charAt(i + 1) == 'n') {
value = value.substring(0, i) + '\n' + value.substring((i++) + 2);
} else if (value.charAt(i + 1) == 'f') {
value = value.substring(0, i) + '\f' + value.substring((i++) + 2);
} else if (value.charAt(i + 1) == '\\') {
value = value.substring(0, i) + '\\' + value.substring((i++) + 2);
} else {
value = value.substring(0, i) + value.substring(i + 1);
}
}
var arr = [], j, index;
i = 0;
while (i < value.length) {
if (value.charAt(i) == '\'') {
if (i == value.length - 1) {
value = value.substring(0, i);
} else if (value.charAt(i + 1) == '\'') {
value = value.substring(0, i) + value.substring(++i);
} else {
j = i + 2;
while ((j = value.indexOf('\'', j)) != -1) {
if (j == value.length - 1 || value.charAt(j + 1) != '\'') {
value = value.substring(0, i) + value.substring(i + 1, j) + value.substring(j + 1);
i = j - 1;
break;
} else {
value = value.substring(0, j) + value.substring(++j);
}
}
if (j == -1) {
value = value.substring(0, i) + value.substring(i + 1);
}
}
} else if (value.charAt(i) == '{') {
j = value.indexOf('}', i + 1);
if (j == -1) {
i++;
} else {
index = parseInt(value.substring(i + 1, j));
if (!isNaN(index) && index >= 0) {
var s = value.substring(0, i);
if (s !== "") {
arr.push(s);
}
arr.push(index);
i = 0;
value = value.substring(j + 1);
} else {
i = j + 1;
}
}
} else {
i++;
}
}
if (value !== "") {
arr.push(value);
}
value = arr;
if (namespace) {
$.i18n.map[settings.namespace][key] = arr;
} else {
$.i18n.map[key] = arr;
}
}
if (value.length === 0) {
return "";
}
if (value.length == 1 && typeof(value[0]) == "string") {
return value[0];
}
var str = "";
for (i = 0, j = value.length; i < j; i++) {
if (typeof(value[i]) == "string") {
str += value[i];
} else if (phvList && value[i] < phvList.length) {
str += phvList[value[i]];
} else if (!phvList && value[i] + 1 < args.length) {
str += args[value[i] + 1];
} else {
str += "{" + value[i] + "}";
}
}
return str;
};
function callbackIfComplete(settings) {
if (settings.debug) {
debug('callbackIfComplete()');
debug('totalFiles: ' + settings.totalFiles);
debug('filesLoaded: ' + settings.filesLoaded);
}
if (settings.async) {
if (settings.filesLoaded === settings.totalFiles) {
if (settings.callback) {
settings.callback();
}
}
}
}
function loadAndParseFiles(fileNames, settings) {
if (settings.debug) debug('loadAndParseFiles');
if (fileNames !== null && fileNames.length > 0) {
loadAndParseFile(fileNames[0], settings, function () {
fileNames.shift();
loadAndParseFiles(fileNames,settings);
});
} else {
callbackIfComplete(settings);
}
}
function loadAndParseFile(filename, settings, nextFile) {
if (settings.debug) {
debug('loadAndParseFile(\'' + filename +'\')');
debug('totalFiles: ' + settings.totalFiles);
debug('filesLoaded: ' + settings.filesLoaded);
}
if (filename !== null && typeof filename !== 'undefined') {
$.ajax({
url: filename,
async: settings.async,
cache: settings.cache,
dataType: 'text',
success: function (data, status) {
if (settings.debug) {
debug('Succeeded in downloading ' + filename + '.');
debug(data);
}
parseData(data, settings);
nextFile();
},
error: function (jqXHR, textStatus, errorThrown) {
if (settings.debug) {
debug('Failed to download or parse ' + filename + '. errorThrown: ' + errorThrown);
}
if (jqXHR.status === 404) {
settings.totalFiles -= 1;
}
nextFile();
}
});
}
}
function parseData(data, settings) {
var parsed = '';
var lines = data.split(/\n/);
var regPlaceHolder = /(\{\d+})/g;
var regRepPlaceHolder = /\{(\d+)}/g;
var unicodeRE = /(\\u.{4})/ig;
for (var i=0,j=lines.length;i<j;i++) {
var line = lines[i];
line = line.trim();
if (line.length > 0 && line.match("^#") != "#") {
var pair = line.split('=');
if (pair.length > 0) {
var name = decodeURI(pair[0]).trim();
var value = pair.length == 1 ? "" : pair[1];
while (value.search(/\\$/) != -1) {
value = value.substring(0, value.length - 1);
value += lines[++i].trimRight();
}
for (var s = 2; s < pair.length; s++) {
value += '=' + pair[s];
}
value = value.trim();
if (settings.mode == 'map' || settings.mode == 'both') {
var unicodeMatches = value.match(unicodeRE);
if (unicodeMatches) {
unicodeMatches.forEach(function (match) {
value = value.replace(match, unescapeUnicode(match));
});
}
if (settings.namespace) {
$.i18n.map[settings.namespace][name] = value;
} else {
$.i18n.map[name] = value;
}
}
if (settings.mode == 'vars' || settings.mode == 'both') {
value = value.replace(/"/g, '\\"');
checkKeyNamespace(name);
if (regPlaceHolder.test(value)) {
var parts = value.split(regPlaceHolder);
var first = true;
var fnArgs = '';
var usedArgs = [];
parts.forEach(function (part) {
if (regPlaceHolder.test(part) && (usedArgs.length === 0 || usedArgs.indexOf(part) == -1)) {
if (!first) {
fnArgs += ',';
}
fnArgs += part.replace(regRepPlaceHolder, 'v$1');
usedArgs.push(part);
first = false;
}
});
parsed += name + '=function(' + fnArgs + '){';
var fnExpr = '"' + value.replace(regRepPlaceHolder, '"+v$1+"') + '"';
parsed += 'return ' + fnExpr + ';' + '};';
} else {
parsed += name + '="' + value + '";';
}
}
}
}
}
eval(parsed);
settings.filesLoaded += 1;
}
function checkKeyNamespace(key) {
var regDot = /\./;
if (regDot.test(key)) {
var fullname = '';
var names = key.split(/\./);
for (var i=0,j=names.length;i<j;i++) {
var name = names[i];
if (i > 0) {
fullname += '.';
}
fullname += name;
if (eval('typeof ' + fullname + ' == "undefined"')) {
eval(fullname + '={};');
}
}
}
}
$.i18n.normaliseLanguageCode = function (settings) {
var lang = settings.language;
if (!lang || lang.length < 2) {
if (settings.debug) debug('No language supplied. Pulling it from the browser ...');
lang = (navigator.languages && navigator.languages.length > 0) ? navigator.languages[0]
: (navigator.language || navigator.userLanguage || 'en');
if (settings.debug) debug('Language from browser: ' + lang);
}
lang = lang.toLowerCase();
lang = lang.replace(/-/,"_");
if (lang.length > 3) {
lang = lang.substring(0, 3) + lang.substring(3).toUpperCase();
}
return lang;
};
function unescapeUnicode(str) {
var codes = [];
var code = parseInt(str.substr(2), 16);
if (code >= 0 && code < Math.pow(2, 16)) {
codes.push(code);
}
return codes.reduce(function (acc, val) { return acc + String.fromCharCode(val); }, '');
}
}) (jQuery);