locale.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.date.buddhist.locale"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.date.buddhist.locale"] = true;
  8. dojo.provide("dojox.date.buddhist.locale");
  9. dojo.experimental("dojox.date.buddhist.locale");
  10. dojo.require("dojox.date.buddhist.Date");
  11. dojo.require("dojo.regexp");
  12. dojo.require("dojo.string");
  13. dojo.require("dojo.i18n");
  14. dojo.requireLocalization("dojo.cldr", "buddhist", null, "ROOT,ar,be,da,de,el,en,en-gb,en-hk,es,eu,fi,fr,gsw,hr,hu,id,in,it,ja,ko,lt,ms,nb,nl,no,pl,pt,pt-pt,ro,ru,sr,sv,th,tr,vi,zh,zh-hant");
  15. (function(){
  16. // Format a pattern without literals
  17. function formatPattern(dateObject, bundle, locale, fullYear, pattern){
  18. return pattern.replace(/([a-z])\1*/ig, function(match){
  19. var s, pad;
  20. var c = match.charAt(0);
  21. var l = match.length;
  22. var widthList = ["abbr", "wide", "narrow"];
  23. switch(c){
  24. case 'G':
  25. s = bundle["eraAbbr"][0];
  26. break;
  27. case 'y':
  28. s = String(dateObject.getFullYear());
  29. break;
  30. case 'M':
  31. var m = dateObject.getMonth();
  32. if(l<3){
  33. s = m+1; pad = true;
  34. }else{
  35. var propM = ["months", "format", widthList[l-3]].join("-");
  36. s = bundle[propM][m];
  37. }
  38. break;
  39. case 'd':
  40. s = dateObject.getDate(true); pad = true;
  41. break;
  42. case 'E':
  43. var d = dateObject.getDay();
  44. if(l<3){
  45. s = d+1; pad = true;
  46. }else{
  47. var propD = ["days", "format", widthList[l-3]].join("-");
  48. s = bundle[propD][d];
  49. }
  50. break;
  51. case 'a':
  52. var timePeriod = (dateObject.getHours() < 12) ? 'am' : 'pm';
  53. s = bundle['dayPeriods-format-wide-' + timePeriod];
  54. break;
  55. case 'h':
  56. case 'H':
  57. case 'K':
  58. case 'k':
  59. var h = dateObject.getHours();
  60. switch (c){
  61. case 'h': // 1-12
  62. s = (h % 12) || 12;
  63. break;
  64. case 'H': // 0-23
  65. s = h;
  66. break;
  67. case 'K': // 0-11
  68. s = (h % 12);
  69. break;
  70. case 'k': // 1-24
  71. s = h || 24;
  72. break;
  73. }
  74. pad = true;
  75. break;
  76. case 'm':
  77. s = dateObject.getMinutes(); pad = true;
  78. break;
  79. case 's':
  80. s = dateObject.getSeconds(); pad = true;
  81. break;
  82. case 'S':
  83. s = Math.round(dateObject.getMilliseconds() * Math.pow(10, l-3)); pad = true;
  84. break;
  85. case 'z':
  86. // We only have one timezone to offer; the one from the browser
  87. s = dojo.date.getTimezoneName(dateObject.toGregorian());
  88. if(s){ break; }
  89. l = 4;
  90. // fallthrough... use GMT if tz not available
  91. case 'Z':
  92. var offset = dateObject.toGregorian().getTimezoneOffset();
  93. var tz = [
  94. (offset <= 0 ? "+" : "-"),
  95. dojo.string.pad(Math.floor(Math.abs(offset) / 60), 2),
  96. dojo.string.pad(Math.abs(offset) % 60, 2)
  97. ];
  98. if(l == 4){
  99. tz.splice(0, 0, "GMT");
  100. tz.splice(3, 0, ":");
  101. }
  102. s = tz.join("");
  103. break;
  104. default:
  105. throw new Error("dojox.date.buddhist.locale.formatPattern: invalid pattern char: "+pattern);
  106. }
  107. if(pad){ s = dojo.string.pad(s, l); }
  108. return s;
  109. });
  110. }
  111. dojox.date.buddhist.locale.format = function(/*buddhist.Date*/dateObject, /*object?*/options){
  112. // based on and similar to dojo.date.locale.format
  113. //summary:
  114. // Format a Date object as a String, using settings.
  115. options = options || {};
  116. var locale = dojo.i18n.normalizeLocale(options.locale);
  117. var formatLength = options.formatLength || 'short';
  118. var bundle = dojox.date.buddhist.locale._getBuddhistBundle(locale);
  119. var str = [];
  120. var sauce = dojo.hitch(this, formatPattern, dateObject, bundle, locale, options.fullYear);
  121. if(options.selector == "year"){
  122. var year = dateObject.getFullYear();
  123. return year;
  124. }
  125. if(options.selector != "time"){
  126. var datePattern = options.datePattern || bundle["dateFormat-"+formatLength];
  127. if(datePattern){str.push(_processPattern(datePattern, sauce));}
  128. }
  129. if(options.selector != "date"){
  130. var timePattern = options.timePattern || bundle["timeFormat-"+formatLength];
  131. if(timePattern){str.push(_processPattern(timePattern, sauce));}
  132. }
  133. var result = str.join(" "); //TODO: use locale-specific pattern to assemble date + time
  134. return result; // String
  135. };
  136. dojox.date.buddhist.locale.regexp = function(/*object?*/options){
  137. // based on and similar to dojo.date.locale.regexp
  138. // summary:
  139. // Builds the regular needed to parse a buddhist.Date
  140. return dojox.date.buddhist.locale._parseInfo(options).regexp; // String
  141. };
  142. dojox.date.buddhist.locale._parseInfo = function(/*oblect?*/options){
  143. /* based on and similar to dojo.date.locale._parseInfo */
  144. options = options || {};
  145. var locale = dojo.i18n.normalizeLocale(options.locale);
  146. var bundle = dojox.date.buddhist.locale._getBuddhistBundle(locale);
  147. var formatLength = options.formatLength || 'short';
  148. var datePattern = options.datePattern || bundle["dateFormat-" + formatLength];
  149. var timePattern = options.timePattern || bundle["timeFormat-" + formatLength];
  150. var pattern;
  151. if(options.selector == 'date'){
  152. pattern = datePattern;
  153. }else if(options.selector == 'time'){
  154. pattern = timePattern;
  155. }else{
  156. pattern = (typeof (timePattern) == "undefined") ? datePattern : datePattern + ' ' + timePattern;
  157. }
  158. var tokens = [];
  159. var re = _processPattern(pattern, dojo.hitch(this, _buildDateTimeRE, tokens, bundle, options));
  160. return {regexp: re, tokens: tokens, bundle: bundle};
  161. };
  162. dojox.date.buddhist.locale.parse= function(/*String*/value, /*object?*/options){
  163. // based on and similar to dojo.date.locale.parse
  164. // summary: This function parse string date value according to options
  165. value = value.replace(/[\u200E\u200F\u202A-\u202E]/g, ""); //remove special chars
  166. if(!options){options={};}
  167. var info = dojox.date.buddhist.locale._parseInfo(options);
  168. var tokens = info.tokens, bundle = info.bundle;
  169. var re = new RegExp("^" + info.regexp + "$");
  170. var match = re.exec(value);
  171. var locale = dojo.i18n.normalizeLocale(options.locale);
  172. if(!match){ return null; } // null
  173. var date, date1;
  174. var result = [2513,0,1,0,0,0,0]; // buddhist date for [1970,0,1,0,0,0,0] used in gregorian locale
  175. var amPm = "";
  176. var mLength = 0;
  177. var widthList = ["abbr", "wide", "narrow"];
  178. var valid = dojo.every(match, function(v, i){
  179. if(!i){return true;}
  180. var token=tokens[i-1];
  181. var l=token.length;
  182. switch(token.charAt(0)){
  183. case 'y':
  184. result[0] = Number(v);
  185. break;
  186. case 'M':
  187. if(l>2){
  188. var months = bundle['months-format-' + widthList[l-3]].concat();
  189. if(!options.strict){
  190. //Tolerate abbreviating period in month part
  191. //Case-insensitive comparison
  192. v = v.replace(".","").toLowerCase();
  193. months = dojo.map(months, function(s){ return s ? s.replace(".","").toLowerCase() : s; } );
  194. }
  195. v = dojo.indexOf(months, v);
  196. if(v == -1){
  197. return false;
  198. }
  199. mLength = l;
  200. }else{
  201. v--;
  202. }
  203. result[1] = Number(v);
  204. break;
  205. case 'D':
  206. result[1] = 0;
  207. // fallthrough...
  208. case 'd':
  209. result[2] = Number(v);
  210. break;
  211. case 'a': //am/pm
  212. var am = options.am || bundle['dayPeriods-format-wide-am'],
  213. pm = options.pm || bundle['dayPeriods-format-wide-pm'];
  214. if(!options.strict){
  215. var period = /\./g;
  216. v = v.replace(period,'').toLowerCase();
  217. am = am.replace(period,'').toLowerCase();
  218. pm = pm.replace(period,'').toLowerCase();
  219. }
  220. if(options.strict && v != am && v != pm){
  221. return false;
  222. }
  223. // we might not have seen the hours field yet, so store the state and apply hour change later
  224. amPm = (v == pm) ? 'p' : (v == am) ? 'a' : '';
  225. break;
  226. case 'K': //hour (1-24)
  227. if(v == 24){ v = 0; }
  228. // fallthrough...
  229. case 'h': //hour (1-12)
  230. case 'H': //hour (0-23)
  231. case 'k': //hour (0-11)
  232. //in the 12-hour case, adjusting for am/pm requires the 'a' part
  233. //which could come before or after the hour, so we will adjust later
  234. result[3] = Number(v);
  235. break;
  236. case 'm': //minutes
  237. result[4] = Number(v);
  238. break;
  239. case 's': //seconds
  240. result[5] = Number(v);
  241. break;
  242. case 'S': //milliseconds
  243. result[6] = Number(v);
  244. }
  245. return true;
  246. });
  247. var hours = +result[3];
  248. if(amPm === 'p' && hours < 12){
  249. result[3] = hours + 12; //e.g., 3pm -> 15
  250. }else if(amPm === 'a' && hours == 12){
  251. result[3] = 0; //12am -> 0
  252. }
  253. var dateObject = new dojox.date.buddhist.Date(result[0], result[1], result[2], result[3], result[4], result[5], result[6]);
  254. return dateObject;
  255. };
  256. function _processPattern(pattern, applyPattern, applyLiteral, applyAll){
  257. //summary: Process a pattern with literals in it
  258. // Break up on single quotes, treat every other one as a literal, except '' which becomes '
  259. var identity = function(x){return x;};
  260. applyPattern = applyPattern || identity;
  261. applyLiteral = applyLiteral || identity;
  262. applyAll = applyAll || identity;
  263. //split on single quotes (which escape literals in date format strings)
  264. //but preserve escaped single quotes (e.g., o''clock)
  265. var chunks = pattern.match(/(''|[^'])+/g);
  266. var literal = pattern.charAt(0) == "'";
  267. dojo.forEach(chunks, function(chunk, i){
  268. if(!chunk){
  269. chunks[i]='';
  270. }else{
  271. chunks[i]=(literal ? applyLiteral : applyPattern)(chunk);
  272. literal = !literal;
  273. }
  274. });
  275. return applyAll(chunks.join(''));
  276. }
  277. function _buildDateTimeRE (tokens, bundle, options, pattern){
  278. // based on and similar to dojo.date.locale._buildDateTimeRE
  279. //
  280. pattern = dojo.regexp.escapeString(pattern);
  281. var locale = dojo.i18n.normalizeLocale(options.locale);
  282. return pattern.replace(/([a-z])\1*/ig, function(match){
  283. // Build a simple regexp. Avoid captures, which would ruin the tokens list
  284. var s;
  285. var c = match.charAt(0);
  286. var l = match.length;
  287. var p2 = '', p3 = '';
  288. if(options.strict){
  289. if(l > 1){ p2 = '0' + '{'+(l-1)+'}'; }
  290. if(l > 2){ p3 = '0' + '{'+(l-2)+'}'; }
  291. }else{
  292. p2 = '0?'; p3 = '0{0,2}';
  293. }
  294. switch(c){
  295. case 'y':
  296. s = '\\d+';
  297. break;
  298. case 'M':
  299. s = (l>2) ? '\\S+' : p2+'[1-9]|1[0-2]';
  300. break;
  301. case 'd':
  302. s = '[12]\\d|'+p2+'[1-9]|3[01]';
  303. break;
  304. case 'E':
  305. s = '\\S+';
  306. break;
  307. case 'h': //hour (1-12)
  308. s = p2+'[1-9]|1[0-2]';
  309. break;
  310. case 'k': //hour (0-11)
  311. s = p2+'\\d|1[01]';
  312. break;
  313. case 'H': //hour (0-23)
  314. s = p2+'\\d|1\\d|2[0-3]';
  315. break;
  316. case 'K': //hour (1-24)
  317. s = p2+'[1-9]|1\\d|2[0-4]';
  318. break;
  319. case 'm':
  320. case 's':
  321. s = p2+'\\d|[0-5]\\d';
  322. break;
  323. case 'S':
  324. s = '\\d{'+l+'}';
  325. break;
  326. case 'a':
  327. var am = options.am || bundle['dayPeriods-format-wide-am'],
  328. pm = options.pm || bundle['dayPeriods-format-wide-pm'];
  329. if(options.strict){
  330. s = am + '|' + pm;
  331. }else{
  332. s = am + '|' + pm;
  333. if(am != am.toLowerCase()){ s += '|' + am.toLowerCase(); }
  334. if(pm != pm.toLowerCase()){ s += '|' + pm.toLowerCase(); }
  335. }
  336. break;
  337. default:
  338. s = ".*";
  339. }
  340. if(tokens){ tokens.push(match); }
  341. return "(" + s + ")"; // add capture
  342. }).replace(/[\xa0 ]/g, "[\\s\\xa0]"); // normalize whitespace. Need explicit handling of \xa0 for IE. */
  343. }
  344. })();
  345. (function(){
  346. var _customFormats = [];
  347. dojox.date.buddhist.locale.addCustomFormats = function(/*String*/packageName, /*String*/bundleName){
  348. // summary:
  349. // Add a reference to a bundle containing localized custom formats to be
  350. // used by date/time formatting and parsing routines.
  351. _customFormats.push({pkg:packageName,name:bundleName});
  352. };
  353. dojox.date.buddhist.locale._getBuddhistBundle = function(/*String*/locale){
  354. var buddhist = {};
  355. dojo.forEach(_customFormats, function(desc){
  356. var bundle = dojo.i18n.getLocalization(desc.pkg, desc.name, locale);
  357. buddhist = dojo.mixin(buddhist, bundle);
  358. }, this);
  359. return buddhist; /*Object*/
  360. };
  361. })();
  362. dojox.date.buddhist.locale.addCustomFormats("dojo.cldr","buddhist");
  363. dojox.date.buddhist.locale.getNames = function(/*String*/item, /*String*/type, /*String?*/context, /*String?*/locale, /*buddhist Date Object?*/date){
  364. // summary:
  365. // Used to get localized strings from dojo.cldr for day or month names.
  366. var label;
  367. var lookup = dojox.date.buddhist.locale._getBuddhistBundle(locale);
  368. var props = [item, context, type];
  369. if(context == 'standAlone'){
  370. var key = props.join('-');
  371. label = lookup[key];
  372. // Fall back to 'format' flavor of name
  373. if(label[0] == 1){ label = undefined; } // kludge, in the absence of real aliasing support in dojo.cldr
  374. }
  375. props[1] = 'format';
  376. // return by copy so changes won't be made accidentally to the in-memory model
  377. return (label || lookup[props.join('-')]).concat(); /*Array*/
  378. };
  379. }