-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathjsi18n_patch.js
214 lines (208 loc) · 7.39 KB
/
jsi18n_patch.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
/**
* Howdy! This script "patches" Google Chrome's implementation of the i18n API
* so that it conforms more fully with ECMAScript's Internationalization API.
* In particular it makes Dates.toLocale[Date,Time]String(), and
* Number.toLocaleString() work as defined by the ECMAScript specification.
*
* Simple usage examples:
* //dates
* date = new Date();
* date.toLocaleString("en-us", {weekday: 'long'}); //returns Monday
* date.toLocaleTimeString("ar"); //returns time in arabic
* //numbers
* (123456).toLocaleString("en-us"); // returns "123,456"
*
* Found a bug? want to contribue?
* please visit: https://github.com/marcoscaceres/jsi18n
*
* Detailed documentation on how to use this script:
* http://marcoscaceres.github.com/jsi18n/
*
* See also the ECMAScript i18n specs page:
* http://wiki.ecmascript.org/doku.php?id=globalization:specification_drafts
*
* Copyright (c) 2012, Marcos Cáceres <[email protected]>
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
* OR OTHER DEALINGS IN THE SOFTWARE.
*
**/
(function(global) {
'use strict';
var lang = ['i'], //this will cause an exception
intlProps = {
'writable': true,
'enumerable': false,
'configurable': true
};
//check if supported
if ('Intl' in global) {
//It's supported, don't need to do anything!
console.log('i18n appears to be supported.');
return;
}
//Check if Chrome's implementation is available
if ('v8Intl' in global) {
//create a reference to Intl
intlProps.value = global.v8Intl;
Object.defineProperty(global, "Intl", intlProps);
//patch V8 functionality
patchV8();
return;
}
//no match found...
return console.warn('bummer, i18n API is not supported \u2639');
/**
* patchV8
* monkey-patch the standard functions in V8 to use i18n formatters
**/
function patchV8() {
var date = new Date();
var num = 12345;
var str = "";
var objsToPatch = [];
var timeDefaults = Object.create(null);
timeDefaults.hour = timeDefaults.minute = timeDefaults.second = 'numeric';
objsToPatch.push({
test: date,
func: 'toLocaleString',
proto: Date.prototype,
formatter: global.Intl.DateTimeFormat,
defaults: undefined
});
objsToPatch.push({
test: date,
func: 'toLocaleDateString',
proto: Date.prototype,
formatter: global.Intl.DateTimeFormat,
defaults: undefined
});
objsToPatch.push({
test: date,
func: 'toLocaleTimeString',
proto: Date.prototype,
formatter: global.Intl.DateTimeFormat,
defaults: timeDefaults
});
objsToPatch.push({
test: num,
func: 'toLocaleString',
proto: Number.prototype,
formatter: global.Intl.NumberFormat,
defaults: undefined
});
//start patching as needed
objsToPatch.forEach(checkAndPatch);
//patch String.prototype.localeCompare
patchLocaleCompare();
function patchLocaleCompare() {
try {
str.localeCompare("", lang);
} catch (e) {
console.log("localeCompare already supported. No need to patch.");
return;
}
String.prototype.localeCompare = (function(old) {
//create the patched function, and then return it
var patchedFunction = function(that, locales, options) {
var collator;
//Call the original "native code" function
if (locales === undefined && options === undefined) {
if (that === undefined) {
return old.call(this);
}
return old.call(this, that);
}
//clean up and normalize values
locales = String(locales).split(',');
options = Object(options);
//collate this and that
collator = new global.Intl.Collator(locales, options);
return collator.compare(this, that);
};
//monkey patch!
return patchedFunction;
}(String.prototype.localeCompare));
try {
str.localeCompare("", lang);
} catch (e) {
console.log("String.prototype.localeCompare patched succesfully.");
return;
}
console.warn("Was not able to succesfully patch localeCompare");
}
//Check if needs to be pached and patch if it does
function checkAndPatch(obj) {
var patched = false,
supported = true;
try {
obj.test[obj.func](lang);
supported = false;
patch(obj.func, obj.proto, obj.formatter, obj.defaults);
patched = true;
obj.test[obj.func](lang);
supported = false;
} catch (e) {
supported = true;
}
if (!patched && supported) {
console.log(obj.func + "already supported. No need to patch.");
return;
}
if (patched && supported) {
console.log("Succefully patched " + obj.func);
return;
}
console.warn("Was not able to succesfully patch " + obj.func);
}
//monkey patch by keeping native functionality when needed
function patch(functionName, ofPrototype, i18nformatter, i18nDefaults) {
//override native function of a given prototype
//(e.g., Number.prototype['toLocaleString'])
ofPrototype[functionName] = (function(old, formatter, defaults) {
//create the patched function, and then return it
var patchedFunction = function(locales, options) {
var optionsClone = Object.create({});
//Call the original "native code" function
if (locales === undefined && options === undefined) {
return old.call(this);
}
//clean up and normalize values
locales = String(locales).split(',');
options = Object(options);
//clone options, so not to change the original object
for (var i in options) {
optionsClone[i] = options[i];
}
//set defaults for output, as Chrome does not do this sometimes
if (defaults) {
for (var i in defaults) {
if (!optionsClone.hasOwnProperty(i)) {
optionsClone[i] = defaults[i];
}
}
}
//localize and format the object and return result
return (new formatter(locales, optionsClone)).format(this);
};
//monkey patch!
return patchedFunction;
}(ofPrototype[functionName], i18nformatter, i18nDefaults));
}
}
}(this));