Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mamayaya1
GitHub Repository: mamayaya1/game
Path: blob/main/projects/cupcake2048/js/l10n.js
4626 views
1
/*
2
* l10n.js
3
* 2013-04-18
4
*
5
* By Eli Grey, http://eligrey.com
6
* Licensed under the X11/MIT License
7
* See LICENSE.md
8
*/
9
10
/*global XMLHttpRequest, setTimeout, document, navigator, ActiveXObject*/
11
12
/*! @source http://purl.eligrey.com/github/l10n.js/blob/master/l10n.js*/
13
14
(function () {
15
"use strict";
16
17
var
18
undef_type = "undefined"
19
, string_type = "string"
20
, nav = self.navigator
21
, String_ctr = String
22
, has_own_prop = Object.prototype.hasOwnProperty
23
, load_queues = {}
24
, localizations = {}
25
, FALSE = !1
26
, TRUE = !0
27
// the official format is application/vnd.oftn.l10n+json, though l10n.js will also
28
// accept application/x-l10n+json and application/l10n+json
29
, l10n_js_media_type = /^\s*application\/(?:vnd\.oftn\.|x-)?l10n\+json\s*(?:$|;)/i
30
, XHR
31
32
// property minification aids
33
, $locale = "locale"
34
, $default_locale = "defaultLocale"
35
, $to_locale_string = "toLocaleString"
36
, $to_lowercase = "toLowerCase"
37
38
, array_index_of = Array.prototype.indexOf || function (item) {
39
var
40
len = this.length
41
, i = 0
42
;
43
44
for (; i < len; i++) {
45
if (i in this && this[i] === item) {
46
return i;
47
}
48
}
49
50
return -1;
51
}
52
, request_JSON = function (uri) {
53
var req = new XHR();
54
55
// sadly, this has to be blocking to allow for a graceful degrading API
56
req.open("GET", uri, FALSE);
57
req.send(null);
58
59
if (req.status !== 200) {
60
// warn about error without stopping execution
61
setTimeout(function () {
62
// Error messages are not localized as not to cause an infinite loop
63
var l10n_err = new Error("Unable to load localization data: " + uri);
64
l10n_err.name = "Localization Error";
65
throw l10n_err;
66
}, 0);
67
68
return {};
69
} else {
70
return JSON.parse(req.responseText);
71
}
72
}
73
, load = String_ctr[$to_locale_string] = function (data) {
74
// don't handle function.toLocaleString(indentationAmount:Number)
75
if (arguments.length > 0 && typeof data !== "number") {
76
if (typeof data === string_type) {
77
load(request_JSON(data));
78
} else if (data === FALSE) {
79
// reset all localizations
80
localizations = {};
81
} else {
82
// Extend current localizations instead of completely overwriting them
83
var locale, localization, message;
84
for (locale in data) {
85
if (has_own_prop.call(data, locale)) {
86
localization = data[locale];
87
locale = locale[$to_lowercase]();
88
89
if (!(locale in localizations) || localization === FALSE) {
90
// reset locale if not existing or reset flag is specified
91
localizations[locale] = {};
92
}
93
94
if (localization === FALSE) {
95
continue;
96
}
97
98
// URL specified
99
if (typeof localization === string_type) {
100
if (String_ctr[$locale][$to_lowercase]().indexOf(locale) === 0) {
101
localization = request_JSON(localization);
102
} else {
103
// queue loading locale if not needed
104
if (!(locale in load_queues)) {
105
load_queues[locale] = [];
106
}
107
load_queues[locale].push(localization);
108
continue;
109
}
110
}
111
112
for (message in localization) {
113
if (has_own_prop.call(localization, message)) {
114
localizations[locale][message] = localization[message];
115
}
116
}
117
}
118
}
119
}
120
}
121
// Return what function.toLocaleString() normally returns
122
return Function.prototype[$to_locale_string].apply(String_ctr, arguments);
123
}
124
, process_load_queue = function (locale) {
125
var
126
queue = load_queues[locale]
127
, i = 0
128
, len = queue.length
129
, localization
130
;
131
132
for (; i < len; i++) {
133
localization = {};
134
localization[locale] = request_JSON(queue[i]);
135
load(localization);
136
}
137
138
delete load_queues[locale];
139
}
140
, use_default
141
, localize = String_ctr.prototype[$to_locale_string] = function () {
142
if (typeof this === undef_type) {
143
return this;
144
}
145
146
var
147
using_default = use_default
148
, current_locale = String_ctr[using_default ? $default_locale : $locale]
149
, parts = current_locale[$to_lowercase]().split("-")
150
, i = parts.length
151
, this_val = this.valueOf()
152
, locale
153
;
154
155
use_default = FALSE;
156
157
// Iterate through locales starting at most-specific until a localization is found
158
do {
159
locale = parts.slice(0, i).join("-");
160
// load locale if not loaded
161
if (locale in load_queues) {
162
process_load_queue(locale);
163
}
164
if (locale in localizations && this_val in localizations[locale]) {
165
return localizations[locale][this_val];
166
}
167
}
168
while (i --> 1);
169
170
if (!using_default && String_ctr[$default_locale]) {
171
use_default = TRUE;
172
return localize.call(this_val);
173
}
174
175
return this_val;
176
}
177
;
178
179
if (typeof XMLHttpRequest === undef_type && typeof ActiveXObject !== undef_type) {
180
var AXO = ActiveXObject;
181
182
XHR = function () {
183
try {
184
return new AXO("Msxml2.XMLHTTP.6.0");
185
} catch (xhrEx1) {}
186
try {
187
return new AXO("Msxml2.XMLHTTP.3.0");
188
} catch (xhrEx2) {}
189
try {
190
return new AXO("Msxml2.XMLHTTP");
191
} catch (xhrEx3) {}
192
193
throw new Error("XMLHttpRequest not supported by this browser.");
194
};
195
} else {
196
XHR = XMLHttpRequest;
197
}
198
199
String_ctr[$default_locale] = String_ctr[$default_locale] || "";
200
String_ctr[$locale] = nav && (nav.language || nav.userLanguage) || "";
201
202
if (typeof document !== undef_type) {
203
var
204
elts = document.getElementsByTagName("link")
205
, i = elts.length
206
, localization
207
;
208
209
while (i--) {
210
var
211
elt = elts[i]
212
, rel = (elt.getAttribute("rel") || "")[$to_lowercase]().split(/\s+/)
213
;
214
215
if (l10n_js_media_type.test(elt.type)) {
216
if (array_index_of.call(rel, "localizations") !== -1) {
217
// multiple localizations
218
load(elt.getAttribute("href"));
219
} else if (array_index_of.call(rel, "localization") !== -1) {
220
// single localization
221
localization = {};
222
localization[(elt.getAttribute("hreflang") || "")[$to_lowercase]()] =
223
elt.getAttribute("href");
224
load(localization);
225
}
226
}
227
}
228
}
229
230
}());
231
232
function Localize(key) {
233
var string = '%' + key;
234
return string.toLocaleString();
235
};
236
237
function LocalizeElement(className) {
238
var element = document.getElementsByClassName(className);
239
if (element[0]) element[0].innerHTML = Localize(className);
240
}
241