generateLanguages.js
4.93 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var pathLib = require('path');
module.exports = function(grunt) {
var config = grunt.config('generateLanguages');
grunt.registerTask('generateLanguages', function() {
var combinedJS = '';
var languageCnt = 0;
var skippedLangCodes = [];
grunt.file.mkdir(config.dest, 0755);
grunt.file.expand(pathLib.join(config.moment, '*.js')).forEach(function(momentPath) {
var langCode = momentPath.match(/([^\/]*)\.js$/)[1];
var js = getLangJS(langCode, momentPath);
if (js) {
grunt.file.write(
pathLib.join(config.dest, langCode + '.js'),
wrapWithUMD(js)
);
combinedJS += wrapWithClosure(js) + '\n';
languageCnt++;
}
else {
skippedLangCodes.push(langCode);
}
});
// code for resetting the language back to English
combinedJS += '\n(moment.locale || moment.lang).call(moment, "en");'; // works with moment-pre-2.8
combinedJS += '\n$.fullCalendar.lang("en");';
combinedJS += '\nif ($.datepicker) $.datepicker.setDefaults($.datepicker.regional[""]);';
if (config.allDest) {
grunt.file.write(config.allDest, wrapWithUMD(combinedJS));
}
grunt.log.writeln(skippedLangCodes.length + ' skipped languages: ' + skippedLangCodes.join(', '));
grunt.log.writeln(languageCnt + ' generated languages.');
});
function getLangJS(langCode, momentPath) {
var shortLangCode;
var momentLangJS;
var datepickerLangJS;
var fullCalendarLangJS;
// given "fr-ca", get just "fr"
if (langCode.indexOf('-') != -1) {
shortLangCode = langCode.replace(/-.*/, '');
}
momentLangJS = getMomentLangJS(momentPath);
datepickerLangJS = getDatepickerLangJS(langCode);
if (!datepickerLangJS && shortLangCode) {
datepickerLangJS = getDatepickerLangJS(shortLangCode, langCode);
}
fullCalendarLangJS = getFullCalendarLangJS(langCode);
if (!fullCalendarLangJS && shortLangCode) {
fullCalendarLangJS = getFullCalendarLangJS(shortLangCode, langCode);
}
// If this is an "en" language, only the Moment config is needed.
// For all other languages, all 3 configs are needed.
if (momentLangJS && (shortLangCode == 'en' || (datepickerLangJS && fullCalendarLangJS))) {
// if there is no definition, we still need to tell FC to set the default
if (!fullCalendarLangJS) {
fullCalendarLangJS = '$.fullCalendar.lang("' + langCode + '");';
}
datepickerLangJS = datepickerLangJS || '';
return momentLangJS + '\n' +
datepickerLangJS + '\n' +
fullCalendarLangJS;
}
}
function wrapWithUMD(body) {
return [
'(function(factory) {',
' if (typeof define === "function" && define.amd) {',
' define([ "jquery", "moment" ], factory);',
' }',
' else {',
' factory(jQuery, moment);',
' }',
'})(function($, moment) {',
'',
body,
'',
'});'
].join('\n');
}
function wrapWithClosure(body) {
return [
'(function() {',
'',
body,
'',
'})();'
].join('\n');
}
function getMomentLangJS(path) { // file assumed to exist
var js = grunt.file.read(path);
js = js.replace( // remove the UMD wrap
/\(\s*function[\S\s]*?function\s*\(\s*moment\s*\)\s*\{([\S\s]*)\}\)\);?/,
function(m0, body) {
body = body.replace(/^ /mg, ''); // remove 1 level of indentation
return body;
}
);
// replace the `return` statement so execution continues
// compatible with moment-pre-2.8
js = js.replace(
/^(\s*)return moment\.(defineLocale|lang)\(/m,
'$1(moment.defineLocale || moment.lang).call(moment, '
);
return js;
}
function getDatepickerLangJS(langCode, targetLangCode) {
// convert "en-ca" to "en-CA"
var datepickerLangCode = langCode.replace(/\-(\w+)/, function(m0, m1) {
return '-' + m1.toUpperCase();
});
var path = pathLib.join(config.datepicker, 'datepicker-' + datepickerLangCode + '.js');
var js;
try {
js = grunt.file.read(path);
}
catch (ex) {
return false;
}
js = js.replace( // remove the UMD wrap
/\(\s*function[\S\s]*?function\s*\(\s*datepicker\s*\)\s*\{([\S\s]*)\}\)\);?/m,
function(m0, body) { // use only the function body, modified
var match = body.match(/datepicker\.regional[\S\s]*?(\{[\S\s]*?\});?/);
var props = match[1];
// remove 1 level of tab indentation
props = props.replace(/^\t/mg, '');
return "$.fullCalendar.datepickerLang(" +
"'" + (targetLangCode || langCode) + "', " + // for FullCalendar
"'" + datepickerLangCode + "', " + // for datepicker
props +
");";
}
);
return js;
}
function getFullCalendarLangJS(langCode, targetLangCode) {
var path = pathLib.join(config.fullCalendar, langCode + '.js');
var js;
try {
js = grunt.file.read(path);
}
catch (ex) {
return false;
}
// if we originally wanted "ar-ma", but only "ar" is available, we have to adjust
// the declaration
if (targetLangCode && targetLangCode != langCode) {
js = js.replace(
/\$\.fullCalendar\.lang\(['"]([^'"]*)['"]/,
'$.fullCalendar.lang("' + targetLangCode + '"'
);
}
return js;
}
};