closes #242, internationalized date handling
This commit is contained in:
parent
7f40d696a4
commit
451be4b3a3
|
@ -342,9 +342,7 @@ CSL.ns = "http://purl.org/net/xbiblio/csl";
|
||||||
CSL.init = function() {
|
CSL.init = function() {
|
||||||
if(!CSL._xmlLang) {
|
if(!CSL._xmlLang) {
|
||||||
// get XML lang
|
// get XML lang
|
||||||
var localeService = Components.classes['@mozilla.org/intl/nslocaleservice;1'].
|
CSL._xmlLang = Scholar.locale;
|
||||||
getService(Components.interfaces.nsILocaleService);
|
|
||||||
CSL._xmlLang = localeService.getLocaleComponentForUserAgent();
|
|
||||||
|
|
||||||
// read locales.xml from directory
|
// read locales.xml from directory
|
||||||
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
|
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
|
||||||
|
@ -1001,7 +999,7 @@ CSL.prototype._formatDate = function(element, date, format) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(child.name == "month") {
|
} else if(child.name == "month") {
|
||||||
if(date.month) {
|
if(date.month != undefined) {
|
||||||
if(format == "compare") {
|
if(format == "compare") {
|
||||||
string = this._lpad(date.month+1, "0", 2);
|
string = this._lpad(date.month+1, "0", 2);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -39,6 +39,7 @@ var Scholar = new function(){
|
||||||
// Public properties
|
// Public properties
|
||||||
this.version;
|
this.version;
|
||||||
this.platform;
|
this.platform;
|
||||||
|
this.locale;
|
||||||
this.isMac;
|
this.isMac;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -77,6 +78,11 @@ var Scholar = new function(){
|
||||||
this.platform = win.navigator.platform;
|
this.platform = win.navigator.platform;
|
||||||
this.isMac = (this.platform.substr(0, 3) == "Mac");
|
this.isMac = (this.platform.substr(0, 3) == "Mac");
|
||||||
|
|
||||||
|
// Locale
|
||||||
|
var localeService = Components.classes['@mozilla.org/intl/nslocaleservice;1'].
|
||||||
|
getService(Components.interfaces.nsILocaleService);
|
||||||
|
this.locale = localeService.getLocaleComponentForUserAgent();
|
||||||
|
|
||||||
// Load in the localization stringbundle for use by getString(name)
|
// Load in the localization stringbundle for use by getString(name)
|
||||||
var src = 'chrome://scholar/locale/scholar.properties';
|
var src = 'chrome://scholar/locale/scholar.properties';
|
||||||
var localeService =
|
var localeService =
|
||||||
|
@ -645,6 +651,10 @@ Scholar.Date = new function(){
|
||||||
* part: anything that does not fall under any of the above categories
|
* part: anything that does not fall under any of the above categories
|
||||||
* (e.g., "Summer," etc.)
|
* (e.g., "Summer," etc.)
|
||||||
*/
|
*/
|
||||||
|
var _slashRe = /\b([0-9]{1,4})([\-\/\.\u5e74])([0-9]{1,2})([\-\/\.\u6708])([0-9]{1,4})\b/
|
||||||
|
var _yearRe = /^(.*)\b((?:circa |around |about |c\.? ?)?[0-9]{1,4}(?: ?B\.? ?C\.?(?: ?E\.?)?| ?C\.? ?E\.?| ?A\.? ?D\.?)|[0-9]{3,4})\b(.*)$/i;
|
||||||
|
var _monthRe = null;
|
||||||
|
var _dayRe = null;
|
||||||
function strToDate(string) {
|
function strToDate(string) {
|
||||||
var date = new Object();
|
var date = new Object();
|
||||||
|
|
||||||
|
@ -655,39 +665,68 @@ Scholar.Date = new function(){
|
||||||
|
|
||||||
string = string.toString().replace(/^\s+/, "").replace(/\s+$/, "").replace(/\s+/, " ");
|
string = string.toString().replace(/^\s+/, "").replace(/\s+$/, "").replace(/\s+/, " ");
|
||||||
|
|
||||||
var dateRe = /^([0-9]{4})[\-\/]([0-9]{2})[\-\/]([0-9]{2})$/;
|
// first, directly inspect the string
|
||||||
var m = dateRe.exec(string);
|
var m = _slashRe.exec(string);
|
||||||
if(m) { // sql date
|
if(m && (m[2] == m[4] || (m[2] == "\u5e74" && m[4] == "\u6708"))) {
|
||||||
Scholar.debug("DATE: used form 1: SQL");
|
// figure out date based on parts
|
||||||
var jsDate = new Date(m[1], m[2]-1, m[3], false, false, false);
|
if(m[1].length == 4 || m[2] == "\u5e74") {
|
||||||
} else { // not an sql date
|
// ISO 8601 style date (big endian)
|
||||||
var yearRe = /^[0-9]+$/i;
|
date.year = m[1];
|
||||||
if(yearRe.test(string)) {
|
date.month = m[3];
|
||||||
// is just a year
|
date.day = m[5];
|
||||||
Scholar.debug("DATE: used form 2: year-only");
|
} else {
|
||||||
date.year = string;
|
// local style date (middle or little endian)
|
||||||
|
date.year = m[5];
|
||||||
|
var country = Scholar.locale.substr(3);
|
||||||
|
if(country == "US" || // The United States
|
||||||
|
country == "FM" || // The Federated States of Micronesia
|
||||||
|
country == "PW" || // Palau
|
||||||
|
country == "PH") { // The Philippines
|
||||||
|
date.month = m[1];
|
||||||
|
date.day = m[3];
|
||||||
|
} else {
|
||||||
|
date.month = m[3];
|
||||||
|
date.day = m[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
date.year = parseInt(date.year, 10);
|
||||||
|
date.month = parseInt(date.month, 10);
|
||||||
|
date.day = parseInt(date.day, 10);
|
||||||
|
|
||||||
|
if(date.month > 12) {
|
||||||
|
// swap day and month
|
||||||
|
var tmp = date.day;
|
||||||
|
date.day = date.month
|
||||||
|
date.month = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(date.month <= 12) {
|
||||||
|
if(date.year < 100) { // for two digit years, determine proper
|
||||||
|
// four digit year
|
||||||
|
var today = new Date();
|
||||||
|
var year = today.getFullYear();
|
||||||
|
var twoDigitYear = year % 100;
|
||||||
|
var century = year - twoDigitYear;
|
||||||
|
|
||||||
|
if(date.year <= twoDigitYear) {
|
||||||
|
// assume this date is from our century
|
||||||
|
date.year = century + date.year;
|
||||||
|
} else {
|
||||||
|
// assume this date is from the previous century
|
||||||
|
date.year = century - 100 + date.year;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
date.month--; // subtract one for JS style
|
||||||
|
Scholar.debug("DATE: retrieved with algorithms: "+date.toSource());
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
// who knows what this is, but try JavaScript's date handling first
|
|
||||||
var jsDate = new Date(string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!isNaN(jsDate.valueOf())) {
|
// couldn't use algorithms; use regexp
|
||||||
Scholar.debug("DATE: retrieved from JavaScript");
|
|
||||||
// got a javascript date
|
|
||||||
date.year = jsDate.getFullYear();
|
|
||||||
date.month = jsDate.getMonth();
|
|
||||||
date.day = jsDate.getDate();
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
|
|
||||||
// no javascript date. time for cruder things.
|
|
||||||
|
|
||||||
// first, see if we have anything resembling a year
|
// first, see if we have anything resembling a year
|
||||||
var yearRe = /^(.*)\b((?:circa |around |about |c\.? ?)?[0-9]{1,4}(?: ?B\.? ?C\.?(?: ?E\.?)?| ?C\.? ?E\.?| ?A\.? ?D\.?)|[0-9]{3,4})\b(.*)$/i;
|
var m = _yearRe.exec(string);
|
||||||
|
|
||||||
var m = yearRe.exec(string);
|
|
||||||
if(m) {
|
if(m) {
|
||||||
date.year = m[2];
|
date.year = m[2];
|
||||||
date.part = m[1]+m[3];
|
date.part = m[1]+m[3];
|
||||||
|
@ -695,18 +734,23 @@ Scholar.Date = new function(){
|
||||||
|
|
||||||
// get short month strings from CSL interpreter
|
// get short month strings from CSL interpreter
|
||||||
var months = CSL.getMonthStrings("short");
|
var months = CSL.getMonthStrings("short");
|
||||||
|
if(!_monthRe) {
|
||||||
// then, see if have anything resembling a month anywhere
|
// then, see if have anything resembling a month anywhere
|
||||||
var monthRe = new RegExp("^(.*)\\b("+months.join("|")+")[^ ]* (.*)$", "i");
|
_monthRe = new RegExp("^(.*)\\b("+months.join("|")+")[^ ]* (.*)$", "i");
|
||||||
var m = monthRe.exec(date.part);
|
}
|
||||||
|
|
||||||
|
var m = _monthRe.exec(date.part);
|
||||||
if(m) {
|
if(m) {
|
||||||
date.month = months.indexOf(m[2][0].toUpperCase()+m[2].substr(1).toLowerCase());
|
date.month = months.indexOf(m[2][0].toUpperCase()+m[2].substr(1).toLowerCase());
|
||||||
date.part = m[1]+m[3];
|
date.part = m[1]+m[3];
|
||||||
Scholar.debug("DATE: got month ("+date.month+", "+date.part+")");
|
Scholar.debug("DATE: got month ("+date.month+", "+date.part+")");
|
||||||
|
|
||||||
// then, see if there's a day
|
// then, see if there's a day
|
||||||
var dayRe = /^(.*)\b([0-9]{1,2})(?:st|nd|rd|th)?\b(.*)$/i;
|
if(!_dayRe) {
|
||||||
var m = dayRe.exec(date.part);
|
var daySuffixes = Scholar.getString("date.daySuffixes").replace(/, ?/g, "|");
|
||||||
|
_dayRe = new RegExp("^(.*)\\b([0-9]{1,2})(?:"+daySuffixes+")?\\b(.*)$", "i");
|
||||||
|
}
|
||||||
|
var m = _dayRe.exec(date.part);
|
||||||
if(m) {
|
if(m) {
|
||||||
date.day = parseInt(m[2], 10);
|
date.day = parseInt(m[2], 10);
|
||||||
date.part = m[1]+m[3];
|
date.part = m[1]+m[3];
|
||||||
|
@ -739,7 +783,7 @@ Scholar.Date = new function(){
|
||||||
// get short month strings from CSL interpreter
|
// get short month strings from CSL interpreter
|
||||||
var months = CSL.getMonthStrings("long");
|
var months = CSL.getMonthStrings("long");
|
||||||
string += months[date.month];
|
string += months[date.month];
|
||||||
if(date.day) {
|
if(date.day != undefined) {
|
||||||
string += " "+date.day+", ";
|
string += " "+date.day+", ";
|
||||||
} else {
|
} else {
|
||||||
string += " ";
|
string += " ";
|
||||||
|
|
|
@ -129,3 +129,5 @@ searchConditions.dateModified = Date Modified
|
||||||
|
|
||||||
exportOptions.exportNotes = Export Notes
|
exportOptions.exportNotes = Export Notes
|
||||||
exportOptions.exportFileData = Export Files
|
exportOptions.exportFileData = Export Files
|
||||||
|
|
||||||
|
date.daySuffixes = st, nd, rd, th
|
Loading…
Reference in New Issue
Block a user