i18n-js 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +2 -2
- data/README.rdoc +21 -7
- data/lib/i18n-js/version.rb +1 -1
- data/source/i18n.js +56 -43
- data/spec/i18n_spec.js +51 -0
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -24,14 +24,14 @@ Every time your application is started, the translations messages defined in you
|
|
24
24
|
To speed up the development process, you can automatically export your messages by adding something
|
25
25
|
like this to your <tt>ApplicationController</tt>:
|
26
26
|
|
27
|
-
|
28
|
-
|
27
|
+
class ApplicationController < ActionController::Base
|
28
|
+
before_filter :export_i18n_messages
|
29
29
|
|
30
|
-
|
30
|
+
private
|
31
31
|
def export_i18n_messages
|
32
32
|
SimplesIdeias::I18n.export! if Rails.env.development?
|
33
33
|
end
|
34
|
-
|
34
|
+
end
|
35
35
|
|
36
36
|
==== Configuration
|
37
37
|
|
@@ -130,7 +130,7 @@ Similar to Rails helpers, you have localize number and currency formatting.
|
|
130
130
|
I18n.l("percentage", 123.45);
|
131
131
|
// 123.450%
|
132
132
|
|
133
|
-
To have more control over number formatting, you can use the <tt>I18n.toNumber</tt>, <tt>I18n.toPercentage</tt> and <tt>I18n.
|
133
|
+
To have more control over number formatting, you can use the <tt>I18n.toNumber</tt>, <tt>I18n.toPercentage</tt>, <tt>I18n.toCurrency</tt> and <tt>I18n.toHumanSize</tt> functions.
|
134
134
|
|
135
135
|
I18n.toNumber(1000); // 1,000.000
|
136
136
|
I18n.toCurrency(1000); // $1,000.00
|
@@ -141,6 +141,7 @@ The +toNumber+ and +toPercentage+ functions accept the following options:
|
|
141
141
|
* +precision+: defaults to 3
|
142
142
|
* +separator+: defaults to <tt>.</tt>
|
143
143
|
* +delimiter+: defaults to <tt>,</tt>
|
144
|
+
* +strip_insignificant_zeros+: defaults to <tt>false</tt>
|
144
145
|
|
145
146
|
See some number formatting examples:
|
146
147
|
|
@@ -155,10 +156,23 @@ The +toCurrency+ function accepts the following options:
|
|
155
156
|
* +delimiter+: sets the thousands delimiter
|
156
157
|
* +format+: sets the format of the output string
|
157
158
|
* +unit+: sets the denomination of the currency
|
159
|
+
* +strip_insignificant_zeros+: defaults to <tt>false</tt>
|
158
160
|
|
159
161
|
You can provide only the options you want to override:
|
160
162
|
|
161
|
-
I18n.toCurrency(1000, {precision: 0});
|
163
|
+
I18n.toCurrency(1000, {precision: 0}); // $1,000
|
164
|
+
|
165
|
+
The +toHumanSize+ function accepts the following options:
|
166
|
+
|
167
|
+
* +precision+: defaults to 1
|
168
|
+
* +separator+: defaults to <tt>.</tt>
|
169
|
+
* +delimiter+: defaults to <tt>""</tt>
|
170
|
+
* +strip_insignificant_zeros+: defaults to <tt>false</tt>
|
171
|
+
* +format+: defaults to <tt>%n%u</tt>
|
172
|
+
|
173
|
+
I18n.toHumanSize(1234); // 1KB
|
174
|
+
I18n.toHumanSize(1234 * 1024); // 1MB
|
175
|
+
|
162
176
|
|
163
177
|
==== Date formatting
|
164
178
|
|
@@ -234,7 +248,7 @@ Once you've made your great commits:
|
|
234
248
|
4. Create an Issue[http://github.com/fnando/i18n-js/issues] with a link to your branch
|
235
249
|
5. That's it!
|
236
250
|
|
237
|
-
Please respect the indentation rules. And use
|
251
|
+
Please respect the indentation rules. And use 2 spaces, not tabs.
|
238
252
|
|
239
253
|
=== Running tests
|
240
254
|
|
data/lib/i18n-js/version.rb
CHANGED
data/source/i18n.js
CHANGED
@@ -41,7 +41,7 @@ I18n.lookup = function(scope, options) {
|
|
41
41
|
}
|
42
42
|
}
|
43
43
|
|
44
|
-
if (!messages && options.defaultValue
|
44
|
+
if (!messages && options.defaultValue !== null && options.defaultValue !== undefined) {
|
45
45
|
messages = options.defaultValue;
|
46
46
|
}
|
47
47
|
|
@@ -67,7 +67,7 @@ I18n.prepareOptions = function() {
|
|
67
67
|
}
|
68
68
|
|
69
69
|
for (var key in opts) {
|
70
|
-
if (options[key]
|
70
|
+
if (options[key] === undefined || options[key] === null) {
|
71
71
|
options[key] = opts[key];
|
72
72
|
}
|
73
73
|
}
|
@@ -91,7 +91,7 @@ I18n.interpolate = function(message, options) {
|
|
91
91
|
|
92
92
|
value = options[name];
|
93
93
|
|
94
|
-
if (options[name]
|
94
|
+
if (options[name] === null || options[name] === undefined) {
|
95
95
|
value = "[missing " + placeholder + " value]";
|
96
96
|
}
|
97
97
|
|
@@ -142,7 +142,7 @@ I18n.localize = function(scope, value) {
|
|
142
142
|
I18n.parseDate = function(d) {
|
143
143
|
var matches, date;
|
144
144
|
matches = d.toString().match(/(\d{4})-(\d{2})-(\d{2})(?:[ |T](\d{2}):(\d{2}):(\d{2}))?(Z)?/);
|
145
|
-
|
145
|
+
|
146
146
|
if (matches) {
|
147
147
|
// date/time strings: yyyy-mm-dd hh:mm:ss or yyyy-mm-dd or yyyy-mm-ddThh:mm:ssZ
|
148
148
|
for (var i = 1; i <= 6; i++) {
|
@@ -191,6 +191,7 @@ I18n.strftime = function(date, format) {
|
|
191
191
|
if (!options) {
|
192
192
|
return date.toString();
|
193
193
|
}
|
194
|
+
options.meridian = options.meridian || ["AM", "PM"];
|
194
195
|
|
195
196
|
var weekDay = date.getDay();
|
196
197
|
var day = date.getDate();
|
@@ -198,7 +199,7 @@ I18n.strftime = function(date, format) {
|
|
198
199
|
var month = date.getMonth() + 1;
|
199
200
|
var hour = date.getHours();
|
200
201
|
var hour12 = hour;
|
201
|
-
var meridian = hour >
|
202
|
+
var meridian = hour > 11 ? 1 : 0;
|
202
203
|
var secs = date.getSeconds();
|
203
204
|
var mins = date.getMinutes();
|
204
205
|
var offset = date.getTimezoneOffset();
|
@@ -208,6 +209,8 @@ I18n.strftime = function(date, format) {
|
|
208
209
|
|
209
210
|
if (hour12 > 12) {
|
210
211
|
hour12 = hour12 - 12;
|
212
|
+
} else if (hour12 === 0) {
|
213
|
+
hour12 = 12;
|
211
214
|
}
|
212
215
|
|
213
216
|
var padding = function(n) {
|
@@ -216,10 +219,10 @@ I18n.strftime = function(date, format) {
|
|
216
219
|
};
|
217
220
|
|
218
221
|
var f = format;
|
219
|
-
f = f.replace("%a", options
|
220
|
-
f = f.replace("%A", options
|
221
|
-
f = f.replace("%b", options
|
222
|
-
f = f.replace("%B", options
|
222
|
+
f = f.replace("%a", options.abbr_day_names[weekDay]);
|
223
|
+
f = f.replace("%A", options.day_names[weekDay]);
|
224
|
+
f = f.replace("%b", options.abbr_month_names[month]);
|
225
|
+
f = f.replace("%B", options.month_names[month]);
|
223
226
|
f = f.replace("%d", padding(day));
|
224
227
|
f = f.replace("%-d", day);
|
225
228
|
f = f.replace("%H", padding(hour));
|
@@ -230,7 +233,7 @@ I18n.strftime = function(date, format) {
|
|
230
233
|
f = f.replace("%-m", month);
|
231
234
|
f = f.replace("%M", padding(mins));
|
232
235
|
f = f.replace("%-M", mins);
|
233
|
-
f = f.replace("%p", meridian);
|
236
|
+
f = f.replace("%p", options.meridian[meridian]);
|
234
237
|
f = f.replace("%S", padding(secs));
|
235
238
|
f = f.replace("%-S", secs);
|
236
239
|
f = f.replace("%w", weekDay);
|
@@ -246,11 +249,11 @@ I18n.toNumber = function(number, options) {
|
|
246
249
|
options = this.prepareOptions(
|
247
250
|
options,
|
248
251
|
this.lookup("number.format"),
|
249
|
-
{precision: 3, separator: ".", delimiter: ","}
|
252
|
+
{precision: 3, separator: ".", delimiter: ",", strip_insignificant_zeros: false}
|
250
253
|
);
|
251
|
-
|
254
|
+
|
252
255
|
var negative = number < 0;
|
253
|
-
var string = Math.abs(number).toFixed(options
|
256
|
+
var string = Math.abs(number).toFixed(options.precision).toString();
|
254
257
|
var parts = string.split(".");
|
255
258
|
|
256
259
|
number = parts[0];
|
@@ -263,16 +266,27 @@ I18n.toNumber = function(number, options) {
|
|
263
266
|
number = number.substr(0, number.length -3);
|
264
267
|
}
|
265
268
|
|
266
|
-
var formattedNumber = n.join(options
|
269
|
+
var formattedNumber = n.join(options.delimiter);
|
267
270
|
|
268
|
-
if (options
|
269
|
-
formattedNumber += options
|
271
|
+
if (options.precision > 0) {
|
272
|
+
formattedNumber += options.separator + parts[1];
|
270
273
|
}
|
271
|
-
|
274
|
+
|
272
275
|
if (negative) {
|
273
276
|
formattedNumber = "-" + formattedNumber;
|
274
277
|
}
|
275
278
|
|
279
|
+
if (options.strip_insignificant_zeros) {
|
280
|
+
var regex = {
|
281
|
+
separator: new RegExp(options.separator.replace(/\./, "\\.") + "$")
|
282
|
+
, zeros: /0+$/
|
283
|
+
};
|
284
|
+
|
285
|
+
formattedNumber = formattedNumber
|
286
|
+
.replace(regex.zeros, "")
|
287
|
+
.replace(regex.separator, "");
|
288
|
+
}
|
289
|
+
|
276
290
|
return formattedNumber;
|
277
291
|
};
|
278
292
|
|
@@ -285,8 +299,8 @@ I18n.toCurrency = function(number, options) {
|
|
285
299
|
);
|
286
300
|
|
287
301
|
number = this.toNumber(number, options);
|
288
|
-
number = options
|
289
|
-
.replace("%u", options
|
302
|
+
number = options.format
|
303
|
+
.replace("%u", options.unit)
|
290
304
|
.replace("%n", number);
|
291
305
|
|
292
306
|
return number;
|
@@ -297,33 +311,32 @@ I18n.toHumanSize = function(number, options) {
|
|
297
311
|
, size = number
|
298
312
|
, iterations = 0
|
299
313
|
, unit
|
300
|
-
, precision
|
301
|
-
|
314
|
+
, precision
|
315
|
+
;
|
316
|
+
|
302
317
|
while (size >= kb && iterations < 4) {
|
303
318
|
size = size / kb;
|
304
319
|
iterations += 1;
|
305
|
-
}
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
};
|
316
|
-
|
320
|
+
}
|
321
|
+
|
322
|
+
if (iterations === 0) {
|
323
|
+
unit = this.t("number.human.storage_units.units.byte", {count: size});
|
324
|
+
precision = 0;
|
325
|
+
} else {
|
326
|
+
unit = this.t("number.human.storage_units.units." + [null, "kb", "mb", "gb", "tb"][iterations]);
|
327
|
+
precision = (size - Math.floor(size) === 0) ? 0 : 1;
|
328
|
+
}
|
329
|
+
|
317
330
|
options = this.prepareOptions(
|
318
331
|
options,
|
319
332
|
{precision: precision, format: "%n%u", delimiter: ""}
|
320
333
|
);
|
321
|
-
|
334
|
+
|
322
335
|
number = this.toNumber(size, options);
|
323
|
-
number = options
|
336
|
+
number = options.format
|
324
337
|
.replace("%u", unit)
|
325
338
|
.replace("%n", number);
|
326
|
-
|
339
|
+
|
327
340
|
return number;
|
328
341
|
};
|
329
342
|
|
@@ -341,28 +354,28 @@ I18n.toPercentage = function(number, options) {
|
|
341
354
|
|
342
355
|
I18n.pluralize = function(count, scope, options) {
|
343
356
|
var translation;
|
344
|
-
|
357
|
+
|
345
358
|
try {
|
346
359
|
translation = this.lookup(scope, options);
|
347
360
|
} catch (error) {}
|
348
|
-
|
361
|
+
|
349
362
|
if (!translation) {
|
350
363
|
return this.missingTranslation(scope);
|
351
364
|
}
|
352
|
-
|
365
|
+
|
353
366
|
var message;
|
354
367
|
options = this.prepareOptions(options);
|
355
|
-
options
|
368
|
+
options.count = count.toString();
|
356
369
|
|
357
370
|
switch(Math.abs(count)) {
|
358
371
|
case 0:
|
359
|
-
message = translation
|
372
|
+
message = translation.zero || translation.none || translation.other || this.missingTranslation(scope, "zero");
|
360
373
|
break;
|
361
374
|
case 1:
|
362
|
-
message = translation
|
375
|
+
message = translation.one || this.missingTranslation(scope, "one");
|
363
376
|
break;
|
364
377
|
default:
|
365
|
-
message = translation
|
378
|
+
message = translation.other || this.missingTranslation(scope, "other");
|
366
379
|
}
|
367
380
|
|
368
381
|
return this.interpolate(message, options);
|
data/spec/i18n_spec.js
CHANGED
@@ -76,6 +76,21 @@ describe("I18n.js", function(){
|
|
76
76
|
am: "AM",
|
77
77
|
pm: "PM"
|
78
78
|
}
|
79
|
+
},
|
80
|
+
|
81
|
+
"en-US": {
|
82
|
+
date: {
|
83
|
+
formats: {
|
84
|
+
"default": "%d/%m/%Y",
|
85
|
+
"short": "%d de %B",
|
86
|
+
"long": "%d de %B de %Y"
|
87
|
+
},
|
88
|
+
day_names: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
|
89
|
+
abbr_day_names: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
|
90
|
+
month_names: [null, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
|
91
|
+
abbr_month_names: [null, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec"],
|
92
|
+
meridian: ["am", "pm"]
|
93
|
+
}
|
79
94
|
}
|
80
95
|
};
|
81
96
|
});
|
@@ -505,6 +520,33 @@ describe("I18n.js", function(){
|
|
505
520
|
expect(I18n.strftime(date, "%z")).toBeEqualTo("+0545");
|
506
521
|
});
|
507
522
|
|
523
|
+
specify("date formatting with custom meridian", function(){
|
524
|
+
I18n.locale = "en-US";
|
525
|
+
var date = new Date(2009, 3, 26, 19, 35, 44);
|
526
|
+
expect(I18n.strftime(date, "%p")).toBeEqualTo("pm");
|
527
|
+
});
|
528
|
+
|
529
|
+
specify("date formatting meridian boundaries", function(){
|
530
|
+
I18n.locale = "en-US";
|
531
|
+
var date = new Date(2009, 3, 26, 0, 35, 44);
|
532
|
+
expect(I18n.strftime(date, "%p")).toBeEqualTo("am");
|
533
|
+
|
534
|
+
date = new Date(2009, 3, 26, 12, 35, 44);
|
535
|
+
expect(I18n.strftime(date, "%p")).toBeEqualTo("pm");
|
536
|
+
});
|
537
|
+
|
538
|
+
specify("date formatting hour12 values", function(){
|
539
|
+
I18n.locale = "pt-BR";
|
540
|
+
var date = new Date(2009, 3, 26, 19, 35, 44);
|
541
|
+
expect(I18n.strftime(date, "%I")).toBeEqualTo("07");
|
542
|
+
|
543
|
+
date = new Date(2009, 3, 26, 12, 35, 44);
|
544
|
+
expect(I18n.strftime(date, "%I")).toBeEqualTo("12");
|
545
|
+
|
546
|
+
date = new Date(2009, 3, 26, 0, 35, 44);
|
547
|
+
expect(I18n.strftime(date, "%I")).toBeEqualTo("12");
|
548
|
+
});
|
549
|
+
|
508
550
|
specify("localize date strings", function(){
|
509
551
|
I18n.locale = "pt-BR";
|
510
552
|
|
@@ -683,4 +725,13 @@ describe("I18n.js", function(){
|
|
683
725
|
specify("return number as human size using custom options", function(){
|
684
726
|
expect(I18n.toHumanSize(1024 * 1.6, {precision: 0})).toBeEqualTo("2KB");
|
685
727
|
});
|
728
|
+
|
729
|
+
specify("return number without insignificant zeros", function(){
|
730
|
+
options = {precision: 4, strip_insignificant_zeros: true};
|
731
|
+
|
732
|
+
expect(I18n.toNumber(65, options)).toBeEqualTo("65");
|
733
|
+
expect(I18n.toNumber(1.2, options)).toBeEqualTo("1.2");
|
734
|
+
expect(I18n.toCurrency(1.2, options)).toBeEqualTo("$1.2");
|
735
|
+
expect(I18n.toHumanSize(1.2, options)).toBeEqualTo("1.2Bytes");
|
736
|
+
});
|
686
737
|
});
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: i18n-js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Nando Vieira
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-03-11 00:00:00 -03:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
requirements: []
|
136
136
|
|
137
137
|
rubyforge_project:
|
138
|
-
rubygems_version: 1.
|
138
|
+
rubygems_version: 1.6.0
|
139
139
|
signing_key:
|
140
140
|
specification_version: 3
|
141
141
|
summary: It's a small library to provide the Rails I18n translations on the Javascript.
|