-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathjquery.localize.js
191 lines (190 loc) · 6.48 KB
/
jquery.localize.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
/*
Copyright (c) Jim Garvin (http://github.com/coderifous), 2008.
Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
Written by Jim Garvin (@coderifous) for use on LMGTFY.com.
http://github.com/coderifous/jquery-localize
Based off of Keith Wood's Localisation jQuery plugin.
http://keith-wood.name/localisation.html
*/
(function($) {
var normaliseLang;
normaliseLang = function(lang) {
lang = lang.replace(/_/, '-').toLowerCase();
if (lang.length > 3) {
lang = lang.substring(0, 3) + lang.substring(3).toUpperCase();
}
return lang;
};
$.defaultLanguage = normaliseLang(navigator.language || navigator.userLanguage);
$.localize = function(pkg, options) {
var defaultCallback, fileExtension, intermediateLangData, jsonCall, lang, loadLanguage, localizeElement, localizeForSpecialKeys, localizeImageElement, localizeInputElement, localizeOptgroupElement, notifyDelegateLanguageLoaded, regexify, setAttrFromValueForKey, setTextFromValueForKey, valueForKey, wrappedSet;
if (options == null) {
options = {};
}
wrappedSet = this;
intermediateLangData = {};
fileExtension = options.fileExtension || "json";
loadLanguage = function(pkg, lang, level) {
var file;
if (level == null) {
level = 1;
}
switch (level) {
case 1:
intermediateLangData = {};
if (options.loadBase) {
file = pkg + ("." + fileExtension);
return jsonCall(file, pkg, lang, level);
} else {
return loadLanguage(pkg, lang, 2);
}
break;
case 2:
if (lang.length >= 2) {
file = "" + pkg + "-" + (lang.substring(0, 2)) + "." + fileExtension;
return jsonCall(file, pkg, lang, level);
}
break;
case 3:
if (lang.length >= 5) {
file = "" + pkg + "-" + (lang.substring(0, 5)) + "." + fileExtension;
return jsonCall(file, pkg, lang, level);
}
}
};
jsonCall = function(file, pkg, lang, level) {
var ajaxOptions, errorFunc, successFunc;
if (options.pathPrefix != null) {
file = "" + options.pathPrefix + "/" + file;
}
successFunc = function(d) {
$.extend(intermediateLangData, d);
notifyDelegateLanguageLoaded(intermediateLangData);
return loadLanguage(pkg, lang, level + 1);
};
errorFunc = function() {
if (level < 3) {
loadLanguage(pkg, lang, level + 1);
}
if (options.fallback && options.fallback !== lang) {
return loadLanguage(pkg, options.fallback);
}
};
ajaxOptions = {
url: file,
dataType: "json",
async: false,
timeout: options.timeout != null ? options.timeout : 500,
success: successFunc,
error: errorFunc
};
if (window.location.protocol === "file:") {
ajaxOptions.error = function(xhr) {
return successFunc($.parseJSON(xhr.responseText));
};
}
return $.ajax(ajaxOptions);
};
notifyDelegateLanguageLoaded = function(data) {
if (options.callback != null) {
return options.callback(data, defaultCallback);
} else {
return defaultCallback(data);
}
};
defaultCallback = function(data) {
$.localize.data[pkg] = data;
return wrappedSet.each(function() {
var elem, key, value;
elem = $(this);
key = elem.data("localize");
key || (key = elem.attr("rel").match(/localize\[(.*?)\]/)[1]);
value = valueForKey(key, data);
if (value != null) {
return localizeElement(elem, key, value);
}
});
};
localizeElement = function(elem, key, value) {
if (elem.is('input')) {
localizeInputElement(elem, key, value);
} else if (elem.is('img')) {
localizeImageElement(elem, key, value);
} else if (elem.is('optgroup')) {
localizeOptgroupElement(elem, key, value);
} else if (!$.isPlainObject(value)) {
elem.html(value);
}
if ($.isPlainObject(value)) {
return localizeForSpecialKeys(elem, value);
}
};
localizeInputElement = function(elem, key, value) {
var val;
val = $.isPlainObject(value) ? value.value : value;
if (elem.is("[placeholder]")) {
return elem.attr("placeholder", val);
} else {
return elem.val(val);
}
};
localizeForSpecialKeys = function(elem, value) {
setAttrFromValueForKey(elem, "title", value);
return setTextFromValueForKey(elem, "text", value);
};
localizeOptgroupElement = function(elem, key, value) {
return elem.attr("label", value);
};
localizeImageElement = function(elem, key, value) {
setAttrFromValueForKey(elem, "alt", value);
return setAttrFromValueForKey(elem, "src", value);
};
valueForKey = function(key, data) {
var keys, value, _i, _len;
keys = key.split(/\./);
value = data;
for (_i = 0, _len = keys.length; _i < _len; _i++) {
key = keys[_i];
value = value != null ? value[key] : null;
}
return value;
};
setAttrFromValueForKey = function(elem, key, value) {
value = valueForKey(key, value);
if (value != null) {
return elem.attr(key, value);
}
};
setTextFromValueForKey = function(elem, key, value) {
value = valueForKey(key, value);
if (value != null) {
return elem.text(value);
}
};
regexify = function(string_or_regex_or_array) {
var thing;
if (typeof string_or_regex_or_array === "string") {
return "^" + string_or_regex_or_array + "$";
} else if (string_or_regex_or_array.length != null) {
return ((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = string_or_regex_or_array.length; _i < _len; _i++) {
thing = string_or_regex_or_array[_i];
_results.push(regexify(thing));
}
return _results;
})()).join("|");
} else {
return string_or_regex_or_array;
}
};
lang = normaliseLang(options.language ? options.language : $.defaultLanguage);
if (!(options.skipLanguage && lang.match(regexify(options.skipLanguage)))) {
loadLanguage(pkg, lang, 1);
}
return wrappedSet;
};
$.fn.localize = $.localize;
return $.localize.data = {};
})(jQuery);