From 7307ad88c60ddba76678b401ef5f187086ba881d Mon Sep 17 00:00:00 2001 From: Tim Wood Date: Wed, 14 Sep 2011 12:52:55 -0700 Subject: [PATCH] adding _date.lang and test cases _date.lang will allow developers to easily store and switch languages based on keys --- test/date.js | 118 ++++++++++++++++++++++++++++++++++++++++++++- underscore.date.js | 79 ++++++++++++++++++------------ 2 files changed, 165 insertions(+), 32 deletions(-) diff --git a/test/date.js b/test/date.js index 9041994f4..66fd249cf 100755 --- a/test/date.js +++ b/test/date.js @@ -101,7 +101,7 @@ test("format", 15, function() { ['m mm', '25 25'], ['s ss', '50 50'], ['a A', 'pm PM'], - ['z zz', 'PST Pacific Standard Time'], + ['z zz', 'PST PST'], ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45th day of the year'] ], b = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), @@ -243,7 +243,7 @@ test("format", 9, function() { ], b = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), i; - + _date.months = ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"]; _date.monthsShort = ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"]; _date.weekdays = ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"]; @@ -323,3 +323,117 @@ test("from future past", 2, function() { _date.relativeTime = backup; }); + + +module('lang'); + +test("format", 48, function() { + var date = _date(new Date(2010, 1, 14, 15, 25, 50, 125)), + en = [ + ['dddd, MMMM Do YYYY, h:mm:ss a', 'Sunday, February 14th 2010, 3:25:50 pm'], + ['ddd, hA', 'Sun, 3PM'], + ['M Mo MM MMMM MMM', '2 2nd 02 February Feb'], + ['D Do DD', '14 14th 14'], + ['d do dddd ddd', '0 0th Sunday Sun'], + ['DDD DDDo DDDD', '45 45th 045'], + ['w wo ww', '8 8th 08'], + ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45th day of the year'] + ], + es = [ + ['dddd, MMMM Do YYYY, h:mm:ss a', 'domingo, febrero 14o 2010, 3:25:50 pm'], + ['ddd, hA', 'dom, 3PM'], + ['M Mo MM MMMM MMM', '2 2o 02 febrero feb'], + ['D Do DD', '14 14o 14'], + ['d do dddd ddd', '0 0o domingo dom'], + ['DDD DDDo DDDD', '45 45o 045'], + ['w wo ww', '8 8o 08'], + ['t\\he DDDo \\d\\ay of t\\he ye\\ar', 'the 45o day of the year'] + ], + esFrom = [ + [{s:-30}, false, "seconds! testing a", "future"], + [{s:30}, false, "seconds! testing b", "past"], + [{s:30}, true, "seconds!", "seconds"], + [{s:60}, true, "a minute!", "minute"], + [{m:5}, true, "5 minutes!", "minutes"], + [{h:1}, true, "an hour!", "hour"], + [{h:5}, true, "5 hours!", "hours"], + [{d:1}, true, "a day!", "day"], + [{d:5}, true, "5 days!", "days"], + [{M:1}, true, "a month!", "month"], + [{M:5}, true, "5 months!", "months"], + [{y:1}, true, "a year!", "year"], + [{y:5}, true, "5 years!", "years"] + ], + enFrom = [ + [{s:-30}, false, "in seconds", "future"], + [{s:30}, false, "seconds ago", "past"], + [{s:30}, true, "seconds", "seconds"], + [{s:60}, true, "a minute", "minute"], + [{m:5}, true, "5 minutes", "minutes"], + [{h:1}, true, "an hour", "hour"], + [{h:5}, true, "5 hours", "hours"], + [{d:1}, true, "a day", "day"], + [{d:5}, true, "5 days", "days"], + [{M:1}, true, "a month", "month"], + [{M:5}, true, "5 months", "months"], + [{y:1}, true, "a year", "year"], + [{y:5}, true, "5 years", "years"] + ], + i; + + function testLang(formatArray, fromArray) { + var start = [2007, 1, 28]; + for (i = 0; i < formatArray.length; i++) { + equal(date.format(formatArray[i][0]), formatArray[i][1], formatArray[i][0] + ' ---> ' + formatArray[i][1]); + } + + for (i = 0; i < formatArray.length; i++) { + equal(_date(start).from(_date(start).add(fromArray[i][0]), fromArray[i][1]), fromArray[i][2], fromArray[i][3]); + } + } + + // switch to es + _date.lang('es', { + months : ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"], + monthsShort : ["ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic"], + weekdays : ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"], + weekdaysShort : ["dom", "lun", "mar", "mié", "jue", "vie", "sáb"], + ordinal : function() { + return 'o'; + }, + relativeTime : { + future: "%s testing a", + past: "%s testing b", + s: "seconds!", + m: "a minute!", + mm: "%d minutes!", + h: "an hour!", + hh: "%d hours!", + d: "a day!", + dd: "%d days!", + M: "a month!", + MM: "%d months!", + y: "a year!", + yy: "%d years!" + } + }); + + + // test es + testLang(es, esFrom); + + // switch to en + _date.lang('en'); + + // test en + testLang(en, enFrom); + + // switch to es + _date.lang('es'); + + // test es + testLang(es, esFrom); + + // switch back to en to prevent other tests from failing + _date.lang('en'); +}); \ No newline at end of file diff --git a/underscore.date.js b/underscore.date.js index dfcf2243d..71823aff8 100644 --- a/underscore.date.js +++ b/underscore.date.js @@ -8,7 +8,9 @@ (function (undefined) { var _date, - round = Math.round; + round = Math.round, + languages = {}, + paramsToParse = 'months|monthsShort|weekdays|weekdaysShort|relativeTime|ordinal'.split('|'); // left zero fill a number // see http://jsperf.com/left-zero-filling for performance comparison @@ -156,12 +158,12 @@ case 'ss' : return leftZeroFill(currentSeconds, 2); // TIMEZONE - case 'z' : - return replaceFunction('zz').replace(nonuppercaseLetters, ''); case 'zz' : + // depreciating 'zz' fall through to 'z' + case 'z' : a = currentString.indexOf('('); if (a > -1) { - return currentString.slice(a + 1, currentString.indexOf(')')); + return currentString.slice(a + 1, currentString.indexOf(')')).replace(nonuppercaseLetters, ''); } return currentString.slice(currentString.indexOf(':')).replace(nonuppercaseLetters, ''); // DEFAULT @@ -308,32 +310,49 @@ return new UnderscoreDate(input, format); }; - _date.months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; - _date.monthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; - _date.weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; - _date.weekdaysShort = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; - _date.relativeTime = { - future: "in %s", - past: "%s ago", - s: "seconds", - m: "a minute", - mm: "%d minutes", - h: "an hour", - hh: "%d hours", - d: "a day", - dd: "%d days", - M: "a month", - MM: "%d months", - y: "a year", - yy: "%d years" - }; - _date.ordinal = function (number) { - var b = number % 10; - return (~~ (number % 100 / 10) === 1) ? 'th' : - (b === 1) ? 'st' : - (b === 2) ? 'nd' : - (b === 3) ? 'rd' : 'th'; - }; + // language switching and caching + _date.lang = function (key, values) { + var i, param; + if (values) { + languages[key] = values; + } + if (languages[key]) { + for (i = 0; i < paramsToParse.length; i++) { + param = paramsToParse[i]; + _date[param] = languages[key][param] || _date[param]; + } + } + } + + // set default language + _date.lang('en', { + months : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], + monthsShort : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], + weekdays : ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], + weekdaysShort : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], + relativeTime : { + future: "in %s", + past: "%s ago", + s: "seconds", + m: "a minute", + mm: "%d minutes", + h: "an hour", + hh: "%d hours", + d: "a day", + dd: "%d days", + M: "a month", + MM: "%d months", + y: "a year", + yy: "%d years" + }, + ordinal : function (number) { + var b = number % 10; + return (~~ (number % 100 / 10) === 1) ? 'th' : + (b === 1) ? 'st' : + (b === 2) ? 'nd' : + (b === 3) ? 'rd' : 'th'; + } + }); // convert any input to milliseconds function makeInputMilliseconds(input) { -- 2.47.3