1/****************************************************************************** 2 * jquery.i18n.properties 3 * 4 * Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 5 * MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 6 * 7 * @version 1.0.x 8 * @author Nuno Fernandes 9 * @url www.codingwithcoffee.com 10 * @inspiration Localisation assistance for jQuery (http://keith-wood.name/localisation.html) 11 * by Keith Wood (kbwood{at}iinet.com.au) June 2007 12 * 13 *****************************************************************************/ 14 15(function($) { 16$.i18n = {}; 17 18/** Map holding bundle keys (if mode: 'map') */ 19$.i18n.map = {}; 20 21/** 22 * Load and parse message bundle files (.properties), 23 * making bundles keys available as javascript variables. 24 * 25 * i18n files are named <name>.js, or <name>_<language>.js or <name>_<language>_<country>.js 26 * Where: 27 * The <language> argument is a valid ISO Language Code. These codes are the lower-case, 28 * two-letter codes as defined by ISO-639. You can find a full list of these codes at a 29 * number of sites, such as: http://www.loc.gov/standards/iso639-2/englangn.html 30 * The <country> argument is a valid ISO Country Code. These codes are the upper-case, 31 * two-letter codes as defined by ISO-3166. You can find a full list of these codes at a 32 * number of sites, such as: http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html 33 * 34 * Sample usage for a bundles/Messages.properties bundle: 35 * $.i18n.properties({ 36 * name: 'Messages', 37 * language: 'en_US', 38 * path: 'bundles' 39 * }); 40 * @param name (string/string[], optional) names of file to load (eg, 'Messages' or ['Msg1','Msg2']). Defaults to "Messages" 41 * @param language (string, optional) language/country code (eg, 'en', 'en_US', 'pt_PT'). if not specified, language reported by the browser will be used instead. 42 * @param path (string, optional) path of directory that contains file to load 43 * @param mode (string, optional) whether bundles keys are available as JavaScript variables/functions or as a map (eg, 'vars' or 'map') 44 * @param cache (boolean, optional) whether bundles should be cached by the browser, or forcibly reloaded on each page load. Defaults to false (i.e. forcibly reloaded) 45 * @param encoding (string, optional) the encoding to request for bundles. Property file resource bundles are specified to be in ISO-8859-1 format. Defaults to UTF-8 for backward compatibility. 46 * @param callback (function, optional) callback function to be called after script is terminated 47 */ 48$.i18n.properties = function(settings) { 49 // set up settings 50 var defaults = { 51 name: 'Messages', 52 language: 'en', 53 path: '/assets/js/language/', 54 mode: 'both', 55 cache: false, 56 encoding: 'UTF-8', 57 callback: null 58 }; 59 settings = $.extend(defaults, settings); 60 if(settings.language === null || settings.language == '') { 61 settings.language = $.i18n.browserLang(); 62 } 63 if(settings.language === null) {settings.language='';} 64 65 // load and parse bundle files 66 var files = getFiles(settings.name); 67 for(i=0; i<files.length; i++) { 68 // 1. load base (eg, Messages.properties) 69 // this is requiring a file which is a duplicate of the _en file 70 //loadAndParseFile(settings.path + files[i] + '.properties', settings); 71 // 2. with language code (eg, Messages_pt.properties) 72 if(settings.language.length >= 2) { 73 loadAndParseFile(settings.path + files[i] + '_' + settings.language.substring(0, 2) +'.properties', settings); 74 } 75 // 3. with language code and country code (eg, Messages_pt_PT.properties) 76 if(settings.language.length >= 5) { 77 loadAndParseFile(settings.path + files[i] + '_' + settings.language.substring(0, 5) +'.properties', settings); 78 } 79 } 80 81 // call callback 82 if(settings.callback){ settings.callback(); } 83}; 84 85 86/** 87 * When configured with mode: 'map', allows access to bundle values by specifying its key. 88 * Eg, jQuery.i18n.prop('com.company.bundles.menu_add') 89 */ 90$.i18n.prop = function(key /* Add parameters as function arguments as necessary */) { 91 var value = $.i18n.map[key]; 92 if (value == null) 93 return '[' + key + ']'; 94 95// if(arguments.length < 2) // No arguments. 96// //if(key == 'spv.lbl.modified') {alert(value);} 97// return value; 98 99// if (!$.isArray(placeHolderValues)) { 100// // If placeHolderValues is not an array, make it into one. 101// placeHolderValues = [placeHolderValues]; 102// for (var i=2; i<arguments.length; i++) 103// placeHolderValues.push(arguments[i]); 104// } 105 106 // Place holder replacement 107 /** 108 * Tested with: 109 * test.t1=asdf ''{0}'' 110 * test.t2=asdf '{0}' '{1}'{1}'zxcv 111 * test.t3=This is \"a quote" 'a''{0}''s'd{fgh{ij' 112 * test.t4="'''{'0}''" {0}{a} 113 * test.t5="'''{0}'''" {1} 114 * test.t6=a {1} b {0} c 115 * test.t7=a 'quoted \\ s\ttringy' \t\t x 116 * 117 * Produces: 118 * test.t1, p1 ==> asdf 'p1' 119 * test.t2, p1 ==> asdf {0} {1}{1}zxcv 120 * test.t3, p1 ==> This is "a quote" a'{0}'sd{fgh{ij 121 * test.t4, p1 ==> "'{0}'" p1{a} 122 * test.t5, p1 ==> "'{0}'" {1} 123 * test.t6, p1 ==> a {1} b p1 c 124 * test.t6, p1, p2 ==> a p2 b p1 c 125 * test.t6, p1, p2, p3 ==> a p2 b p1 c 126 * test.t7 ==> a quoted \ s tringy x 127 */ 128 129 var i; 130 if (typeof(value) == 'string') { 131 // Handle escape characters. Done separately from the tokenizing loop below because escape characters are 132 // active in quoted strings. 133 i = 0; 134 while ((i = value.indexOf('\\', i)) != -1) { 135 if (value[i+1] == 't') 136 value = value.substring(0, i) + '\t' + value.substring((i++) + 2); // tab 137 else if (value[i+1] == 'r') 138 value = value.substring(0, i) + '\r' + value.substring((i++) + 2); // return 139 else if (value[i+1] == 'n') 140 value = value.substring(0, i) + '\n' + value.substring((i++) + 2); // line feed 141 else if (value[i+1] == 'f') 142 value = value.substring(0, i) + '\f' + value.substring((i++) + 2); // form feed 143 else if (value[i+1] == '\\') 144 value = value.substring(0, i) + '\\' + value.substring((i++) + 2); // \ 145 else 146 value = value.substring(0, i) + value.substring(i+1); // Quietly drop the character 147 } 148 149 // Lazily convert the string to a list of tokens. 150 var arr = [], j, index; 151 i = 0; 152 while (i < value.length) { 153 if (value[i] == '\'') { 154 // Handle quotes 155 if (i == value.length-1) 156 value = value.substring(0, i); // Silently drop the trailing quote 157 else if (value[i+1] == '\'') 158 value = value.substring(0, i) + value.substring(++i); // Escaped quote 159 else { 160 // Quoted string 161 j = i + 2; 162 while ((j = value.indexOf('\'', j)) != -1) { 163 if (j == value.length-1 || value[j+1] != '\'') { 164 // Found start and end quotes. Remove them 165 value = value.substring(0,i) + value.substring(i+1, j) + value.substring(j+1); 166 i = j - 1; 167 break; 168 } 169 else { 170 // Found a double quote, reduce to a single quote. 171 value = value.substring(0,j) + value.substring(++j); 172 } 173 } 174 175 if (j == -1) { 176 // There is no end quote. Drop the start quote 177 value = value.substring(0,i) + value.substring(i+1); 178 } 179 } 180 } 181 else if (value[i] == '{') { 182 // Beginning of an unquoted place holder. 183 j = value.indexOf('}', i+1); 184 if (j == -1) 185 i++; // No end. Process the rest of the line. Java would throw an exception 186 else { 187 // Add 1 to the index so that it aligns with the function arguments. 188 index = parseInt(value.substring(i+1, j)); 189 if (!isNaN(index) && index >= 0) { 190 // Put the line thus far (if it isn't empty) into the array 191 var s = value.substring(0, i); 192 if (s != "") 193 arr.push(s); 194 // Put the parameter reference into the array 195 arr.push(index); 196 // Start the processing over again starting from the rest of the line. 197 i = 0; 198 value = value.substring(j+1); 199 } 200 else 201 i = j + 1; // Invalid parameter. Leave as is. 202 } 203 } 204 else 205 i++; 206 } 207 208 // Put the remainder of the no-empty line into the array. 209 if (value != "") 210 arr.push(value); 211 value = arr; 212 213 // Make the array the value for the entry. 214 $.i18n.map[key] = arr; 215 } 216 217 if (value.length == 0) 218 return ""; 219 if (value.lengh == 1 && typeof(value[0]) == "string") 220 return value[0]; 221 222 var s = ""; 223 for (i=0; i<value.length; i++) { 224 if (typeof(value[i]) == "string") 225 s += value[i]; 226 // Must be a number 227 else if (value[i] + 1 < arguments.length) 228 s += arguments[value[i] + 1]; 229 else 230 s += "{"+ value[i] +"}"; 231 } 232 233 return s; 234}; 235
236/** Language reported by browser, normalized code */ 237$.i18n.browserLang = function() { 238 return normaliseLanguageCode(navigator.language /* Mozilla */ || navigator.userLanguage /* IE */); 239} 240 241 242/** Load and parse .properties files */ 243function loadAndParseFile(filename, settings) { 244 $.ajax({ 245 url: filename, 246 async: false, 247 cache: settings.cache, 248 contentType:'text/plain;charset='+ settings.encoding, 249 dataType: 'text', 250 success: function(data, status) { 251 parseData(data, settings.mode); 252 } 253 }); 254} 255 256/** Parse .properties files */ 257function parseData(data, mode) { 258 var parsed = ''; 259 var parameters = data.split( /\n/ ); 260 var regPlaceHolder = /(\{\d+\})/g; 261 var regRepPlaceHolder = /\{(\d+)\}/g; 262 var unicodeRE = /(\\u.{4})/ig; 263 for(var i=0; i<parameters.length; i++ ) { 264 parameters[i] = parameters[i].replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' ); // trim 265 if(parameters[i].length > 0 && parameters[i].match("^#")!="#") { // skip comments 266 var pair = parameters[i].split('='); 267 if(pair.length > 0) { 268 /** Process key & value */ 269 var name = unescape(pair[0]).replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' ); // trim 270 var value = pair.length == 1 ? "" : pair[1]; 271 // process multi-line values 272 while(value.match(/\\$/)=="\\") { 273 value = value.substring(0, value.length - 1); 274 value += parameters[++i].replace( /\s\s*$/, '' ); // right trim 275 } 276 // Put values with embedded '='s back together 277 for(var s=2;s<pair.length;s++){ value +='=' + pair[s]; } 278 value = value.replace( /^\s\s*/, '' ).replace( /\s\s*$/, '' ); // trim 279 280 /** Mode: bundle keys in a map */ 281 if(mode == 'map' || mode == 'both') { 282 // handle unicode chars possibly left out 283 var unicodeMatches = value.match(unicodeRE); 284 if(unicodeMatches) { 285 for(var u=0; u<unicodeMatches.length; u++) { 286 value = value.replace( unicodeMatches[u], unescapeUnicode(unicodeMatches[u])); 287 } 288 } 289 // add to map 290 $.i18n.map[name] = value; 291 } 292 293 /** Mode: bundle keys as vars/functions */ 294 if(mode == 'vars' || mode == 'both') { 295 value = value.replace( /"/g, '\\"' ); // escape quotation mark (") 296 297 // make sure namespaced key exists (eg, 'some.key') 298 checkKeyNamespace(name); 299 300 // value with variable substitutions 301 if(regPlaceHolder.test(value)) { 302 var parts = value.split(regPlaceHolder); 303 // process function args 304 var first = true; 305 var fnArgs = ''; 306 var usedArgs = []; 307 for(var p=0; p<parts.length; p++) { 308 if(regPlaceHolder.test(parts[p]) && (usedArgs.length == 0 || usedArgs.indexOf(parts[p]) == -1)) { 309 if(!first) {fnArgs += ',';} 310 fnArgs += parts[p].replace(regRepPlaceHolder, 'v$1'); 311 usedArgs.push(parts[p]); 312 first = false; 313 } 314 } 315 parsed += name + '=function(' + fnArgs + '){'; 316 // process function body 317 var fnExpr = '"' + value.replace(regRepPlaceHolder, '"+v$1+"') + '"'; 318 parsed += 'return ' + fnExpr + ';' + '};'; 319 320 // simple value 321 }else{ 322 parsed += name+'="'+value+'";'; 323 } 324 } // END: Mode: bundle keys as vars/functions 325 } // END: if(pair.length > 0) 326 } // END: skip comments 327 } 328 eval(parsed); 329} 330 331/** Make sure namespace exists (for keys with dots in name) */ 332// TODO key parts that start with numbers quietly fail. i.e. month.short.1=Jan 333function checkKeyNamespace(key) { 334 var regDot = /\./; 335 if(regDot.test(key)) { 336 var fullname = ''; 337 var names = key.split( /\./ ); 338 for(var i=0; i<names.length; i++) { 339 if(i>0) {fullname += '.';} 340 fullname += names[i]; 341 if(eval('typeof '+fullname+' == "undefined"')) { 342 eval(fullname + '={};'); 343 } 344 } 345 } 346} 347 348/** Make sure filename is an array */ 349function getFiles(names) { 350 return (names && names.constructor == Array) ? names : [names]; 351} 352 353/** Ensure language code is in the format aa_AA. */ 354function normaliseLanguageCode(lang) { 355 lang = lang.toLowerCase(); 356 if(lang.length > 3) { 357 lang = lang.substring(0, 3) + lang.substring(3).toUpperCase(); 358 } 359 return lang; 360} 361 362/** Unescape unicode chars ('\u00e3') */ 363function unescapeUnicode(str) { 364 // unescape unicode codes 365 var codes = []; 366 var code = parseInt(str.substr(2), 16); 367 if (code >= 0 && code < Math.pow(2, 16)) { 368 codes.push(code); 369 } 370 // convert codes to text 371 var unescaped = ''; 372 for (var i = 0; i < codes.length; ++i) { 373 unescaped += String.fromCharCode(codes[i]); 374 } 375 return unescaped; 376} 377 378/* Cross-Browser Split 1.0.1 379(c) Steven Levithan <stevenlevithan.com>; MIT License 380An ECMA-compliant, uniform cross-browser split method */ 381var cbSplit; 382// avoid running twice, which would break `cbSplit._nativeSplit`'s reference to the native `split` 383if (!cbSplit) { 384 cbSplit = function(str, separator, limit) { 385 // if `separator` is not a regex, use the native `split` 386 if (Object.prototype.toString.call(separator) !== "[object RegExp]") { 387 if(typeof cbSplit._nativeSplit == "undefined") 388 return str.split(separator, limit); 389 else 390 return cbSplit._nativeSplit.call(str, separator, limit); 391 } 392 393 var output = [], 394 lastLastIndex = 0, 395 flags = (separator.ignoreCase ? "i" : "") + 396 (separator.multiline ? "m" : "") + 397 (separator.sticky ? "y" : ""), 398 separator = RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues by working with a copy 399 separator2, match, lastIndex, lastLength; 400 401 str = str + ""; // type conversion 402 if (!cbSplit._compliantExecNpcg) { 403 separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags); // doesn't need /g or /y, but they don't hurt 404 } 405 406 /* behavior for `limit`: if it's... 407 - `undefined`: no limit. 408 - `NaN` or zero: return an empty array. 409 - a positive number: use `Math.floor(limit)`. 410 - a negative number: no limit. 411 - other: type-convert, then use the above rules. */ 412 if (limit === undefined || +limit < 0) { 413 limit = Infinity; 414 } else { 415 limit = Math.floor(+limit); 416 if (!limit) { 417 return []; 418 } 419 } 420 421 while (match = separator.exec(str)) { 422 lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser 423 424 if (lastIndex > lastLastIndex) { 425 output.push(str.slice(lastLastIndex, match.index)); 426 427 // fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups 428 if (!cbSplit._compliantExecNpcg && match.length > 1) { 429 match[0].replace(separator2, function () { 430 for (var i = 1; i < arguments.length - 2; i++) { 431 if (arguments[i] === undefined) { 432 match[i] = undefined; 433 } 434 } 435 }); 436 } 437 438 if (match.length > 1 && match.index < str.length) { 439 Array.prototype.push.apply(output, match.slice(1)); 440 } 441 442 lastLength = match[0].length; 443 lastLastIndex = lastIndex; 444 445 if (output.length >= limit) { 446 break; 447 } 448 } 449 450 if (separator.lastIndex === match.index) { 451 separator.lastIndex++; // avoid an infinite loop 452 } 453 } 454 455 if (lastLastIndex === str.length) { 456 if (lastLength || !separator.test("")) { 457 output.push(""); 458 } 459 } else { 460 output.push(str.slice(lastLastIndex)); 461 } 462 463 return output.length > limit ? output.slice(0, limit) : output; 464 }; 465 466 cbSplit._compliantExecNpcg = /()??/.exec("")[1] === undefined; // NPCG: nonparticipating capturing group 467 cbSplit._nativeSplit = String.prototype.split; 468 469} // end `if (!cbSplit)` 470String.prototype.split = function (separator, limit) { 471 return cbSplit(this, separator, limit); 472}; 473 474})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.