1
1
/*
2
- * jQuery Globalization plugin
2
+ * Globalization
3
3
* http://github.com/nje/jquery-glob
4
4
*/
5
- ( function ( $ ) {
5
+ ( function ( ) {
6
6
7
- var localized = { en : { } } ;
7
+ var Globalization = { } ,
8
+ localized = { en : { } } ;
8
9
localized [ "default" ] = localized . en ;
9
10
11
+ Globalization . extend = function ( deep ) {
12
+ var target = arguments [ 1 ] || { } ;
13
+ for ( var i = 2 , l = arguments . length ; i < l ; i ++ ) {
14
+ var source = arguments [ i ] ;
15
+ if ( source ) {
16
+ for ( var field in source ) {
17
+ var sourceVal = source [ field ] ;
18
+ if ( typeof sourceVal !== "undefined" ) {
19
+ if ( deep && ( isObject ( sourceVal ) || isArray ( sourceVal ) ) ) {
20
+ var targetVal = target [ field ] ;
21
+ // extend onto the existing value, or create a new one
22
+ targetVal = targetVal && ( isObject ( targetVal ) || isArray ( targetVal ) )
23
+ ? targetVal
24
+ : ( isArray ( sourceVal ) ? [ ] : { } ) ;
25
+ target [ field ] = this . extend ( true , targetVal , sourceVal ) ;
26
+ }
27
+ else {
28
+ target [ field ] = sourceVal ;
29
+ }
30
+ }
31
+ }
32
+ }
33
+ }
34
+ return target ;
35
+ }
10
36
11
- $ . findClosestCulture = function ( name ) {
37
+ Globalization . findClosestCulture = function ( name ) {
12
38
var match ;
13
39
if ( ! name ) {
14
- return $ . culture || $ . cultures [ "default" ] ;
40
+ return this . culture || this . cultures [ "default" ] ;
15
41
}
16
42
if ( isString ( name ) ) {
17
43
name = name . split ( ',' ) ;
18
44
}
19
45
if ( isArray ( name ) ) {
20
46
var lang ,
21
- cultures = $ . cultures ,
47
+ cultures = this . cultures ,
22
48
list = name ,
23
49
i , l = list . length ,
24
50
prioritized = [ ] ;
@@ -76,13 +102,13 @@ $.findClosestCulture = function(name) {
76
102
}
77
103
return match || null ;
78
104
}
79
- $ . preferCulture = function ( name ) {
80
- $ . culture = $ . findClosestCulture ( name ) || $ . cultures [ "default" ] ;
105
+ Globalization . preferCulture = function ( name ) {
106
+ this . culture = this . findClosestCulture ( name ) || this . cultures [ "default" ] ;
81
107
}
82
- $ . localize = function ( key , culture , value ) {
108
+ Globalization . localize = function ( key , culture , value ) {
83
109
if ( typeof culture === 'string' ) {
84
110
culture = culture || "default" ;
85
- culture = $ . cultures [ culture ] || { name : culture } ;
111
+ culture = this . cultures [ culture ] || { name : culture } ;
86
112
}
87
113
var local = localized [ culture . name ] ;
88
114
if ( arguments . length === 3 ) {
@@ -107,8 +133,8 @@ $.localize = function(key, culture, value) {
107
133
}
108
134
return typeof value === "undefined" ? null : value ;
109
135
}
110
- $ . format = function ( value , format , culture ) {
111
- culture = $ . findClosestCulture ( culture ) ;
136
+ Globalization . format = function ( value , format , culture ) {
137
+ culture = this . findClosestCulture ( culture ) ;
112
138
if ( typeof value === "number" ) {
113
139
value = formatNumber ( value , format , culture ) ;
114
140
}
@@ -117,11 +143,11 @@ $.format = function(value, format, culture) {
117
143
}
118
144
return value ;
119
145
}
120
- $ . parseInt = function ( value , radix , culture ) {
121
- return Math . floor ( $ . parseFloat ( value , radix , culture ) ) ;
146
+ Globalization . parseInt = function ( value , radix , culture ) {
147
+ return Math . floor ( this . parseFloat ( value , radix , culture ) ) ;
122
148
}
123
- $ . parseFloat = function ( value , radix , culture ) {
124
- culture = $ . findClosestCulture ( culture ) ;
149
+ Globalization . parseFloat = function ( value , radix , culture ) {
150
+ culture = this . findClosestCulture ( culture ) ;
125
151
var ret = NaN ,
126
152
nf = culture . numberFormat ;
127
153
@@ -195,8 +221,8 @@ $.parseFloat = function(value, radix, culture) {
195
221
}
196
222
return ret ;
197
223
}
198
- $ . parseDate = function ( value , formats , culture ) {
199
- culture = $ . findClosestCulture ( culture ) ;
224
+ Globalization . parseDate = function ( value , formats , culture ) {
225
+ culture = this . findClosestCulture ( culture ) ;
200
226
201
227
var date , prop , patterns ;
202
228
if ( formats ) {
@@ -228,7 +254,7 @@ $.parseDate = function(value, formats, culture) {
228
254
}
229
255
230
256
// 1. When defining a culture, all fields are required except the ones stated as optional.
231
- // 2. You can use $ .extend to copy an existing culture and provide only the differing values,
257
+ // 2. You can use Globalization .extend to copy an existing culture and provide only the differing values,
232
258
// a good practice since most cultures do not differ too much from the 'default' culture.
233
259
// DO use the 'default' culture if you do this, as it is the only one that definitely
234
260
// exists.
@@ -244,10 +270,10 @@ $.parseDate = function(value, formats, culture) {
244
270
// To define a culture, use the following pattern, which handles defining the culture based
245
271
// on the 'default culture, extending it with the existing culture if it exists, and defining
246
272
// it if it does not exist.
247
- // $ .cultures.foo = $ .extend(true, $ .extend(true, {}, $ .cultures['default'], fooCulture), $ .cultures.foo)
273
+ // Globalization .cultures.foo = Globalization .extend(true, Globalization .extend(true, {}, Globalization .cultures['default'], fooCulture), Globalization .cultures.foo)
248
274
249
- var cultures = $ . cultures = $ . cultures || { } ;
250
- var en = cultures [ "default" ] = cultures . en = $ . extend ( true , {
275
+ var cultures = Globalization . cultures = Globalization . cultures || { } ;
276
+ var en = cultures [ "default" ] = cultures . en = Globalization . extend ( true , {
251
277
// A unique name for the culture in the form <language code>-<country/region code>
252
278
name : "en" ,
253
279
// the name of the culture in the english language
@@ -451,7 +477,15 @@ function zeroPad(str, count, left) {
451
477
}
452
478
453
479
function isArray ( obj ) {
454
- return toString . call ( obj ) === "[object Array]" ;
480
+ return toString . call ( obj ) === "[object Array]" ;
481
+ }
482
+
483
+ function isString ( obj ) {
484
+ return toString . call ( obj ) === "[object String]" ;
485
+ }
486
+
487
+ function isObject ( obj ) {
488
+ return toString . call ( obj ) === "[object Object]" ;
455
489
}
456
490
457
491
function arrayIndexOf ( array , item ) {
@@ -1282,5 +1316,19 @@ function formatDate(value, format, culture) {
1282
1316
return ret . join ( '' ) ;
1283
1317
}
1284
1318
1285
- } ) ( jQuery ) ;
1319
+ // EXPORTS
1320
+
1321
+ window . Globalization = Globalization ;
1322
+
1323
+ //jQuery.findClosestCulture = Globalization.findClosestCulture;
1324
+ //jQuery.culture = Globalization.culture;
1325
+ //jQuery.cultures = Globalization.cultures
1326
+ //jQuery.preferCulture = Globalization.preferCulture
1327
+ //jQuery.localize = Globalization.localize
1328
+ //jQuery.format = Globalization.format
1329
+ //jQuery.parseInt = Globalization.parseInt
1330
+ //jQuery.parseFloat = Globalization.parseFloat
1331
+ //jQuery.parseDate = Globalization.parseDate
1332
+
1333
+ } ) ( ) ;
1286
1334
0 commit comments