-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathlocalize_test.js
280 lines (280 loc) · 8.34 KB
/
localize_test.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
(function($) {
var applyTagAttributes, localizableTagWithDataLocalize, localizableTagWithRel;
localizableTagWithRel = function(tag, localizeKey, attributes) {
var t;
t = $("<" + tag + ">").attr("rel", "localize[" + localizeKey + "]");
return applyTagAttributes(t, attributes);
};
localizableTagWithDataLocalize = function(tag, localizeKey, attributes) {
var t;
t = $("<" + tag + ">").attr("data-localize", localizeKey);
return applyTagAttributes(t, attributes);
};
applyTagAttributes = function(tag, attributes) {
var k, v;
if (attributes.text != null) {
tag.text(attributes.text);
delete attributes.text;
}
if (attributes.val != null) {
tag.val(attributes.val);
delete attributes.val;
}
for (k in attributes) {
v = attributes[k];
tag.attr(k, v);
}
return tag;
};
module("Basic Usage");
setup(function() {
return this.testOpts = {
language: "ja",
pathPrefix: "lang"
};
});
test("basic tag text substitution", function() {
var t;
t = localizableTagWithRel("p", "basic", {
text: "basic fail"
});
t.localize("test", this.testOpts);
return equal(t.text(), "basic success");
});
test("basic tag text substitution using data-localize instead of rel", function() {
var t;
t = localizableTagWithDataLocalize("p", "basic", {
text: "basic fail"
});
t.localize("test", this.testOpts);
return equal(t.text(), "basic success");
});
test("basic tag text substitution with nested key", function() {
var t;
t = localizableTagWithRel("p", "test.nested", {
text: "nested fail"
});
t.localize("test", this.testOpts);
return equal(t.text(), "nested success");
});
test("basic tag text substitution for special title key", function() {
var t;
t = localizableTagWithDataLocalize("p", "with_title", {
text: "with_title element fail",
title: "with_title title fail"
});
t.localize("test", this.testOpts);
equal(t.text(), "with_title text success");
return equal(t.attr("title"), "with_title title success");
});
test("input tag value substitution", function() {
var t;
t = localizableTagWithRel("input", "test.input", {
val: "input fail"
});
t.localize("test", this.testOpts);
return equal(t.val(), "input success");
});
test("input tag value after second localization without key", function() {
var t;
t = localizableTagWithRel("input", "test.input", {
val: "input fail"
});
t.localize("test", this.testOpts);
t.localize("test2", this.testOpts);
return equal(t.val(), "input success");
});
test("input tag placeholder substitution", function() {
var t;
t = localizableTagWithRel("input", "test.input", {
placeholder: "placeholder fail"
});
t.localize("test", this.testOpts);
return equal(t.attr("placeholder"), "input success");
});
test("titled input tag value substitution", function() {
var t;
t = localizableTagWithRel("input", "test.input_as_obj", {
val: "input_as_obj fail"
});
t.localize("test", this.testOpts);
return equal(t.val(), "input_as_obj value success");
});
test("titled input tag title substitution", function() {
var t;
t = localizableTagWithRel("input", "test.input_as_obj", {
val: "input_as_obj fail"
});
t.localize("test", this.testOpts);
return equal(t.attr("title"), "input_as_obj title success");
});
test("titled input tag placeholder substitution", function() {
var t;
t = localizableTagWithRel("input", "test.input_as_obj", {
placeholder: "placeholder fail"
});
t.localize("test", this.testOpts);
return equal(t.attr("placeholder"), "input_as_obj value success");
});
test("image tag src, alt, and title substitution", function() {
var t;
t = localizableTagWithRel("img", "test.ruby_image", {
src: "ruby_square.gif",
alt: "a square ruby",
title: "A Square Ruby"
});
t.localize("test", this.testOpts);
equal(t.attr("src"), "ruby_round.gif");
equal(t.attr("alt"), "a round ruby");
return equal(t.attr("title"), "A Round Ruby");
});
test("chained call", function() {
var t;
t = localizableTagWithRel("p", "basic", {
text: "basic fail"
});
t.localize("test", this.testOpts).localize("test", this.testOpts);
return equal(t.text(), "basic success");
});
test("alternative file extension", function() {
var t;
t = localizableTagWithRel("p", "basic", {
text: "basic fail"
});
t.localize("test", $.extend({
fileExtension: "foo"
}, this.testOpts));
return equal(t.text(), "basic success foo");
});
moreSetup(function() {
return this.t = $('<select> <optgroup rel="localize[test.optgroup]" label="optgroup fail"> <option rel="localize[test.option]" value="1">option fail</option> </optgroup> </select>');
});
test("optgroup tag label substitution", function() {
var t;
t = this.t.find("optgroup");
t.localize("test", this.testOpts);
return equal(t.attr("label"), "optgroup success");
});
test("option tag text substitution", function() {
var t;
t = this.t.find("option");
t.localize("test", this.testOpts);
return equal(t.text(), "option success");
});
module("Options");
test("fallback language loads", function() {
var opts, t;
opts = {
language: "fo",
fallback: "ja",
pathPrefix: "lang"
};
t = localizableTagWithRel("p", "basic", {
text: "basic fail"
});
t.localize("test", opts);
return equal(t.text(), "basic success");
});
test("pathPrefix loads lang files from custom path", function() {
var opts, t;
opts = {
language: "fo",
pathPrefix: "/test/lang/custom"
};
t = localizableTagWithRel("p", "path_prefix", {
text: "pathPrefix fail"
});
t.localize("test", opts);
return equal(t.text(), "pathPrefix success");
});
test("custom callback is fired", function() {
var opts, t;
opts = {
language: "ja",
pathPrefix: "lang"
};
opts.callback = function(data, defaultCallback) {
data.custom_callback = "custom callback success";
return defaultCallback(data);
};
t = localizableTagWithRel("p", "custom_callback", {
text: "custom callback fail"
});
t.localize("test", opts);
return equal(t.text(), "custom callback success");
});
test("language with country code", function() {
var opts, t;
opts = {
language: "ja-XX",
pathPrefix: "lang"
};
t = localizableTagWithRel("p", "message", {
text: "country code fail"
});
t.localize("test", opts);
return equal(t.text(), "country code success");
});
test("load language-country code json file directly", function() {
var opts, t;
opts = {
language: "zh-CN",
pathPrefix: "lang"
};
t = localizableTagWithRel("p", "message", {
text: "language-country code fail"
});
t.localize("test", opts);
return equal(t.text(), "language-country code success");
});
module("Language optimization");
test("skipping language using string match", function() {
var opts, t;
opts = {
language: "en",
pathPrefix: "lang",
skipLanguage: "en"
};
t = localizableTagWithRel("p", "en_message", {
text: "en not loaded"
});
t.localize("test", opts);
return equal(t.text(), "en not loaded");
});
test("skipping language using regex match", function() {
var opts, t;
opts = {
language: "en-US",
pathPrefix: "lang",
skipLanguage: /^en/
};
t = localizableTagWithRel("p", "en_us_message", {
text: "en-US not loaded"
});
t.localize("test", opts);
return equal(t.text(), "en-US not loaded");
});
return test("skipping language using array match", function() {
var opts, t;
opts = {
language: "en",
pathPrefix: "lang",
skipLanguage: ["en", "en-US"]
};
t = localizableTagWithRel("p", "en_message", {
text: "en not loaded"
});
t.localize("test", opts);
equal(t.text(), "en not loaded");
opts = {
language: "en-US",
pathPrefix: "lang",
skipLanguage: ["en", "en-US"]
};
t = localizableTagWithRel("p", "en_us_message", {
text: "en-US not loaded"
});
t.localize("test", opts);
return equal(t.text(), "en-US not loaded");
});
})(jQuery);