From ea8e0851ad68bd8393a63fda77c317804d9780b9 Mon Sep 17 00:00:00 2001 From: jquense Date: Sun, 29 Nov 2015 14:00:40 -0500 Subject: [PATCH] [added] modern globalize support --- src/localizers/globalize.js | 54 ++++++++++++++-------------- src/localizers/oldGlobalize.js | 66 ++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 src/localizers/oldGlobalize.js diff --git a/src/localizers/globalize.js b/src/localizers/globalize.js index aaeed78..1dd7eee 100644 --- a/src/localizers/globalize.js +++ b/src/localizers/globalize.js @@ -1,68 +1,68 @@ import dates from '../utils/dates'; +import oldGlobalize from './oldGlobalize'; import { set } from '../formats'; import { set as setLocalizer } from '../localizer'; -function inSame12Hr(start, end){ - let s = 12 - dates.hours(start) - let e = 12 - dates.hours(end) - return (s <= 0 && e <= 0) || (s >= 0 && e >= 0) -} - let dateRangeFormat = ({ start, end }, culture, local)=> - local.format(start, 'd', culture) + ' — ' + local.format(end, 'd', culture) + local.format(start, { date: 'short' }, culture) + ' — ' + local.format(end, { date: 'short' }, culture) let timeRangeFormat = ({ start, end }, culture, local)=> - local.format(start, 'h:mmtt', culture) + - ' — ' + local.format(end, inSame12Hr(start, end) ? 'h:mm' : 'h:mmtt', culture) + local.format(start, { time: 'short' }, culture) + + ' — ' + local.format(end, { time: 'short' }, culture) let weekRangeFormat = ({ start, end }, culture, local)=> local.format(start, 'MMM dd', culture) + - ' - ' + local.format(end, dates.eq(start, end, 'month') ? 'dd' : 'MMM dd', culture) + ' — ' + local.format(end, dates.eq(start, end, 'month') ? 'dd' : 'MMM dd', culture) export let formats = { dateFormat: 'dd', - dayFormat: 'ddd dd/MM', - weekdayFormat: 'ddd', + dayFormat: 'eee dd/MM', + weekdayFormat: 'eee', selectRangeFormat: timeRangeFormat, eventTimeRangeFormat: timeRangeFormat, - timeGutterFormat: 't', + timeGutterFormat: { time: 'short' }, - monthHeaderFormat: 'Y', - dayHeaderFormat: 'dddd MMM dd', + monthHeaderFormat: 'MMMM yyyy', + dayHeaderFormat: 'eeee MMM dd', dayRangeHeaderFormat: weekRangeFormat, agendaHeaderFormat: dateRangeFormat, - agendaDateFormat: 'ddd MMM dd', - agendaTimeFormat: 't', + agendaDateFormat: 'eee MMM dd', + agendaTimeFormat: { time: 'short' }, agendaTimeRangeFormat: timeRangeFormat } export default function(globalize) { - - function getCulture(culture){ - return culture - ? globalize.findClosestCulture(culture) - : globalize.culture() - } + let locale = culture => culture ? globalize(culture) : globalize; function firstOfWeek(culture) { - culture = getCulture(culture) - return (culture && culture.calendar.firstDay) || 0 + let date = new Date(); + //cldr-data doesn't seem to be zero based + let localeDay = Math.max( + parseInt(locale(culture).formatDate(date, { raw: 'e' }), 10) - 1, 0) + + return Math.abs(date.getDay() - localeDay) } + if (!globalize.load) + return oldGlobalize(globalize); + + set(formats) return setLocalizer({ firstOfWeek, parse(value, format, culture){ - return globalize.parseDate(value, format, culture) + format = typeof format === 'string' ? { raw: format } : format; + return locale(culture).parseDate(value, format) }, format(value, format, culture){ - return globalize.format(value, format, culture) + format = typeof format === 'string' ? { raw: format } : format; + return locale(culture).formatDate(value, format) } }) } diff --git a/src/localizers/oldGlobalize.js b/src/localizers/oldGlobalize.js new file mode 100644 index 0000000..2af568e --- /dev/null +++ b/src/localizers/oldGlobalize.js @@ -0,0 +1,66 @@ +import dates from '../utils/dates'; +import { set } from '../formats'; +import { set as setLocalizer } from '../localizer'; + +let dateRangeFormat = ({ start, end }, culture, local)=> + local.format(start, 'd', culture) + ' — ' + local.format(end, 'd', culture) + +let timeRangeFormat = ({ start, end }, culture, local)=> + local.format(start, 't', culture) + + ' — ' + local.format(end, 't', culture) + +let weekRangeFormat = ({ start, end }, culture, local)=> + local.format(start, 'MMM dd', culture) + + ' - ' + local.format(end, dates.eq(start, end, 'month') ? 'dd' : 'MMM dd', culture) + +export default function(globalize) { + +} + +export let formats = { + dateFormat: 'dd', + dayFormat: 'ddd dd/MM', + weekdayFormat: 'ddd', + + selectRangeFormat: timeRangeFormat, + eventTimeRangeFormat: timeRangeFormat, + + timeGutterFormat: 't', + + monthHeaderFormat: 'Y', + dayHeaderFormat: 'dddd MMM dd', + dayRangeHeaderFormat: weekRangeFormat, + agendaHeaderFormat: dateRangeFormat, + + agendaDateFormat: 'ddd MMM dd', + agendaTimeFormat: 't', + agendaTimeRangeFormat: timeRangeFormat +} + +export default function(globalize) { + + function getCulture(culture){ + return culture + ? globalize.findClosestCulture(culture) + : globalize.culture() + } + + function firstOfWeek(culture) { + culture = getCulture(culture) + return (culture && culture.calendar.firstDay) || 0 + } + + set(formats) + + return setLocalizer({ + firstOfWeek, + + parse(value, format, culture){ + return globalize.parseDate(value, format, culture) + }, + + format(value, format, culture){ + return globalize.format(value, format, culture) + } + }) +}