Skip to content

Commit ea8e085

Browse files
committed
[added] modern globalize support
1 parent 4b3d3ba commit ea8e085

File tree

2 files changed

+93
-27
lines changed

2 files changed

+93
-27
lines changed

src/localizers/globalize.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
import dates from '../utils/dates';
2+
import oldGlobalize from './oldGlobalize';
23
import { set } from '../formats';
34
import { set as setLocalizer } from '../localizer';
45

5-
function inSame12Hr(start, end){
6-
let s = 12 - dates.hours(start)
7-
let e = 12 - dates.hours(end)
8-
return (s <= 0 && e <= 0) || (s >= 0 && e >= 0)
9-
}
10-
116
let dateRangeFormat = ({ start, end }, culture, local)=>
12-
local.format(start, 'd', culture) + ' — ' + local.format(end, 'd', culture)
7+
local.format(start, { date: 'short' }, culture) + ' — ' + local.format(end, { date: 'short' }, culture)
138

149
let timeRangeFormat = ({ start, end }, culture, local)=>
15-
local.format(start, 'h:mmtt', culture) +
16-
' — ' + local.format(end, inSame12Hr(start, end) ? 'h:mm' : 'h:mmtt', culture)
10+
local.format(start, { time: 'short' }, culture) +
11+
' — ' + local.format(end, { time: 'short' }, culture)
1712

1813
let weekRangeFormat = ({ start, end }, culture, local)=>
1914
local.format(start, 'MMM dd', culture) +
20-
' - ' + local.format(end, dates.eq(start, end, 'month') ? 'dd' : 'MMM dd', culture)
15+
' ' + local.format(end, dates.eq(start, end, 'month') ? 'dd' : 'MMM dd', culture)
2116

2217
export let formats = {
2318
dateFormat: 'dd',
24-
dayFormat: 'ddd dd/MM',
25-
weekdayFormat: 'ddd',
19+
dayFormat: 'eee dd/MM',
20+
weekdayFormat: 'eee',
2621

2722
selectRangeFormat: timeRangeFormat,
2823
eventTimeRangeFormat: timeRangeFormat,
2924

30-
timeGutterFormat: 't',
25+
timeGutterFormat: { time: 'short' },
3126

32-
monthHeaderFormat: 'Y',
33-
dayHeaderFormat: 'dddd MMM dd',
27+
monthHeaderFormat: 'MMMM yyyy',
28+
dayHeaderFormat: 'eeee MMM dd',
3429
dayRangeHeaderFormat: weekRangeFormat,
3530
agendaHeaderFormat: dateRangeFormat,
3631

37-
agendaDateFormat: 'ddd MMM dd',
38-
agendaTimeFormat: 't',
32+
agendaDateFormat: 'eee MMM dd',
33+
agendaTimeFormat: { time: 'short' },
3934
agendaTimeRangeFormat: timeRangeFormat
4035
}
4136

4237
export default function(globalize) {
43-
44-
function getCulture(culture){
45-
return culture
46-
? globalize.findClosestCulture(culture)
47-
: globalize.culture()
48-
}
38+
let locale = culture => culture ? globalize(culture) : globalize;
4939

5040
function firstOfWeek(culture) {
51-
culture = getCulture(culture)
52-
return (culture && culture.calendar.firstDay) || 0
41+
let date = new Date();
42+
//cldr-data doesn't seem to be zero based
43+
let localeDay = Math.max(
44+
parseInt(locale(culture).formatDate(date, { raw: 'e' }), 10) - 1, 0)
45+
46+
return Math.abs(date.getDay() - localeDay)
5347
}
5448

49+
if (!globalize.load)
50+
return oldGlobalize(globalize);
51+
52+
5553
set(formats)
5654

5755
return setLocalizer({
5856
firstOfWeek,
5957

6058
parse(value, format, culture){
61-
return globalize.parseDate(value, format, culture)
59+
format = typeof format === 'string' ? { raw: format } : format;
60+
return locale(culture).parseDate(value, format)
6261
},
6362

6463
format(value, format, culture){
65-
return globalize.format(value, format, culture)
64+
format = typeof format === 'string' ? { raw: format } : format;
65+
return locale(culture).formatDate(value, format)
6666
}
6767
})
6868
}

src/localizers/oldGlobalize.js

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import dates from '../utils/dates';
2+
import { set } from '../formats';
3+
import { set as setLocalizer } from '../localizer';
4+
5+
let dateRangeFormat = ({ start, end }, culture, local)=>
6+
local.format(start, 'd', culture) + ' — ' + local.format(end, 'd', culture)
7+
8+
let timeRangeFormat = ({ start, end }, culture, local)=>
9+
local.format(start, 't', culture) +
10+
' — ' + local.format(end, 't', culture)
11+
12+
let weekRangeFormat = ({ start, end }, culture, local)=>
13+
local.format(start, 'MMM dd', culture) +
14+
' - ' + local.format(end, dates.eq(start, end, 'month') ? 'dd' : 'MMM dd', culture)
15+
16+
export default function(globalize) {
17+
18+
}
19+
20+
export let formats = {
21+
dateFormat: 'dd',
22+
dayFormat: 'ddd dd/MM',
23+
weekdayFormat: 'ddd',
24+
25+
selectRangeFormat: timeRangeFormat,
26+
eventTimeRangeFormat: timeRangeFormat,
27+
28+
timeGutterFormat: 't',
29+
30+
monthHeaderFormat: 'Y',
31+
dayHeaderFormat: 'dddd MMM dd',
32+
dayRangeHeaderFormat: weekRangeFormat,
33+
agendaHeaderFormat: dateRangeFormat,
34+
35+
agendaDateFormat: 'ddd MMM dd',
36+
agendaTimeFormat: 't',
37+
agendaTimeRangeFormat: timeRangeFormat
38+
}
39+
40+
export default function(globalize) {
41+
42+
function getCulture(culture){
43+
return culture
44+
? globalize.findClosestCulture(culture)
45+
: globalize.culture()
46+
}
47+
48+
function firstOfWeek(culture) {
49+
culture = getCulture(culture)
50+
return (culture && culture.calendar.firstDay) || 0
51+
}
52+
53+
set(formats)
54+
55+
return setLocalizer({
56+
firstOfWeek,
57+
58+
parse(value, format, culture){
59+
return globalize.parseDate(value, format, culture)
60+
},
61+
62+
format(value, format, culture){
63+
return globalize.format(value, format, culture)
64+
}
65+
})
66+
}

0 commit comments

Comments
 (0)