forked from phonegap/phonegap-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobalization.js
548 lines (500 loc) · 18.9 KB
/
Globalization.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2010-2011, IBM Corporation
*
*/
var Globalization = function() {
};
Globalization.prototype.getPreferredLanguage = function(successCB, failureCB)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.getPreferredLanguage Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getPreferredLanguage Error: failureCB is not a function");
return;
}
Codova.exec(successCB, failureCB, "Globalization","getPreferredLanguage", []);
};
/**
* Returns the string identifier for the client's current locale setting.
* It returns the locale identifier string to the successCB callback with a
* properties object as a parameter. If there is an error getting the locale,
* then the errorCB callback is invoked.
*
* @param {Function} successCB
* @param {Function} errorCB
*
* @return Object.value {String}: The locale identifier
*
* @error GlobalizationError.UNKNOWN_ERROR
*
* Example
* globalization.getLocaleName(function (locale) {alert('locale:' + locale.value + '\n');},
* function () {});
*/
Globalization.prototype.getLocaleName = function(successCB, failureCB)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.getLocaleName Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getLocaleName Error: failureCB is not a function");
return;
}
Cordova.exec(successCB, failureCB, "Globalization","getLocaleName", []);
};
/**
* Returns a date formatted as a string according to the client's user preferences and
* calendar using the time zone of the client. It returns the formatted date string to the
* successCB callback with a properties object as a parameter. If there is an error
* formatting the date, then the errorCB callback is invoked.
*
* The defaults are: formatLenght="short" and selector="date and time"
*
* @param {Date} date
* @param {Function} successCB
* @param {Function} errorCB
* @param {Object} options {optional}
* formatLength {String}: “short”, “medium”, “long”, or “full”
* selector {String}: “date”, “time”, or “date and time”
*
* @return Object.value {String}: The localized date string
*
* @error GlobalizationError.FORMATTING_ERROR
*
* Example
* globalization.dateToString(new Date(),
* function (date) {alert('date:' + date.value + '\n');},
* function (errorCode) {alert(errorCode);},
* {formatLength:'short'});
*/
Globalization.prototype.dateToString = function(date, successCB, failureCB, options)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.dateToString Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.dateToString Error: failureCB is not a function");
return;
}
if (date instanceof Date){
var dateValue;
dateValue = date.valueOf();
Cordova.exec(successCB, failureCB, "Globalization", "dateToString", [{"date": dateValue, "options": options}]);
}
else {
console.log("Globalization.dateToString Error: date is not a Date object");
}
};
/**
* Parses a date formatted as a string according to the client's user
* preferences and calendar using the time zone of the client and returns
* the corresponding date object. It returns the date to the successCB
* callback with a properties object as a parameter. If there is an error
* parsing the date string, then the errorCB callback is invoked.
*
* The defaults are: formatLength="short" and selector="date and time"
*
* @param {String} dateString
* @param {Function} successCB
* @param {Function} errorCB
* @param {Object} options {optional}
* formatLength {String}: “short”, “medium”, “long”, or “full”
* selector {String}: “date”, “time”, or “date and time”
*
* @return Object.year {Number}: The four digit year
* Object.month {Number}: The month from (0 - 11)
* Object.day {Number}: The day from (1 - 31)
* Object.hour {Number}: The hour from (0 - 23)
* Object.minute {Number}: The minute from (0 - 59)
* Object.second {Number}: The second from (0 - 59)
* Object.millisecond {Number}: The milliseconds (from 0 – 999),
* not available on all platforms
*
* @error GlobalizationError.PARSING_ERROR
*
* Example
* globalization.stringToDate('4/11/2011',
* function (date) { alert('Month:' + date.month + “\n” +
* 'Day:' + date.day + “\n” +
* 'Year:' + date.year + “\n”);},
* function (errorCode) {alert(errorCode);},
* {selector:'date'});
*/
Globalization.prototype.stringToDate = function(dateString, successCB, failureCB, options)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.stringToDate Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.stringToDate Error: failureCB is not a function");
return;
}
if (typeof dateString == "string"){
Cordova.exec(successCB, failureCB, "Globalization", "stringToDate", [{"dateString": dateString, "options": options}]);
}
else {
console.log("Globalization.stringToDate Error: dateString is not a string");
}
};
/**
* Returns a pattern string for formatting and parsing dates according to the client's
* user preferences. It returns the pattern to the successCB callback with a
* properties object as a parameter. If there is an error obtaining the pattern,
* then the errorCB callback is invoked.
*
* The defaults are: formatLength="short" and selector="date and time"
*
* @param {Function} successCB
* @param {Function} errorCB
* @param {Object} options {optional}
* formatLength {String}: “short”, “medium”, “long”, or “full”
* selector {String}: “date”, “time”, or “date and time”
*
* @return Object.pattern {String}: The date and time pattern for formatting and parsing dates.
* The patterns follow Unicode Technical Standard #35
* http://unicode.org/reports/tr35/tr35-4.html
* Object.timezone {String}: The abbreviated name of the time zone on the client
* Object.utc_offset {Number}: The current difference in seconds between the client's
* time zone and coordinated universal time.
* Object.dst_offset {Number}: The current daylight saving time offset in seconds
* between the client's non-daylight saving's time zone
* and the client's daylight saving's time zone.
*
* @error GlobalizationError.PATTERN_ERROR
*
* Example
* globalization.getDatePattern(
* function (date) {alert('pattern:' + date.pattern + '\n');},
* function () {},
* {formatLength:'short'});
*/
Globalization.prototype.getDatePattern = function(successCB, failureCB, options)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.getDatePattern Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getDatePattern Error: failureCB is not a function");
return;
}
Cordova.exec(successCB, failureCB, "Globalization", "getDatePattern", [{"options": options}]);
};
/**
* Returns an array of either the names of the months or days of the week
* according to the client's user preferences and calendar. It returns the array of names to the
* successCB callback with a properties object as a parameter. If there is an error obtaining the
* names, then the errorCB callback is invoked.
*
* The defaults are: type="wide" and item="months"
*
* @param {Function} successCB
* @param {Function} errorCB
* @param {Object} options {optional}
* type {String}: “narrow” or “wide”
* item {String}: “months”, or “days”
*
* @return Object.value {Array{String}}: The array of names starting from either
* the first month in the year or the
* first day of the week.
* @error GlobalizationError.UNKNOWN_ERROR
*
* Example
* globalization.getDateNames(function (names) {
* for(var i = 0; i < names.value.length; i++) {
* alert('Month:' + names.value[i] + '\n');}},
* function () {});
*/
Globalization.prototype.getDateNames = function(successCB, failureCB, options)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.getDateNames Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getDateNames Error: failureCB is not a function");
return;
}
Cordova.exec(successCB, failureCB, "Globalization", "getDateNames", [{"options": options}]);
};
/**
* Returns whether daylight savings time is in effect for a given date using the client's
* time zone and calendar. It returns whether or not daylight savings time is in effect
* to the successCB callback with a properties object as a parameter. If there is an error
* reading the date, then the errorCB callback is invoked.
*
* @param {Date} date
* @param {Function} successCB
* @param {Function} errorCB
*
* @return Object.dst {Boolean}: The value "true" indicates that daylight savings time is
* in effect for the given date and "false" indicate that it is not.
*
* @error GlobalizationError.UNKNOWN_ERROR
*
* Example
* globalization.isDayLightSavingsTime(new Date(),
* function (date) {alert('dst:' + date.dst + '\n');}
* function () {});
*/
Globalization.prototype.isDayLightSavingsTime = function(date, successCB, failureCB)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.isDayLightSavingsTime Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.isDayLightSavingsTime Error: failureCB is not a function");
return;
}
if (date instanceof Date){
var dateValue;
dateValue = date.valueOf();
Cordova.exec(successCB, failureCB, "Globalization", "isDayLightSavingsTime", [{"date": dateValue}]);
}
else {
console.log("Globalization.isDayLightSavingsTime Error: date is not a Date object");
}
};
/**
* Returns the first day of the week according to the client's user preferences and calendar.
* The days of the week are numbered starting from 1 where 1 is considered to be Sunday.
* It returns the day to the successCB callback with a properties object as a parameter.
* If there is an error obtaining the pattern, then the errorCB callback is invoked.
*
* @param {Function} successCB
* @param {Function} errorCB
*
* @return Object.value {Number}: The number of the first day of the week.
*
* @error GlobalizationError.UNKNOWN_ERROR
*
* Example
* globalization.getFirstDayOfWeek(function (day)
* { alert('Day:' + day.value + '\n');},
* function () {});
*/
Globalization.prototype.getFirstDayOfWeek = function(successCB, failureCB)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.getFirstDayOfWeek Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getFirstDayOfWeek Error: failureCB is not a function");
return;
}
Cordova.exec(successCB, failureCB, "Globalization", "getFirstDayOfWeek", []);
};
/**
* Returns a number formatted as a string according to the client's user preferences.
* It returns the formatted number string to the successCB callback with a properties object as a
* parameter. If there is an error formatting the number, then the errorCB callback is invoked.
*
* The defaults are: type="decimal"
*
* @param {Number} number
* @param {Function} successCB
* @param {Function} errorCB
* @param {Object} options {optional}
* type {String}: “decimal”, "percent", or “currency”
*
* @return Object.value {String}: The formatted number string.
*
* @error GlobalizationError.FORMATTING_ERROR
*
* Example
* globalization.numberToString(3.25,
* function (number) {alert('number:' + number.value + '\n');},
* function () {},
* {type:'decimal'});
*/
Globalization.prototype.numberToString = function(number, successCB, failureCB, options)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.numberToString Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.numberToString Error: failureCB is not a function");
return;
}
if(typeof number == "number") {
Cordova.exec(successCB, failureCB, "Globalization", "numberToString", [{"number": number, "options": options}]);
}
else {
console.log("Globalization.numberToString Error: number is not a number");
}
};
/**
* Parses a number formatted as a string according to the client's user preferences and
* returns the corresponding number. It returns the number to the successCB callback with a
* properties object as a parameter. If there is an error parsing the number string, then
* the errorCB callback is invoked.
*
* The defaults are: type="decimal"
*
* @param {String} numberString
* @param {Function} successCB
* @param {Function} errorCB
* @param {Object} options {optional}
* type {String}: “decimal”, "percent", or “currency”
*
* @return Object.value {Number}: The parsed number.
*
* @error GlobalizationError.PARSING_ERROR
*
* Example
* globalization.stringToNumber('1234.56',
* function (number) {alert('Number:' + number.value + '\n');},
* function () { alert('Error parsing number');});
*/
Globalization.prototype.stringToNumber = function(numberString, successCB, failureCB, options)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.stringToNumber Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.stringToNumber Error: failureCB is not a function");
return;
}
if(typeof numberString == "string") {
Cordova.exec(successCB, failureCB, "Globalization", "stringToNumber", [{"numberString": numberString, "options": options}]);
}
else {
console.log("Globalization.stringToNumber Error: numberString is not a string");
}
};
/**
* Returns a pattern string for formatting and parsing numbers according to the client's user
* preferences. It returns the pattern to the successCB callback with a properties object as a
* parameter. If there is an error obtaining the pattern, then the errorCB callback is invoked.
*
* The defaults are: type="decimal"
*
* @param {Function} successCB
* @param {Function} errorCB
* @param {Object} options {optional}
* type {String}: “decimal”, "percent", or “currency”
*
* @return Object.pattern {String}: The number pattern for formatting and parsing numbers.
* The patterns follow Unicode Technical Standard #35.
* http://unicode.org/reports/tr35/tr35-4.html
* Object.symbol {String}: The symbol to be used when formatting and parsing
* e.g., percent or currency symbol.
* Object.fraction {Number}: The number of fractional digits to use when parsing and
* formatting numbers.
* Object.rounding {Number}: The rounding increment to use when parsing and formatting.
* Object.positive {String}: The symbol to use for positive numbers when parsing and formatting.
* Object.negative: {String}: The symbol to use for negative numbers when parsing and formatting.
* Object.decimal: {String}: The decimal symbol to use for parsing and formatting.
* Object.grouping: {String}: The grouping symbol to use for parsing and formatting.
*
* @error GlobalizationError.PATTERN_ERROR
*
* Example
* '
*/
Globalization.prototype.getNumberPattern = function(successCB, failureCB, options)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.getNumberPattern Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getNumberPattern Error: failureCB is not a function");
return;
}
Cordova.exec(successCB, failureCB, "Globalization", "getNumberPattern", [{"options": options}]);
};
/**
* Returns a pattern string for formatting and parsing currency values according to the client's
* user preferences and ISO 4217 currency code. It returns the pattern to the successCB callback with a
* properties object as a parameter. If there is an error obtaining the pattern, then the errorCB
* callback is invoked.
*
* @param {String} currencyCode
* @param {Function} successCB
* @param {Function} errorCB
*
* @return Object.pattern {String}: The currency pattern for formatting and parsing currency values.
* The patterns follow Unicode Technical Standard #35
* http://unicode.org/reports/tr35/tr35-4.html
* Object.code {String}: The ISO 4217 currency code for the pattern.
* Object.fraction {Number}: The number of fractional digits to use when parsing and
* formatting currency.
* Object.rounding {Number}: The rounding increment to use when parsing and formatting.
* Object.decimal: {String}: The decimal symbol to use for parsing and formatting.
* Object.grouping: {String}: The grouping symbol to use for parsing and formatting.
*
* @error GlobalizationError.FORMATTING_ERROR
*
* Example
* globalization.getCurrencyPattern('EUR',
* function (currency) {alert('Pattern:' + currency.pattern + '\n');}
* function () {});
*/
Globalization.prototype.getCurrencyPattern = function(currencyCode, successCB, failureCB)
{
// successCallback required
if (typeof successCB != "function") {
console.log("Globalization.getCurrencyPattern Error: successCB is not a function");
return;
}
// errorCallback required
if (typeof failureCB != "function") {
console.log("Globalization.getCurrencyPattern Error: failureCB is not a function");
return;
}
if(typeof currencyCode == "string") {
Cordova.exec(successCB, failureCB, "Globalization", "getCurrencyPattern", [{"currencyCode": currencyCode}]);
}
else {
console.log("Globalization.getCurrencyPattern Error: currencyCode is not a currency code");
}
};
GlobalizationError = function() {
this.code = null;
}
// Globalization error codes
GlobalizationError.UNKNOWN_ERROR = 0;
GlobalizationError.FORMATTING_ERROR = 1;
GlobalizationError.PARSING_ERROR = 2;
GlobalizationError.PATTERN_ERROR = 3;
if(!window.plugins) {
window.plugins = {};
}
if (!window.plugins.globalization) {
window.plugins.globalization = new Globalization();
}