forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lang.js
149 lines (114 loc) · 5.02 KB
/
lang.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
var langOptionHash = fc.langs = {}; // initialize and expose
// TODO: document the structure and ordering of a FullCalendar lang file
// TODO: rename everything "lang" to "locale", like what the moment project did
// Initialize jQuery UI datepicker translations while using some of the translations
// Will set this as the default language for datepicker.
fc.datepickerLang = function(langCode, dpLangCode, dpOptions) {
// get the FullCalendar internal option hash for this language. create if necessary
var fcOptions = langOptionHash[langCode] || (langOptionHash[langCode] = {});
// transfer some simple options from datepicker to fc
fcOptions.isRTL = dpOptions.isRTL;
fcOptions.weekNumberTitle = dpOptions.weekHeader;
// compute some more complex options from datepicker
$.each(dpComputableOptions, function(name, func) {
fcOptions[name] = func(dpOptions);
});
// is jQuery UI Datepicker is on the page?
if ($.datepicker) {
// Register the language data.
// FullCalendar and MomentJS use language codes like "pt-br" but Datepicker
// does it like "pt-BR" or if it doesn't have the language, maybe just "pt".
// Make an alias so the language can be referenced either way.
$.datepicker.regional[dpLangCode] =
$.datepicker.regional[langCode] = // alias
dpOptions;
// Alias 'en' to the default language data. Do this every time.
$.datepicker.regional.en = $.datepicker.regional[''];
// Set as Datepicker's global defaults.
$.datepicker.setDefaults(dpOptions);
}
};
// Sets FullCalendar-specific translations. Will set the language as the global default.
fc.lang = function(langCode, newFcOptions) {
var fcOptions;
var momOptions;
// get the FullCalendar internal option hash for this language. create if necessary
fcOptions = langOptionHash[langCode] || (langOptionHash[langCode] = {});
// provided new options for this language? merge them in
if (newFcOptions) {
mergeOptions(fcOptions, newFcOptions);
}
// compute language options that weren't defined.
// always do this. newFcOptions can be undefined when initializing from i18n file,
// so no way to tell if this is an initialization or a default-setting.
momOptions = getMomentLocaleData(langCode); // will fall back to en
$.each(momComputableOptions, function(name, func) {
if (fcOptions[name] === undefined) {
fcOptions[name] = func(momOptions, fcOptions);
}
});
// set it as the default language for FullCalendar
defaults.lang = langCode;
};
// NOTE: can't guarantee any of these computations will run because not every language has datepicker
// configs, so make sure there are English fallbacks for these in the defaults file.
var dpComputableOptions = {
defaultButtonText: function(dpOptions) {
return {
// the translations sometimes wrongly contain HTML entities
prev: stripHtmlEntities(dpOptions.prevText),
next: stripHtmlEntities(dpOptions.nextText),
today: stripHtmlEntities(dpOptions.currentText)
};
},
// Produces format strings like "MMMM YYYY" -> "September 2014"
monthYearFormat: function(dpOptions) {
return dpOptions.showMonthAfterYear ?
'YYYY[' + dpOptions.yearSuffix + '] MMMM' :
'MMMM YYYY[' + dpOptions.yearSuffix + ']';
}
};
var momComputableOptions = {
// Produces format strings like "ddd MM/DD" -> "Fri 12/10"
dayOfMonthFormat: function(momOptions, fcOptions) {
var format = momOptions.longDateFormat('l'); // for the format like "M/D/YYYY"
// strip the year off the edge, as well as other misc non-whitespace chars
format = format.replace(/^Y+[^\w\s]*|[^\w\s]*Y+$/g, '');
if (fcOptions.isRTL) {
format += ' ddd'; // for RTL, add day-of-week to end
}
else {
format = 'ddd ' + format; // for LTR, add day-of-week to beginning
}
return format;
},
// Produces format strings like "H(:mm)a" -> "6pm" or "6:30pm"
smallTimeFormat: function(momOptions) {
return momOptions.longDateFormat('LT')
.replace(':mm', '(:mm)')
.replace(/(\Wmm)$/, '($1)') // like above, but for foreign langs
.replace(/\s*a$/i, 'a'); // convert AM/PM/am/pm to lowercase. remove any spaces beforehand
},
// Produces format strings like "H(:mm)t" -> "6p" or "6:30p"
extraSmallTimeFormat: function(momOptions) {
return momOptions.longDateFormat('LT')
.replace(':mm', '(:mm)')
.replace(/(\Wmm)$/, '($1)') // like above, but for foreign langs
.replace(/\s*a$/i, 't'); // convert to AM/PM/am/pm to lowercase one-letter. remove any spaces beforehand
},
// Produces format strings like "H:mm" -> "6:30" (with no AM/PM)
noMeridiemTimeFormat: function(momOptions) {
return momOptions.longDateFormat('LT')
.replace(/\s*a$/i, ''); // remove trailing AM/PM
}
};
// Returns moment's internal locale data. If doesn't exist, returns English.
// Works with moment-pre-2.8
function getMomentLocaleData(langCode) {
var func = moment.localeData || moment.langData;
return func.call(moment, langCode) ||
func.call(moment, 'en'); // the newer localData could return null, so fall back to en
}
// Initialize English by forcing computation of moment-derived options.
// Also, sets it as the default.
fc.lang('en', englishDefaults);