Skip to content

Commit

Permalink
[added] modern globalize support
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Nov 29, 2015
1 parent 4b3d3ba commit ea8e085
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/localizers/globalize.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
66 changes: 66 additions & 0 deletions src/localizers/oldGlobalize.js
Original file line number Diff line number Diff line change
@@ -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)
}
})
}

0 comments on commit ea8e085

Please sign in to comment.