Skip to content

Commit

Permalink
добавление локалей
Browse files Browse the repository at this point in the history
  • Loading branch information
Katochimoto committed Nov 15, 2013
1 parent 8a7bf6d commit e51a3a8
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 58 deletions.
15 changes: 13 additions & 2 deletions lib/locale.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
(function(strftime) {

strftime.locale = {
var locales = {};

strftime.setLocale = function(name, data) {
locales[name] = data;
};

strftime.getLocale = function(name) {
name = name || 'en';
return locales[name] || {};
};

strftime.setLocale('en', {
'a': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
'A': ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
'b': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
Expand All @@ -27,7 +38,7 @@
'Date_dfY': '%-d %#f %Y',
'Date_dB_in_HM': '%#B %-d at %-H:%M',
'Date_df': '%-d %#f'
};
});

}(strftime));

4 changes: 2 additions & 2 deletions lib/strftime.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @returns {String|Null}
*/
/*jshint -W079 */
var strftime = function(format, date, utc) {
var strftime = function(format, date, utc, localeName) {
if (typeof date === 'undefined') {
date = new Date();
}
Expand All @@ -28,5 +28,5 @@ var strftime = function(format, date, utc) {
date.setTime(date.getTime() + date.getTimezoneOffset() * 60000);
}

return strftime.format(format, date);
return strftime.format(format, date, localeName);
};
49 changes: 25 additions & 24 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
(function(strftime) {

var locale = strftime.locale;
var currentLocale;
var regAgregat = /%(Date_[a-zA-Z0-9_]+|([#\^]?)[v]|[cDFrRTxX])/g;
var regAgregatSearch = /%(Date_[a-zA-Z0-9_]+|[#\^]?[v]|[cDFrRTxX])/;
var regSpec = /%(([#\^!~]{0,2})[aAbBfh]|([0\-_]?)[CdegHIjmMSVWyl]|[GnpPtuUwYzZs%])/g;

var specifiers = {
'a': function(d, mode) {
return toLetterCase(locale.a[d.getDay()], mode);
return toLetterCase(currentLocale.a[d.getDay()], mode);
},
'A': function(d, mode) {
return toLetterCase(locale.A[d.getDay()], mode);
return toLetterCase(currentLocale.A[d.getDay()], mode);
},
'b': function(d, mode, numPad, genitive) {
return toLetterCase(locale[genitive ? 'bg' : 'b'][d.getMonth()], mode);
return toLetterCase(currentLocale[genitive ? 'bg' : 'b'][d.getMonth()], mode);
},
'h': function(d, mode, numPad, genitive) {
return specifiers.b(d, mode, numPad, genitive);
},
'f': function(d, mode, numPad, genitive) {
return toLetterCase(locale[genitive ? 'fg' : 'f'][d.getMonth()], mode);
return toLetterCase(currentLocale[genitive ? 'fg' : 'f'][d.getMonth()], mode);
},
'B': function(d, mode, numPad, genitive) {
return toLetterCase(locale[genitive ? 'Bg' : 'B'][d.getMonth()], mode);
return toLetterCase(currentLocale[genitive ? 'Bg' : 'B'][d.getMonth()], mode);
},
'c': function() {
return locale.c;
return currentLocale.c;
},
'C': function(d, mode, numPad) {
return pad(parseInt(d.getFullYear() / 100, 10), numPad, 0);
Expand Down Expand Up @@ -89,14 +89,14 @@
},
'p': function(d) {
var p = d.getHours() >= 12 ? 1 : 0;
return String(locale.P[p]).toUpperCase();
return String(currentLocale.P[p]).toUpperCase();
},
'P': function(d) {
var p = d.getHours() >= 12 ? 1 : 0;
return String(locale.P[p]);
return String(currentLocale.P[p]);
},
'r': function() {
return locale.r;
return currentLocale.r;
},
'R': function() {
return '%H:%M';
Expand Down Expand Up @@ -141,10 +141,10 @@
return pad(woy, numPad, 0, 10);
},
'x': function() {
return locale.x;
return currentLocale.x;
},
'X': function() {
return locale.X;
return currentLocale.X;
},
'y': function(d, mode, numPad) {
return pad(d.getFullYear() % 100, numPad, 0);
Expand Down Expand Up @@ -177,8 +177,8 @@
var time = now.getTime() + now.getTimezoneOffset() * 60000;
var diff = Math.ceil((td - time) / 60000 / 60 / 24) + 1;

if (locale.day[diff]) {
return toLetterCase(locale.day[diff], mode);
if (currentLocale.day[diff]) {
return toLetterCase(currentLocale.day[diff], mode);

} else {
return '%d %' + mode + 'B';
Expand All @@ -189,34 +189,34 @@
return '%Y-%m-%dT%H:%M:%S';
},
'Date_dBY_year_in_HM': function() {
return locale.Date_dBY_year_in_HM;
return currentLocale.Date_dBY_year_in_HM;
},
'Date_dBY_year': function() {
return locale.Date_dBY_year;
return currentLocale.Date_dBY_year;
},
'Date_dBY': function() {
return locale.Date_dBY;
return currentLocale.Date_dBY;
},
'Date_dBA': function() {
return locale.Date_dBA;
return currentLocale.Date_dBA;
},
'Date_AdBY': function() {
return locale.Date_AdBY;
return currentLocale.Date_AdBY;
},
'Date_df_in_HM': function() {
return locale.Date_df_in_HM;
return currentLocale.Date_df_in_HM;
},
'Date_dfY': function() {
return locale.Date_dfY;
return currentLocale.Date_dfY;
},
'Date_dB_in_HM': function() {
return locale.Date_dB_in_HM;
return currentLocale.Date_dB_in_HM;
},
'Date_dmY__dot': function() {
return '%d.%m.%Y';
},
'Date_df': function() {
return locale.Date_df;
return currentLocale.Date_df;
},
'Date_FT': function() {
return '%F %T';
Expand All @@ -231,8 +231,9 @@
* @param {Date} date
* @returns {String}
*/
strftime.format = function(format, date) {
strftime.format = function(format, date, localeName) {
formatTransform.date = date;
currentLocale = strftime.getLocale(localeName);

var loop = 5;
while (regAgregatSearch.test(format) && loop) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "strftime",
"version": "0.0.1",
"version": "1.0.1",
"description": "Date formatting function",
"main": "strftime.js",
"keywords": [
Expand Down
68 changes: 40 additions & 28 deletions strftime.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @returns {String|Null}
*/
/*jshint -W079 */
var strftime = function(format, date, utc) {
var strftime = function(format, date, utc, localeName) {
if (typeof date === 'undefined') {
date = new Date();
}
Expand All @@ -32,7 +32,7 @@ var strftime = function(format, date, utc) {
date.setTime(date.getTime() + date.getTimezoneOffset() * 60000);
}

return strftime.format(format, date);
return strftime.format(format, date, localeName);
};


Expand All @@ -52,7 +52,18 @@ var strftime = function(format, date, utc) {

(function(strftime) {

strftime.locale = {
var locales = {};

strftime.setLocale = function(name, data) {
locales[name] = data;
};

strftime.getLocale = function(name) {
name = name || 'en';
return locales[name] || {};
};

strftime.setLocale('en', {
'a': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
'A': ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
'b': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
Expand All @@ -79,39 +90,39 @@ var strftime = function(format, date, utc) {
'Date_dfY': '%-d %#f %Y',
'Date_dB_in_HM': '%#B %-d at %-H:%M',
'Date_df': '%-d %#f'
};
});

}(strftime));


(function(strftime) {

var locale = strftime.locale;
var currentLocale;
var regAgregat = /%(Date_[a-zA-Z0-9_]+|([#\^]?)[v]|[cDFrRTxX])/g;
var regAgregatSearch = /%(Date_[a-zA-Z0-9_]+|[#\^]?[v]|[cDFrRTxX])/;
var regSpec = /%(([#\^!~]{0,2})[aAbBfh]|([0\-_]?)[CdegHIjmMSVWyl]|[GnpPtuUwYzZs%])/g;

var specifiers = {
'a': function(d, mode) {
return toLetterCase(locale.a[d.getDay()], mode);
return toLetterCase(currentLocale.a[d.getDay()], mode);
},
'A': function(d, mode) {
return toLetterCase(locale.A[d.getDay()], mode);
return toLetterCase(currentLocale.A[d.getDay()], mode);
},
'b': function(d, mode, numPad, genitive) {
return toLetterCase(locale[genitive ? 'bg' : 'b'][d.getMonth()], mode);
return toLetterCase(currentLocale[genitive ? 'bg' : 'b'][d.getMonth()], mode);
},
'h': function(d, mode, numPad, genitive) {
return specifiers.b(d, mode, numPad, genitive);
},
'f': function(d, mode, numPad, genitive) {
return toLetterCase(locale[genitive ? 'fg' : 'f'][d.getMonth()], mode);
return toLetterCase(currentLocale[genitive ? 'fg' : 'f'][d.getMonth()], mode);
},
'B': function(d, mode, numPad, genitive) {
return toLetterCase(locale[genitive ? 'Bg' : 'B'][d.getMonth()], mode);
return toLetterCase(currentLocale[genitive ? 'Bg' : 'B'][d.getMonth()], mode);
},
'c': function() {
return locale.c;
return currentLocale.c;
},
'C': function(d, mode, numPad) {
return pad(parseInt(d.getFullYear() / 100, 10), numPad, 0);
Expand Down Expand Up @@ -175,14 +186,14 @@ var strftime = function(format, date, utc) {
},
'p': function(d) {
var p = d.getHours() >= 12 ? 1 : 0;
return String(locale.P[p]).toUpperCase();
return String(currentLocale.P[p]).toUpperCase();
},
'P': function(d) {
var p = d.getHours() >= 12 ? 1 : 0;
return String(locale.P[p]);
return String(currentLocale.P[p]);
},
'r': function() {
return locale.r;
return currentLocale.r;
},
'R': function() {
return '%H:%M';
Expand Down Expand Up @@ -227,10 +238,10 @@ var strftime = function(format, date, utc) {
return pad(woy, numPad, 0, 10);
},
'x': function() {
return locale.x;
return currentLocale.x;
},
'X': function() {
return locale.X;
return currentLocale.X;
},
'y': function(d, mode, numPad) {
return pad(d.getFullYear() % 100, numPad, 0);
Expand Down Expand Up @@ -263,8 +274,8 @@ var strftime = function(format, date, utc) {
var time = now.getTime() + now.getTimezoneOffset() * 60000;
var diff = Math.ceil((td - time) / 60000 / 60 / 24) + 1;

if (locale.day[diff]) {
return toLetterCase(locale.day[diff], mode);
if (currentLocale.day[diff]) {
return toLetterCase(currentLocale.day[diff], mode);

} else {
return '%d %' + mode + 'B';
Expand All @@ -275,34 +286,34 @@ var strftime = function(format, date, utc) {
return '%Y-%m-%dT%H:%M:%S';
},
'Date_dBY_year_in_HM': function() {
return locale.Date_dBY_year_in_HM;
return currentLocale.Date_dBY_year_in_HM;
},
'Date_dBY_year': function() {
return locale.Date_dBY_year;
return currentLocale.Date_dBY_year;
},
'Date_dBY': function() {
return locale.Date_dBY;
return currentLocale.Date_dBY;
},
'Date_dBA': function() {
return locale.Date_dBA;
return currentLocale.Date_dBA;
},
'Date_AdBY': function() {
return locale.Date_AdBY;
return currentLocale.Date_AdBY;
},
'Date_df_in_HM': function() {
return locale.Date_df_in_HM;
return currentLocale.Date_df_in_HM;
},
'Date_dfY': function() {
return locale.Date_dfY;
return currentLocale.Date_dfY;
},
'Date_dB_in_HM': function() {
return locale.Date_dB_in_HM;
return currentLocale.Date_dB_in_HM;
},
'Date_dmY__dot': function() {
return '%d.%m.%Y';
},
'Date_df': function() {
return locale.Date_df;
return currentLocale.Date_df;
},
'Date_FT': function() {
return '%F %T';
Expand All @@ -317,8 +328,9 @@ var strftime = function(format, date, utc) {
* @param {Date} date
* @returns {String}
*/
strftime.format = function(format, date) {
strftime.format = function(format, date, localeName) {
formatTransform.date = date;
currentLocale = strftime.getLocale(localeName);

var loop = 5;
while (regAgregatSearch.test(format) && loop) {
Expand Down
Loading

0 comments on commit e51a3a8

Please sign in to comment.