jquery.i18n.properties.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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.2.7
  8. * @url https://github.com/jquery-i18n-properties/jquery-i18n-properties
  9. * @inspiration Localisation assistance for jQuery (http://keith-wood.name/localisation.html)
  10. * by Keith Wood (kbwood{at}iinet.com.au) June 2007
  11. *
  12. *****************************************************************************/
  13. (function ($) {
  14. $.i18n = {};
  15. /**
  16. * Map holding bundle keys if mode is 'map' or 'both'. Values of this can also be an
  17. * Object, in which case the key is a namespace.
  18. */
  19. $.i18n.map = {};
  20. var debug = function (message) {
  21. window.console && console.log('i18n::' + message);
  22. };
  23. /**
  24. * Load and parse message bundle files (.properties),
  25. * making bundles keys available as javascript variables.
  26. *
  27. * i18n files are named <name>.js, or <name>_<language>.js or <name>_<language>_<country>.js
  28. * Where:
  29. * The <language> argument is a valid ISO Language Code. These codes are the lower-case,
  30. * two-letter codes as defined by ISO-639. You can find a full list of these codes at a
  31. * number of sites, such as: http://www.loc.gov/standards/iso639-2/englangn.html
  32. * The <country> argument is a valid ISO Country Code. These codes are the upper-case,
  33. * two-letter codes as defined by ISO-3166. You can find a full list of these codes at a
  34. * number of sites, such as: http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
  35. *
  36. * Sample usage for a bundles/Messages.properties bundle:
  37. * $.i18n.properties({
  38. * name: 'Messages',
  39. * language: 'en_US',
  40. * path: 'bundles'
  41. * });
  42. * @param name (string/string[], optional) names of file to load (eg, 'Messages' or ['Msg1','Msg2']). Defaults to "Messages"
  43. * @param language (string, optional) language/country code (eg, 'en', 'en_US', 'pt_BR'). if not specified, language reported by the browser will be used instead.
  44. * @param path (string, optional) path of directory that contains file to load
  45. * @param mode (string, optional) whether bundles keys are available as JavaScript variables/functions or as a map (eg, 'vars' or 'map')
  46. * @param debug (boolean, optional) whether debug statements are logged at the console
  47. * @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)
  48. * @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.
  49. * @param callback (function, optional) callback function to be called after script is terminated
  50. */
  51. $.i18n.properties = function (settings) {
  52. var defaults = {
  53. name: 'Messages',
  54. language: '',
  55. path: '',
  56. namespace: null,
  57. mode: 'vars',
  58. cache: false,
  59. debug: false,
  60. encoding: 'UTF-8',
  61. async: false,
  62. callback: null
  63. };
  64. settings = $.extend(defaults, settings);
  65. if (settings.namespace && typeof settings.namespace == 'string') {
  66. // A namespace has been supplied, initialise it.
  67. if (settings.namespace.match(/^[a-z]*$/)) {
  68. $.i18n.map[settings.namespace] = {};
  69. } else {
  70. debug('Namespaces can only be lower case letters, a - z');
  71. settings.namespace = null;
  72. }
  73. }
  74. // Ensure a trailing slash on the path
  75. if (!settings.path.match(/\/$/)) settings.path += '/';
  76. // Try to ensure that we have at a least a two letter language code
  77. settings.language = this.normaliseLanguageCode(settings);
  78. // Ensure an array
  79. var files = (settings.name && settings.name.constructor === Array) ? settings.name : [settings.name];
  80. // A locale is at least a language code which means at least two files per name. If
  81. // we also have a country code, thats an extra file per name.
  82. settings.totalFiles = (files.length * 2) + ((settings.language.length >= 5) ? files.length : 0);
  83. if (settings.debug) {
  84. debug('totalFiles: ' + settings.totalFiles);
  85. }
  86. settings.filesLoaded = 0;
  87. files.forEach(function (file) {
  88. var defaultFileName, shortFileName, longFileName, fileNames;
  89. // 1. load base (eg, Messages.properties)
  90. defaultFileName = settings.path + file + '.properties';
  91. // 2. with language code (eg, Messages_pt.properties)
  92. var shortCode = settings.language.substring(0, 2);
  93. shortFileName = settings.path + file + '_' + shortCode + '.properties';
  94. // 3. with language code and country code (eg, Messages_pt_BR.properties)
  95. if (settings.language.length >= 5) {
  96. var longCode = settings.language.substring(0, 5);
  97. longFileName = settings.path + file + '_' + longCode + '.properties';
  98. fileNames = [defaultFileName, shortFileName, longFileName];
  99. } else {
  100. fileNames = [defaultFileName, shortFileName];
  101. }
  102. loadAndParseFiles(fileNames, settings);
  103. });
  104. // call callback
  105. if (settings.callback && !settings.async) {
  106. settings.callback();
  107. }
  108. }; // properties
  109. /**
  110. * When configured with mode: 'map', allows access to bundle values by specifying its key.
  111. * Eg, jQuery.i18n.prop('com.company.bundles.menu_add')
  112. */
  113. $.i18n.prop = function (key /* Add parameters as function arguments as necessary */) {
  114. var args = [].slice.call(arguments);
  115. var phvList, namespace;
  116. if (args.length == 2) {
  117. if ($.isArray(args[1])) {
  118. // An array was passed as the second parameter, so assume it is the list of place holder values.
  119. phvList = args[1];
  120. } else if (typeof args[1] === 'object') {
  121. // Second argument is an options object {namespace: 'mynamespace', replacements: ['egg', 'nog']}
  122. namespace = args[1].namespace;
  123. var replacements = args[1].replacements;
  124. args.splice(-1, 1);
  125. if (replacements) {
  126. Array.prototype.push.apply(args, replacements);
  127. }
  128. }
  129. }
  130. var value = (namespace) ? $.i18n.map[namespace][key] : $.i18n.map[key];
  131. if (value === null) {
  132. return '[' + ((namespace) ? namespace + '#' + key : key) + ']';
  133. }
  134. // Place holder replacement
  135. /**
  136. * Tested with:
  137. * test.t1=asdf ''{0}''
  138. * test.t2=asdf '{0}' '{1}'{1}'zxcv
  139. * test.t3=This is \"a quote" 'a''{0}''s'd{fgh{ij'
  140. * test.t4="'''{'0}''" {0}{a}
  141. * test.t5="'''{0}'''" {1}
  142. * test.t6=a {1} b {0} c
  143. * test.t7=a 'quoted \\ s\ttringy' \t\t x
  144. *
  145. * Produces:
  146. * test.t1, p1 ==> asdf 'p1'
  147. * test.t2, p1 ==> asdf {0} {1}{1}zxcv
  148. * test.t3, p1 ==> This is "a quote" a'{0}'sd{fgh{ij
  149. * test.t4, p1 ==> "'{0}'" p1{a}
  150. * test.t5, p1 ==> "'{0}'" {1}
  151. * test.t6, p1 ==> a {1} b p1 c
  152. * test.t6, p1, p2 ==> a p2 b p1 c
  153. * test.t6, p1, p2, p3 ==> a p2 b p1 c
  154. * test.t7 ==> a quoted \ s tringy x
  155. */
  156. var i;
  157. if (typeof(value) == 'string') {
  158. // Handle escape characters. Done separately from the tokenizing loop below because escape characters are
  159. // active in quoted strings.
  160. i = 0;
  161. while ((i = value.indexOf('\\', i)) != -1) {
  162. if (value.charAt(i + 1) == 't') {
  163. value = value.substring(0, i) + '\t' + value.substring((i++) + 2); // tab
  164. } else if (value.charAt(i + 1) == 'r') {
  165. value = value.substring(0, i) + '\r' + value.substring((i++) + 2); // return
  166. } else if (value.charAt(i + 1) == 'n') {
  167. value = value.substring(0, i) + '\n' + value.substring((i++) + 2); // line feed
  168. } else if (value.charAt(i + 1) == 'f') {
  169. value = value.substring(0, i) + '\f' + value.substring((i++) + 2); // form feed
  170. } else if (value.charAt(i + 1) == '\\') {
  171. value = value.substring(0, i) + '\\' + value.substring((i++) + 2); // \
  172. } else {
  173. value = value.substring(0, i) + value.substring(i + 1); // Quietly drop the character
  174. }
  175. }
  176. // Lazily convert the string to a list of tokens.
  177. var arr = [], j, index;
  178. i = 0;
  179. while (i < value.length) {
  180. if (value.charAt(i) == '\'') {
  181. // Handle quotes
  182. if (i == value.length - 1) {
  183. value = value.substring(0, i); // Silently drop the trailing quote
  184. } else if (value.charAt(i + 1) == '\'') {
  185. value = value.substring(0, i) + value.substring(++i); // Escaped quote
  186. } else {
  187. // Quoted string
  188. j = i + 2;
  189. while ((j = value.indexOf('\'', j)) != -1) {
  190. if (j == value.length - 1 || value.charAt(j + 1) != '\'') {
  191. // Found start and end quotes. Remove them
  192. value = value.substring(0, i) + value.substring(i + 1, j) + value.substring(j + 1);
  193. i = j - 1;
  194. break;
  195. } else {
  196. // Found a double quote, reduce to a single quote.
  197. value = value.substring(0, j) + value.substring(++j);
  198. }
  199. }
  200. if (j == -1) {
  201. // There is no end quote. Drop the start quote
  202. value = value.substring(0, i) + value.substring(i + 1);
  203. }
  204. }
  205. } else if (value.charAt(i) == '{') {
  206. // Beginning of an unquoted place holder.
  207. j = value.indexOf('}', i + 1);
  208. if (j == -1) {
  209. i++; // No end. Process the rest of the line. Java would throw an exception
  210. } else {
  211. // Add 1 to the index so that it aligns with the function arguments.
  212. index = parseInt(value.substring(i + 1, j));
  213. if (!isNaN(index) && index >= 0) {
  214. // Put the line thus far (if it isn't empty) into the array
  215. var s = value.substring(0, i);
  216. if (s !== "") {
  217. arr.push(s);
  218. }
  219. // Put the parameter reference into the array
  220. arr.push(index);
  221. // Start the processing over again starting from the rest of the line.
  222. i = 0;
  223. value = value.substring(j + 1);
  224. } else {
  225. i = j + 1; // Invalid parameter. Leave as is.
  226. }
  227. }
  228. } else {
  229. i++;
  230. }
  231. } // while
  232. // Put the remainder of the no-empty line into the array.
  233. if (value !== "") {
  234. arr.push(value);
  235. }
  236. value = arr;
  237. // Make the array the value for the entry.
  238. if (namespace) {
  239. $.i18n.map[settings.namespace][key] = arr;
  240. } else {
  241. $.i18n.map[key] = arr;
  242. }
  243. }
  244. if(value==null){
  245. console.log("errorerrorerror:"+key)
  246. }
  247. if (value.length === 0) {
  248. return "";
  249. }
  250. if (value.length == 1 && typeof(value[0]) == "string") {
  251. return value[0];
  252. }
  253. var str = "";
  254. for (i = 0, j = value.length; i < j; i++) {
  255. if (typeof(value[i]) == "string") {
  256. str += value[i];
  257. } else if (phvList && value[i] < phvList.length) {
  258. // Must be a number
  259. str += phvList[value[i]];
  260. } else if (!phvList && value[i] + 1 < args.length) {
  261. str += args[value[i] + 1];
  262. } else {
  263. str += "{" + value[i] + "}";
  264. }
  265. }
  266. return str;
  267. };
  268. function callbackIfComplete(settings) {
  269. if (settings.debug) {
  270. debug('callbackIfComplete()');
  271. debug('totalFiles: ' + settings.totalFiles);
  272. debug('filesLoaded: ' + settings.filesLoaded);
  273. }
  274. if (settings.async) {
  275. if (settings.filesLoaded === settings.totalFiles) {
  276. if (settings.callback) {
  277. settings.callback();
  278. }
  279. }
  280. }
  281. }
  282. function loadAndParseFiles(fileNames, settings) {
  283. if (settings.debug) debug('loadAndParseFiles');
  284. if (fileNames !== null && fileNames.length > 0) {
  285. loadAndParseFile(fileNames[0], settings, function () {
  286. fileNames.shift();
  287. loadAndParseFiles(fileNames,settings);
  288. });
  289. } else {
  290. callbackIfComplete(settings);
  291. }
  292. }
  293. /** Load and parse .properties files */
  294. function loadAndParseFile(filename, settings, nextFile) {
  295. if (settings.debug) {
  296. debug('loadAndParseFile(\'' + filename +'\')');
  297. debug('totalFiles: ' + settings.totalFiles);
  298. debug('filesLoaded: ' + settings.filesLoaded);
  299. }
  300. if (filename !== null && typeof filename !== 'undefined') {
  301. console.log(filename)
  302. $.ajax({
  303. url: filename,
  304. async: settings.async,
  305. cache: settings.cache,
  306. dataType: 'text',
  307. success: function (data, status) {
  308. if (settings.debug) {
  309. debug('Succeeded in downloading ' + filename + '.');
  310. debug(data);
  311. }
  312. parseData(data, settings);
  313. nextFile();
  314. },
  315. error: function (jqXHR, textStatus, errorThrown) {
  316. if (settings.debug) {
  317. debug('Failed to download or parse ' + filename + '. errorThrown: ' + errorThrown);
  318. }
  319. if (jqXHR.status === 404) {
  320. settings.totalFiles -= 1;
  321. }
  322. nextFile();
  323. }
  324. });
  325. }
  326. }
  327. /** Parse .properties files */
  328. function parseData(data, settings) {
  329. var parsed = '';
  330. var lines = data.split(/\n/);
  331. var regPlaceHolder = /(\{\d+})/g;
  332. var regRepPlaceHolder = /\{(\d+)}/g;
  333. var unicodeRE = /(\\u.{4})/ig;
  334. for (var i=0,j=lines.length;i<j;i++) {
  335. var line = lines[i];
  336. line = line.trim();
  337. if (line.length > 0 && line.match("^#") != "#") { // skip comments
  338. var pair = line.split('=');
  339. if (pair.length > 0) {
  340. /** Process key & value */
  341. var name = decodeURI(pair[0]).trim();
  342. var value = pair.length == 1 ? "" : pair[1];
  343. // process multi-line values
  344. while (value.search(/\\$/) != -1) {
  345. value = value.substring(0, value.length - 1);
  346. value += lines[++i].trimRight();
  347. }
  348. // Put values with embedded '='s back together
  349. for (var s = 2; s < pair.length; s++) {
  350. value += '=' + pair[s];
  351. }
  352. value = value.trim();
  353. /** Mode: bundle keys in a map */
  354. if (settings.mode == 'map' || settings.mode == 'both') {
  355. // handle unicode chars possibly left out
  356. var unicodeMatches = value.match(unicodeRE);
  357. if (unicodeMatches) {
  358. unicodeMatches.forEach(function (match) {
  359. value = value.replace(match, unescapeUnicode(match));
  360. });
  361. }
  362. // add to map
  363. if (settings.namespace) {
  364. $.i18n.map[settings.namespace][name] = value;
  365. } else {
  366. $.i18n.map[name] = value;
  367. }
  368. }
  369. /** Mode: bundle keys as vars/functions */
  370. if (settings.mode == 'vars' || settings.mode == 'both') {
  371. value = value.replace(/"/g, '\\"'); // escape quotation mark (")
  372. // make sure namespaced key exists (eg, 'some.key')
  373. checkKeyNamespace(name);
  374. // value with variable substitutions
  375. if (regPlaceHolder.test(value)) {
  376. var parts = value.split(regPlaceHolder);
  377. // process function args
  378. var first = true;
  379. var fnArgs = '';
  380. var usedArgs = [];
  381. parts.forEach(function (part) {
  382. if (regPlaceHolder.test(part) && (usedArgs.length === 0 || usedArgs.indexOf(part) == -1)) {
  383. if (!first) {
  384. fnArgs += ',';
  385. }
  386. fnArgs += part.replace(regRepPlaceHolder, 'v$1');
  387. usedArgs.push(part);
  388. first = false;
  389. }
  390. });
  391. parsed += name + '=function(' + fnArgs + '){';
  392. // process function body
  393. var fnExpr = '"' + value.replace(regRepPlaceHolder, '"+v$1+"') + '"';
  394. parsed += 'return ' + fnExpr + ';' + '};';
  395. // simple value
  396. } else {
  397. parsed += name + '="' + value + '";';
  398. }
  399. } // END: Mode: bundle keys as vars/functions
  400. } // END: if(pair.length > 0)
  401. } // END: skip comments
  402. }
  403. eval(parsed);
  404. settings.filesLoaded += 1;
  405. }
  406. /** Make sure namespace exists (for keys with dots in name) */
  407. // TODO key parts that start with numbers quietly fail. i.e. month.short.1=Jan
  408. function checkKeyNamespace(key) {
  409. var regDot = /\./;
  410. if (regDot.test(key)) {
  411. var fullname = '';
  412. var names = key.split(/\./);
  413. for (var i=0,j=names.length;i<j;i++) {
  414. var name = names[i];
  415. if (i > 0) {
  416. fullname += '.';
  417. }
  418. fullname += name;
  419. if (eval('typeof ' + fullname + ' == "undefined"')) {
  420. eval(fullname + '={};');
  421. }
  422. }
  423. }
  424. }
  425. /** Ensure language code is in the format aa_AA. */
  426. $.i18n.normaliseLanguageCode = function (settings) {
  427. var lang = settings.language;
  428. if (!lang || lang.length < 2) {
  429. if (settings.debug) debug('No language supplied. Pulling it from the browser ...');
  430. lang = (navigator.languages && navigator.languages.length > 0) ? navigator.languages[0]
  431. : (navigator.language || navigator.userLanguage /* IE */ || 'en');
  432. if (settings.debug) debug('Language from browser: ' + lang);
  433. }
  434. lang = lang.toLowerCase();
  435. lang = lang.replace(/-/,"_"); // some browsers report language as en-US instead of en_US
  436. if (lang.length > 3) {
  437. lang = lang.substring(0, 3) + lang.substring(3).toUpperCase();
  438. }
  439. return lang;
  440. };
  441. /** Unescape unicode chars ('\u00e3') */
  442. function unescapeUnicode(str) {
  443. // unescape unicode codes
  444. var codes = [];
  445. var code = parseInt(str.substr(2), 16);
  446. if (code >= 0 && code < Math.pow(2, 16)) {
  447. codes.push(code);
  448. }
  449. // convert codes to text
  450. return codes.reduce(function (acc, val) { return acc + String.fromCharCode(val); }, '');
  451. }
  452. }) (jQuery);