locale.js 12 KB

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