jquery.i18n.properties.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.length === 0) {
  245. return "";
  246. }
  247. if (value.length == 1 && typeof(value[0]) == "string") {
  248. return value[0];
  249. }
  250. var str = "";
  251. for (i = 0, j = value.length; i < j; i++) {
  252. if (typeof(value[i]) == "string") {
  253. str += value[i];
  254. } else if (phvList && value[i] < phvList.length) {
  255. // Must be a number
  256. str += phvList[value[i]];
  257. } else if (!phvList && value[i] + 1 < args.length) {
  258. str += args[value[i] + 1];
  259. } else {
  260. str += "{" + value[i] + "}";
  261. }
  262. }
  263. return str;
  264. };
  265. function callbackIfComplete(settings) {
  266. if (settings.debug) {
  267. debug('callbackIfComplete()');
  268. debug('totalFiles: ' + settings.totalFiles);
  269. debug('filesLoaded: ' + settings.filesLoaded);
  270. }
  271. if (settings.async) {
  272. if (settings.filesLoaded === settings.totalFiles) {
  273. if (settings.callback) {
  274. settings.callback();
  275. }
  276. }
  277. }
  278. }
  279. function loadAndParseFiles(fileNames, settings) {
  280. if (settings.debug) debug('loadAndParseFiles');
  281. if (fileNames !== null && fileNames.length > 0) {
  282. loadAndParseFile(fileNames[0], settings, function () {
  283. fileNames.shift();
  284. loadAndParseFiles(fileNames,settings);
  285. });
  286. } else {
  287. callbackIfComplete(settings);
  288. }
  289. }
  290. /** Load and parse .properties files */
  291. function loadAndParseFile(filename, settings, nextFile) {
  292. if (settings.debug) {
  293. debug('loadAndParseFile(\'' + filename +'\')');
  294. debug('totalFiles: ' + settings.totalFiles);
  295. debug('filesLoaded: ' + settings.filesLoaded);
  296. }
  297. if (filename !== null && typeof filename !== 'undefined') {
  298. console.log(filename)
  299. $.ajax({
  300. url: filename,
  301. async: settings.async,
  302. cache: settings.cache,
  303. dataType: 'text',
  304. success: function (data, status) {
  305. if (settings.debug) {
  306. debug('Succeeded in downloading ' + filename + '.');
  307. debug(data);
  308. }
  309. parseData(data, settings);
  310. nextFile();
  311. },
  312. error: function (jqXHR, textStatus, errorThrown) {
  313. if (settings.debug) {
  314. debug('Failed to download or parse ' + filename + '. errorThrown: ' + errorThrown);
  315. }
  316. if (jqXHR.status === 404) {
  317. settings.totalFiles -= 1;
  318. }
  319. nextFile();
  320. }
  321. });
  322. }
  323. }
  324. /** Parse .properties files */
  325. function parseData(data, settings) {
  326. var parsed = '';
  327. var lines = data.split(/\n/);
  328. var regPlaceHolder = /(\{\d+})/g;
  329. var regRepPlaceHolder = /\{(\d+)}/g;
  330. var unicodeRE = /(\\u.{4})/ig;
  331. for (var i=0,j=lines.length;i<j;i++) {
  332. var line = lines[i];
  333. line = line.trim();
  334. if (line.length > 0 && line.match("^#") != "#") { // skip comments
  335. var pair = line.split('=');
  336. if (pair.length > 0) {
  337. /** Process key & value */
  338. var name = decodeURI(pair[0]).trim();
  339. var value = pair.length == 1 ? "" : pair[1];
  340. // process multi-line values
  341. while (value.search(/\\$/) != -1) {
  342. value = value.substring(0, value.length - 1);
  343. value += lines[++i].trimRight();
  344. }
  345. // Put values with embedded '='s back together
  346. for (var s = 2; s < pair.length; s++) {
  347. value += '=' + pair[s];
  348. }
  349. value = value.trim();
  350. /** Mode: bundle keys in a map */
  351. if (settings.mode == 'map' || settings.mode == 'both') {
  352. // handle unicode chars possibly left out
  353. var unicodeMatches = value.match(unicodeRE);
  354. if (unicodeMatches) {
  355. unicodeMatches.forEach(function (match) {
  356. value = value.replace(match, unescapeUnicode(match));
  357. });
  358. }
  359. // add to map
  360. if (settings.namespace) {
  361. $.i18n.map[settings.namespace][name] = value;
  362. } else {
  363. $.i18n.map[name] = value;
  364. }
  365. }
  366. /** Mode: bundle keys as vars/functions */
  367. if (settings.mode == 'vars' || settings.mode == 'both') {
  368. value = value.replace(/"/g, '\\"'); // escape quotation mark (")
  369. // make sure namespaced key exists (eg, 'some.key')
  370. checkKeyNamespace(name);
  371. // value with variable substitutions
  372. if (regPlaceHolder.test(value)) {
  373. var parts = value.split(regPlaceHolder);
  374. // process function args
  375. var first = true;
  376. var fnArgs = '';
  377. var usedArgs = [];
  378. parts.forEach(function (part) {
  379. if (regPlaceHolder.test(part) && (usedArgs.length === 0 || usedArgs.indexOf(part) == -1)) {
  380. if (!first) {
  381. fnArgs += ',';
  382. }
  383. fnArgs += part.replace(regRepPlaceHolder, 'v$1');
  384. usedArgs.push(part);
  385. first = false;
  386. }
  387. });
  388. parsed += name + '=function(' + fnArgs + '){';
  389. // process function body
  390. var fnExpr = '"' + value.replace(regRepPlaceHolder, '"+v$1+"') + '"';
  391. parsed += 'return ' + fnExpr + ';' + '};';
  392. // simple value
  393. } else {
  394. parsed += name + '="' + value + '";';
  395. }
  396. } // END: Mode: bundle keys as vars/functions
  397. } // END: if(pair.length > 0)
  398. } // END: skip comments
  399. }
  400. eval(parsed);
  401. settings.filesLoaded += 1;
  402. }
  403. /** Make sure namespace exists (for keys with dots in name) */
  404. // TODO key parts that start with numbers quietly fail. i.e. month.short.1=Jan
  405. function checkKeyNamespace(key) {
  406. var regDot = /\./;
  407. if (regDot.test(key)) {
  408. var fullname = '';
  409. var names = key.split(/\./);
  410. for (var i=0,j=names.length;i<j;i++) {
  411. var name = names[i];
  412. if (i > 0) {
  413. fullname += '.';
  414. }
  415. fullname += name;
  416. if (eval('typeof ' + fullname + ' == "undefined"')) {
  417. eval(fullname + '={};');
  418. }
  419. }
  420. }
  421. }
  422. /** Ensure language code is in the format aa_AA. */
  423. $.i18n.normaliseLanguageCode = function (settings) {
  424. var lang = settings.language;
  425. if (!lang || lang.length < 2) {
  426. if (settings.debug) debug('No language supplied. Pulling it from the browser ...');
  427. lang = (navigator.languages && navigator.languages.length > 0) ? navigator.languages[0]
  428. : (navigator.language || navigator.userLanguage /* IE */ || 'en');
  429. if (settings.debug) debug('Language from browser: ' + lang);
  430. }
  431. lang = lang.toLowerCase();
  432. lang = lang.replace(/-/,"_"); // some browsers report language as en-US instead of en_US
  433. if (lang.length > 3) {
  434. lang = lang.substring(0, 3) + lang.substring(3).toUpperCase();
  435. }
  436. return lang;
  437. };
  438. /** Unescape unicode chars ('\u00e3') */
  439. function unescapeUnicode(str) {
  440. // unescape unicode codes
  441. var codes = [];
  442. var code = parseInt(str.substr(2), 16);
  443. if (code >= 0 && code < Math.pow(2, 16)) {
  444. codes.push(code);
  445. }
  446. // convert codes to text
  447. return codes.reduce(function (acc, val) { return acc + String.fromCharCode(val); }, '');
  448. }
  449. }) (jQuery);