i18n-js 2.1.2 → 3.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.editorconfig +24 -0
- data/.gitignore +5 -4
- data/.npmignore +27 -0
- data/.travis.yml +37 -0
- data/Appraisals +20 -0
- data/CHANGELOG.md +354 -0
- data/Gemfile +1 -1
- data/README.md +872 -0
- data/Rakefile +19 -7
- data/app/assets/javascripts/i18n/filtered.js.erb +23 -0
- data/app/assets/javascripts/i18n/shims.js +208 -0
- data/app/assets/javascripts/i18n/translations.js +3 -0
- data/app/assets/javascripts/i18n.js +1077 -0
- data/gemfiles/i18n_0_6.gemfile +7 -0
- data/gemfiles/i18n_0_7.gemfile +7 -0
- data/gemfiles/i18n_0_8.gemfile +7 -0
- data/gemfiles/i18n_0_9.gemfile +7 -0
- data/gemfiles/i18n_1_0.gemfile +7 -0
- data/i18n-js.gemspec +11 -9
- data/lib/i18n/js/dependencies.rb +59 -0
- data/lib/i18n/js/engine.rb +87 -0
- data/lib/i18n/js/fallback_locales.rb +70 -0
- data/lib/{i18n-js → i18n/js}/middleware.rb +32 -9
- data/lib/i18n/js/private/hash_with_symbol_keys.rb +36 -0
- data/lib/i18n/js/segment.rb +88 -0
- data/lib/i18n/js/utils.rb +52 -0
- data/lib/i18n/js/version.rb +7 -0
- data/lib/i18n/js.rb +242 -0
- data/lib/i18n-js.rb +1 -177
- data/lib/rails/generators/i18n/js/config/config_generator.rb +19 -0
- data/{config → lib/rails/generators/i18n/js/config/templates}/i18n-js.yml +11 -6
- data/lib/tasks/export.rake +8 -0
- data/package.json +25 -0
- data/spec/fixtures/custom_path.yml +5 -0
- data/spec/fixtures/default.yml +5 -0
- data/spec/fixtures/erb.yml +5 -0
- data/spec/fixtures/except_condition.yml +7 -0
- data/spec/fixtures/js_export_dir_custom.yml +7 -0
- data/spec/fixtures/js_export_dir_none.yml +6 -0
- data/spec/fixtures/js_extend_parent.yml +6 -0
- data/spec/fixtures/js_extend_segment.yml +6 -0
- data/spec/fixtures/js_file_per_locale.yml +7 -0
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +4 -0
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +6 -0
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +4 -0
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +4 -0
- data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +4 -0
- data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +4 -0
- data/spec/fixtures/js_file_with_namespace_and_pretty_print.yml +7 -0
- data/spec/fixtures/js_sort_translation_keys_false.yml +6 -0
- data/spec/fixtures/js_sort_translation_keys_true.yml +6 -0
- data/spec/{resources → fixtures}/locales.yml +13 -1
- data/spec/fixtures/multiple_conditions.yml +7 -0
- data/spec/fixtures/multiple_conditions_per_locale.yml +7 -0
- data/spec/fixtures/multiple_files.yml +7 -0
- data/spec/{resources → fixtures}/no_config.yml +0 -0
- data/spec/fixtures/no_scope.yml +4 -0
- data/spec/fixtures/simple_scope.yml +5 -0
- data/spec/js/currency.spec.js +62 -0
- data/spec/js/current_locale.spec.js +19 -0
- data/spec/js/dates.spec.js +265 -0
- data/spec/js/defaults.spec.js +31 -0
- data/spec/js/extend.spec.js +110 -0
- data/spec/js/interpolation.spec.js +124 -0
- data/spec/js/jasmine/MIT.LICENSE +20 -0
- data/spec/js/jasmine/jasmine-html.js +190 -0
- data/spec/js/jasmine/jasmine.css +166 -0
- data/spec/js/jasmine/jasmine.js +2476 -0
- data/spec/js/jasmine/jasmine_favicon.png +0 -0
- data/spec/js/locales.spec.js +31 -0
- data/spec/js/localization.spec.js +48 -0
- data/spec/js/numbers.spec.js +170 -0
- data/spec/js/placeholder.spec.js +24 -0
- data/spec/js/pluralization.spec.js +211 -0
- data/spec/js/prepare_options.spec.js +41 -0
- data/spec/js/require.js +2083 -0
- data/spec/js/specs.html +49 -0
- data/spec/js/specs_requirejs.html +72 -0
- data/spec/js/translate.spec.js +277 -0
- data/spec/js/translations.js +164 -0
- data/spec/js/utility_functions.spec.js +20 -0
- data/spec/ruby/i18n/js/fallback_locales_spec.rb +84 -0
- data/spec/ruby/i18n/js/segment_spec.rb +157 -0
- data/spec/ruby/i18n/js/utils_spec.rb +106 -0
- data/spec/ruby/i18n/js_spec.rb +627 -0
- data/spec/spec_helper.rb +55 -14
- data/yarn.lock +131 -0
- metadata +188 -96
- data/.rspec +0 -1
- data/Gemfile.lock +0 -51
- data/README.rdoc +0 -305
- data/lib/i18n-js/engine.rb +0 -62
- data/lib/i18n-js/railtie.rb +0 -13
- data/lib/i18n-js/rake.rb +0 -16
- data/lib/i18n-js/version.rb +0 -10
- data/spec/i18n_spec.js +0 -768
- data/spec/i18n_spec.rb +0 -205
- data/spec/resources/custom_path.yml +0 -4
- data/spec/resources/default.yml +0 -4
- data/spec/resources/js_file_per_locale.yml +0 -3
- data/spec/resources/multiple_conditions.yml +0 -6
- data/spec/resources/multiple_files.yml +0 -6
- data/spec/resources/no_scope.yml +0 -3
- data/spec/resources/simple_scope.yml +0 -4
- data/vendor/assets/javascripts/i18n/translations.js.erb +0 -7
- data/vendor/assets/javascripts/i18n.js +0 -450
Binary file
|
@@ -0,0 +1,31 @@
|
|
1
|
+
var I18n = require("../../app/assets/javascripts/i18n");
|
2
|
+
|
3
|
+
describe("Locales", function(){
|
4
|
+
beforeEach(function(){
|
5
|
+
I18n.reset();
|
6
|
+
});
|
7
|
+
|
8
|
+
it("returns the requested locale, if available", function(){
|
9
|
+
I18n.locales["ab"] = ["ab"];
|
10
|
+
expect(I18n.locales.get("ab")).toEqual(["ab"]);
|
11
|
+
});
|
12
|
+
|
13
|
+
it("wraps single results in an array", function(){
|
14
|
+
I18n.locales["cd"] = "cd";
|
15
|
+
expect(I18n.locales.get("cd")).toEqual(["cd"]);
|
16
|
+
});
|
17
|
+
|
18
|
+
it("returns the result of locale functions", function(){
|
19
|
+
I18n.locales["fn"] = function() {
|
20
|
+
return "gg";
|
21
|
+
};
|
22
|
+
expect(I18n.locales.get("fn")).toEqual(["gg"]);
|
23
|
+
});
|
24
|
+
|
25
|
+
it("uses I18n.locale as a fallback", function(){
|
26
|
+
I18n.locale = "xx";
|
27
|
+
I18n.locales["xx"] = ["xx"];
|
28
|
+
expect(I18n.locales.get()).toEqual(["xx"]);
|
29
|
+
expect(I18n.locales.get("yy")).toEqual(["xx"]);
|
30
|
+
});
|
31
|
+
});
|
@@ -0,0 +1,48 @@
|
|
1
|
+
var I18n = require("../../app/assets/javascripts/i18n")
|
2
|
+
, Translations = require("./translations")
|
3
|
+
;
|
4
|
+
|
5
|
+
describe("Localization", function(){
|
6
|
+
var actual, expected;
|
7
|
+
|
8
|
+
beforeEach(function() {
|
9
|
+
I18n.reset();
|
10
|
+
I18n.translations = Translations();
|
11
|
+
});
|
12
|
+
|
13
|
+
it("localizes number", function(){
|
14
|
+
expect(I18n.l("number", 1234567)).toEqual("1,234,567.000");
|
15
|
+
});
|
16
|
+
|
17
|
+
it("localizes currency", function(){
|
18
|
+
expect(I18n.l("currency", 1234567)).toEqual("$1,234,567.00");
|
19
|
+
});
|
20
|
+
|
21
|
+
it("localizes date strings", function(){
|
22
|
+
I18n.locale = "pt-BR";
|
23
|
+
|
24
|
+
expect(I18n.l("date.formats.default", "2009-11-29")).toEqual("29/11/2009");
|
25
|
+
expect(I18n.l("date.formats.short", "2009-01-07")).toEqual("07 de Janeiro");
|
26
|
+
expect(I18n.l("date.formats.long", "2009-01-07")).toEqual("07 de Janeiro de 2009");
|
27
|
+
});
|
28
|
+
|
29
|
+
it("localizes time strings", function(){
|
30
|
+
I18n.locale = "pt-BR";
|
31
|
+
|
32
|
+
expect(I18n.l("time.formats.default", "2009-11-29 15:07:59")).toEqual("Domingo, 29 de Novembro de 2009, 15:07 h");
|
33
|
+
expect(I18n.l("time.formats.short", "2009-01-07 09:12:35")).toEqual("07/01, 09:12 h");
|
34
|
+
expect(I18n.l("time.formats.long", "2009-11-29 15:07:59")).toEqual("Domingo, 29 de Novembro de 2009, 15:07 h");
|
35
|
+
});
|
36
|
+
|
37
|
+
it("localizes date/time strings with placeholders", function(){
|
38
|
+
I18n.locale = "pt-BR";
|
39
|
+
|
40
|
+
expect(I18n.l("date.formats.short_with_placeholders", "2009-01-07", { p1: "!", p2: "?" })).toEqual("07 de Janeiro ! ?");
|
41
|
+
expect(I18n.l("time.formats.short_with_placeholders", "2009-01-07 09:12:35", { p1: "!" })).toEqual("07/01, 09:12 h !");
|
42
|
+
});
|
43
|
+
|
44
|
+
it("localizes percentage", function(){
|
45
|
+
I18n.locale = "pt-BR";
|
46
|
+
expect(I18n.l("percentage", 123.45)).toEqual("123,45%");
|
47
|
+
});
|
48
|
+
});
|
@@ -0,0 +1,170 @@
|
|
1
|
+
var I18n = require("../../app/assets/javascripts/i18n")
|
2
|
+
, Translations = require("./translations")
|
3
|
+
;
|
4
|
+
|
5
|
+
describe("Numbers", function(){
|
6
|
+
var actual, expected;
|
7
|
+
|
8
|
+
beforeEach(function() {
|
9
|
+
I18n.reset();
|
10
|
+
I18n.translations = Translations();
|
11
|
+
});
|
12
|
+
|
13
|
+
it("formats number with default settings", function(){
|
14
|
+
expect(I18n.toNumber(1)).toEqual("1.000");
|
15
|
+
expect(I18n.toNumber(12)).toEqual("12.000");
|
16
|
+
expect(I18n.toNumber(123)).toEqual("123.000");
|
17
|
+
expect(I18n.toNumber(1234)).toEqual("1,234.000");
|
18
|
+
expect(I18n.toNumber(12345)).toEqual("12,345.000");
|
19
|
+
expect(I18n.toNumber(123456)).toEqual("123,456.000");
|
20
|
+
expect(I18n.toNumber(1234567)).toEqual("1,234,567.000");
|
21
|
+
expect(I18n.toNumber(12345678)).toEqual("12,345,678.000");
|
22
|
+
expect(I18n.toNumber(123456789)).toEqual("123,456,789.000");
|
23
|
+
});
|
24
|
+
|
25
|
+
it("formats negative numbers with default settings", function(){
|
26
|
+
expect(I18n.toNumber(-1)).toEqual("-1.000");
|
27
|
+
expect(I18n.toNumber(-12)).toEqual("-12.000");
|
28
|
+
expect(I18n.toNumber(-123)).toEqual("-123.000");
|
29
|
+
expect(I18n.toNumber(-1234)).toEqual("-1,234.000");
|
30
|
+
expect(I18n.toNumber(-12345)).toEqual("-12,345.000");
|
31
|
+
expect(I18n.toNumber(-123456)).toEqual("-123,456.000");
|
32
|
+
expect(I18n.toNumber(-1234567)).toEqual("-1,234,567.000");
|
33
|
+
expect(I18n.toNumber(-12345678)).toEqual("-12,345,678.000");
|
34
|
+
expect(I18n.toNumber(-123456789)).toEqual("-123,456,789.000");
|
35
|
+
});
|
36
|
+
|
37
|
+
it("formats number with partial translation and default options", function(){
|
38
|
+
I18n.translations.en.number = {
|
39
|
+
format: {
|
40
|
+
precision: 2
|
41
|
+
}
|
42
|
+
};
|
43
|
+
|
44
|
+
expect(I18n.toNumber(1234)).toEqual("1,234.00");
|
45
|
+
});
|
46
|
+
|
47
|
+
it("formats number with full translation and default options", function(){
|
48
|
+
I18n.translations.en.number = {
|
49
|
+
format: {
|
50
|
+
delimiter: ".",
|
51
|
+
separator: ",",
|
52
|
+
precision: 2
|
53
|
+
}
|
54
|
+
};
|
55
|
+
|
56
|
+
expect(I18n.toNumber(1234)).toEqual("1.234,00");
|
57
|
+
});
|
58
|
+
|
59
|
+
it("formats numbers with some custom options that should be merged with default options", function(){
|
60
|
+
expect(I18n.toNumber(1234.56, {precision: 0})).toEqual("1,235");
|
61
|
+
expect(I18n.toNumber(1234, {separator: '-'})).toEqual("1,234-000");
|
62
|
+
expect(I18n.toNumber(1234, {delimiter: '_'})).toEqual("1_234.000");
|
63
|
+
});
|
64
|
+
|
65
|
+
it("formats number considering options", function(){
|
66
|
+
options = {
|
67
|
+
precision: 2,
|
68
|
+
separator: ",",
|
69
|
+
delimiter: "."
|
70
|
+
};
|
71
|
+
|
72
|
+
expect(I18n.toNumber(1, options)).toEqual("1,00");
|
73
|
+
expect(I18n.toNumber(12, options)).toEqual("12,00");
|
74
|
+
expect(I18n.toNumber(123, options)).toEqual("123,00");
|
75
|
+
expect(I18n.toNumber(1234, options)).toEqual("1.234,00");
|
76
|
+
expect(I18n.toNumber(123456, options)).toEqual("123.456,00");
|
77
|
+
expect(I18n.toNumber(1234567, options)).toEqual("1.234.567,00");
|
78
|
+
expect(I18n.toNumber(12345678, options)).toEqual("12.345.678,00");
|
79
|
+
});
|
80
|
+
|
81
|
+
it("formats numbers with different precisions", function(){
|
82
|
+
options = {separator: ".", delimiter: ","};
|
83
|
+
|
84
|
+
options["precision"] = 2;
|
85
|
+
expect(I18n.toNumber(1.98, options)).toEqual("1.98");
|
86
|
+
|
87
|
+
options["precision"] = 3;
|
88
|
+
expect(I18n.toNumber(1.98, options)).toEqual("1.980");
|
89
|
+
|
90
|
+
options["precision"] = 2;
|
91
|
+
expect(I18n.toNumber(1.987, options)).toEqual("1.99");
|
92
|
+
|
93
|
+
options["precision"] = 1;
|
94
|
+
expect(I18n.toNumber(1.98, options)).toEqual("2.0");
|
95
|
+
|
96
|
+
options["precision"] = 0;
|
97
|
+
expect(I18n.toNumber(1.98, options)).toEqual("2");
|
98
|
+
});
|
99
|
+
|
100
|
+
it("rounds numbers correctly when precision is given", function(){
|
101
|
+
options = {separator: ".", delimiter: ","};
|
102
|
+
|
103
|
+
options["precision"] = 2;
|
104
|
+
expect(I18n.toNumber(0.104, options)).toEqual("0.10");
|
105
|
+
|
106
|
+
options["precision"] = 2;
|
107
|
+
expect(I18n.toNumber(0.105, options)).toEqual("0.11");
|
108
|
+
|
109
|
+
options["precision"] = 2;
|
110
|
+
expect(I18n.toNumber(1.005, options)).toEqual("1.01");
|
111
|
+
|
112
|
+
options["precision"] = 3;
|
113
|
+
expect(I18n.toNumber(35.855, options)).toEqual("35.855");
|
114
|
+
|
115
|
+
options["precision"] = 2;
|
116
|
+
expect(I18n.toNumber(35.855, options)).toEqual("35.86");
|
117
|
+
|
118
|
+
options["precision"] = 1;
|
119
|
+
expect(I18n.toNumber(35.855, options)).toEqual("35.9");
|
120
|
+
|
121
|
+
options["precision"] = 0;
|
122
|
+
expect(I18n.toNumber(35.855, options)).toEqual("36");
|
123
|
+
|
124
|
+
options["precision"] = 0;
|
125
|
+
expect(I18n.toNumber(0.000000000000001, options)).toEqual("0");
|
126
|
+
});
|
127
|
+
|
128
|
+
it("returns number as human size", function(){
|
129
|
+
var kb = 1024;
|
130
|
+
|
131
|
+
expect(I18n.toHumanSize(1)).toEqual("1Byte");
|
132
|
+
expect(I18n.toHumanSize(100)).toEqual("100Bytes");
|
133
|
+
|
134
|
+
expect(I18n.toHumanSize(kb)).toEqual("1KB");
|
135
|
+
expect(I18n.toHumanSize(kb * 1.5)).toEqual("1.5KB");
|
136
|
+
|
137
|
+
expect(I18n.toHumanSize(kb * kb)).toEqual("1MB");
|
138
|
+
expect(I18n.toHumanSize(kb * kb * 1.5)).toEqual("1.5MB");
|
139
|
+
|
140
|
+
expect(I18n.toHumanSize(kb * kb * kb)).toEqual("1GB");
|
141
|
+
expect(I18n.toHumanSize(kb * kb * kb * 1.5)).toEqual("1.5GB");
|
142
|
+
|
143
|
+
expect(I18n.toHumanSize(kb * kb * kb * kb)).toEqual("1TB");
|
144
|
+
expect(I18n.toHumanSize(kb * kb * kb * kb * 1.5)).toEqual("1.5TB");
|
145
|
+
|
146
|
+
expect(I18n.toHumanSize(kb * kb * kb * kb * kb)).toEqual("1024TB");
|
147
|
+
});
|
148
|
+
|
149
|
+
it("returns number as human size using custom options", function(){
|
150
|
+
expect(I18n.toHumanSize(1024 * 1.6, {precision: 0})).toEqual("2KB");
|
151
|
+
});
|
152
|
+
|
153
|
+
it("formats numbers with strip insignificant zero", function() {
|
154
|
+
options = {separator: ".", delimiter: ",", strip_insignificant_zeros: true};
|
155
|
+
|
156
|
+
options["precision"] = 2;
|
157
|
+
expect(I18n.toNumber(1.0, options)).toEqual("1");
|
158
|
+
|
159
|
+
options["precision"] = 3;
|
160
|
+
expect(I18n.toNumber(1.98, options)).toEqual("1.98");
|
161
|
+
|
162
|
+
options["precision"] = 4;
|
163
|
+
expect(I18n.toNumber(1.987, options)).toEqual("1.987");
|
164
|
+
});
|
165
|
+
|
166
|
+
it("keeps significant zeros [issue#103]", function() {
|
167
|
+
actual = I18n.toNumber(30, {strip_insignificant_zeros: true, precision: 0});
|
168
|
+
expect(actual).toEqual("30");
|
169
|
+
});
|
170
|
+
});
|
@@ -0,0 +1,24 @@
|
|
1
|
+
var I18n = require("../../app/assets/javascripts/i18n");
|
2
|
+
|
3
|
+
describe("Placeholder", function(){
|
4
|
+
beforeEach(function(){
|
5
|
+
I18n.reset();
|
6
|
+
});
|
7
|
+
|
8
|
+
it("matches {{name}}", function(){
|
9
|
+
expect("{{name}}").toMatch(I18n.placeholder);
|
10
|
+
});
|
11
|
+
|
12
|
+
it("matches %{name}", function(){
|
13
|
+
expect("%{name}").toMatch(I18n.placeholder);
|
14
|
+
});
|
15
|
+
|
16
|
+
it("returns placeholders", function(){
|
17
|
+
var translation = "I like %{javascript}. I also like %{ruby}"
|
18
|
+
, matches = translation.match(I18n.placeholder);
|
19
|
+
;
|
20
|
+
|
21
|
+
expect(matches[0]).toEqual("%{javascript}");
|
22
|
+
expect(matches[1]).toEqual("%{ruby}");
|
23
|
+
});
|
24
|
+
});
|
@@ -0,0 +1,211 @@
|
|
1
|
+
var I18n = require("../../app/assets/javascripts/i18n")
|
2
|
+
, Translations = require("./translations")
|
3
|
+
;
|
4
|
+
|
5
|
+
describe("Pluralization", function(){
|
6
|
+
var actual, expected;
|
7
|
+
|
8
|
+
beforeEach(function(){
|
9
|
+
I18n.reset();
|
10
|
+
I18n.translations = Translations();
|
11
|
+
});
|
12
|
+
|
13
|
+
it("sets alias", function() {
|
14
|
+
expect(I18n.p).toEqual(I18n.pluralize);
|
15
|
+
});
|
16
|
+
|
17
|
+
it("pluralizes scope", function(){
|
18
|
+
expect(I18n.p(0, "inbox")).toEqual("You have no messages");
|
19
|
+
expect(I18n.p(1, "inbox")).toEqual("You have 1 message");
|
20
|
+
expect(I18n.p(5, "inbox")).toEqual("You have 5 messages");
|
21
|
+
});
|
22
|
+
|
23
|
+
it("pluralizes using the 'other' scope", function(){
|
24
|
+
I18n.translations["en"]["inbox"]["zero"] = null;
|
25
|
+
expect(I18n.p(0, "inbox")).toEqual("You have 0 messages");
|
26
|
+
});
|
27
|
+
|
28
|
+
it("pluralizes using the 'zero' scope", function(){
|
29
|
+
I18n.translations["en"]["inbox"]["zero"] = "No messages (zero)";
|
30
|
+
|
31
|
+
expect(I18n.p(0, "inbox")).toEqual("No messages (zero)");
|
32
|
+
});
|
33
|
+
|
34
|
+
it("pluralizes using negative values", function(){
|
35
|
+
expect(I18n.p(-1, "inbox")).toEqual("You have -1 messages");
|
36
|
+
expect(I18n.p(-5, "inbox")).toEqual("You have -5 messages");
|
37
|
+
});
|
38
|
+
|
39
|
+
it("returns missing translation", function(){
|
40
|
+
expect(I18n.p(-1, "missing")).toEqual('[missing "en.missing" translation]');
|
41
|
+
});
|
42
|
+
|
43
|
+
it("pluralizes using multiple placeholders", function(){
|
44
|
+
actual = I18n.p(1, "unread", {unread: 5});
|
45
|
+
expect(actual).toEqual("You have 1 new message (5 unread)");
|
46
|
+
|
47
|
+
actual = I18n.p(10, "unread", {unread: 2});
|
48
|
+
expect(actual).toEqual("You have 10 new messages (2 unread)");
|
49
|
+
|
50
|
+
actual = I18n.p(0, "unread", {unread: 5});
|
51
|
+
expect(actual).toEqual("You have no new messages (5 unread)");
|
52
|
+
});
|
53
|
+
|
54
|
+
it("allows empty strings", function(){
|
55
|
+
I18n.translations["en"]["inbox"]["zero"] = "";
|
56
|
+
|
57
|
+
expect(I18n.p(0, "inbox")).toEqual("");
|
58
|
+
});
|
59
|
+
|
60
|
+
it("returns missing message on null values", function(){
|
61
|
+
I18n.translations["en"]["sent"]["zero"] = null;
|
62
|
+
I18n.translations["en"]["sent"]["one"] = null;
|
63
|
+
I18n.translations["en"]["sent"]["other"] = null;
|
64
|
+
|
65
|
+
expect(I18n.p(0, "sent")).toEqual('[missing "en.sent.zero" translation]');
|
66
|
+
expect(I18n.p(1, "sent")).toEqual('[missing "en.sent.one" translation]');
|
67
|
+
expect(I18n.p(5, "sent")).toEqual('[missing "en.sent.other" translation]');
|
68
|
+
});
|
69
|
+
|
70
|
+
it("pluralizes using custom rules", function() {
|
71
|
+
I18n.locale = "custom";
|
72
|
+
|
73
|
+
I18n.pluralization["custom"] = function(count) {
|
74
|
+
if (count === 0) { return ["zero"]; }
|
75
|
+
if (count >= 1 && count <= 5) { return ["few", "other"]; }
|
76
|
+
return ["other"];
|
77
|
+
};
|
78
|
+
|
79
|
+
I18n.translations["custom"] = {
|
80
|
+
"things": {
|
81
|
+
"zero": "No things"
|
82
|
+
, "few": "A few things"
|
83
|
+
, "other": "%{count} things"
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
expect(I18n.p(0, "things")).toEqual("No things");
|
88
|
+
expect(I18n.p(4, "things")).toEqual("A few things");
|
89
|
+
expect(I18n.p(-4, "things")).toEqual("-4 things");
|
90
|
+
expect(I18n.p(10, "things")).toEqual("10 things");
|
91
|
+
});
|
92
|
+
|
93
|
+
it("pluralizes default value", function(){
|
94
|
+
options = {defaultValue: {
|
95
|
+
zero: "No things here!"
|
96
|
+
, one: "There is {{count}} thing here!"
|
97
|
+
, other: "There are {{count}} things here!"
|
98
|
+
}};
|
99
|
+
|
100
|
+
expect(I18n.p(0, "things", options)).toEqual("No things here!");
|
101
|
+
expect(I18n.p(1, "things", options)).toEqual("There is 1 thing here!");
|
102
|
+
expect(I18n.p(5, "things", options)).toEqual("There are 5 things here!");
|
103
|
+
});
|
104
|
+
|
105
|
+
it("ignores pluralization when scope exists", function(){
|
106
|
+
options = {defaultValue: {
|
107
|
+
zero: "No things here!"
|
108
|
+
, one: "There is {{count}} thing here!"
|
109
|
+
, other: "There are {{count}} things here!"
|
110
|
+
}};
|
111
|
+
|
112
|
+
expect(I18n.p(0, "inbox", options)).toEqual("You have no messages");
|
113
|
+
expect(I18n.p(1, "inbox", options)).toEqual("You have 1 message");
|
114
|
+
expect(I18n.p(5, "inbox", options)).toEqual("You have 5 messages");
|
115
|
+
});
|
116
|
+
|
117
|
+
it("fallback to default locale when I18n.fallbacks is enabled", function() {
|
118
|
+
I18n.locale = "pt-BR";
|
119
|
+
I18n.fallbacks = true;
|
120
|
+
I18n.translations["pt-BR"].inbox= {
|
121
|
+
one: null
|
122
|
+
, other: null
|
123
|
+
, zero: null
|
124
|
+
};
|
125
|
+
expect(I18n.p(0, "inbox", { count: 0 })).toEqual("You have no messages");
|
126
|
+
expect(I18n.p(1, "inbox", { count: 1 })).toEqual("You have 1 message");
|
127
|
+
expect(I18n.p(5, "inbox", { count: 5 })).toEqual("You have 5 messages");
|
128
|
+
});
|
129
|
+
|
130
|
+
it("fallback to default locale when I18n.fallbacks is enabled", function() {
|
131
|
+
I18n.locale = "pt-BR";
|
132
|
+
I18n.fallbacks = true;
|
133
|
+
I18n.translations["pt-BR"].inbox= {
|
134
|
+
one: "Você tem uma mensagem"
|
135
|
+
, other: null
|
136
|
+
, zero: "Você não tem nenhuma mensagem"
|
137
|
+
};
|
138
|
+
expect(I18n.p(0, "inbox", { count: 0 })).toEqual("Você não tem nenhuma mensagem");
|
139
|
+
expect(I18n.p(1, "inbox", { count: 1 })).toEqual("Você tem uma mensagem");
|
140
|
+
expect(I18n.p(5, "inbox", { count: 5 })).toEqual('You have 5 messages');
|
141
|
+
});
|
142
|
+
|
143
|
+
it("fallback to 'other' scope", function() {
|
144
|
+
I18n.locale = "pt-BR";
|
145
|
+
I18n.fallbacks = true;
|
146
|
+
I18n.translations["pt-BR"].inbox= {
|
147
|
+
one: "Você tem uma mensagem"
|
148
|
+
, other: "Você tem {{count}} mensagens"
|
149
|
+
, zero: null
|
150
|
+
}
|
151
|
+
expect(I18n.p(0, "inbox", { count: 0 })).toEqual("Você tem 0 mensagens");
|
152
|
+
expect(I18n.p(1, "inbox", { count: 1 })).toEqual("Você tem uma mensagem");
|
153
|
+
expect(I18n.p(5, "inbox", { count: 5 })).toEqual("Você tem 5 mensagens");
|
154
|
+
});
|
155
|
+
|
156
|
+
it("fallback to defaulValue when defaultValue is string", function() {
|
157
|
+
I18n.locale = "pt-BR";
|
158
|
+
I18n.fallbacks = true;
|
159
|
+
I18n.translations["en"]["inbox"]["zero"] = null;
|
160
|
+
I18n.translations["en"]["inbox"]["one"] = null;
|
161
|
+
I18n.translations["en"]["inbox"]["other"] = null;
|
162
|
+
I18n.translations["pt-BR"].inbox= {
|
163
|
+
one: "Você tem uma mensagem"
|
164
|
+
, other: null
|
165
|
+
, zero: null
|
166
|
+
}
|
167
|
+
options = {
|
168
|
+
defaultValue: "default message"
|
169
|
+
};
|
170
|
+
expect(I18n.p(0, "inbox", options)).toEqual("default message");
|
171
|
+
expect(I18n.p(1, "inbox", options)).toEqual("Você tem uma mensagem");
|
172
|
+
expect(I18n.p(5, "inbox", options)).toEqual("default message");
|
173
|
+
});
|
174
|
+
|
175
|
+
it("fallback to defaulValue when defaultValue is an object", function() {
|
176
|
+
I18n.locale = "pt-BR";
|
177
|
+
I18n.fallbacks = true;
|
178
|
+
I18n.translations["en"]["inbox"]["zero"] = null;
|
179
|
+
I18n.translations["en"]["inbox"]["one"] = null;
|
180
|
+
I18n.translations["en"]["inbox"]["other"] = null;
|
181
|
+
I18n.translations["pt-BR"].inbox= {
|
182
|
+
one: "Você tem uma mensagem"
|
183
|
+
, other: null
|
184
|
+
, zero: null
|
185
|
+
}
|
186
|
+
options = {
|
187
|
+
defaultValue: {
|
188
|
+
zero: "default message for no message"
|
189
|
+
, one: "default message for 1 message"
|
190
|
+
, other: "default message for {{count}} messages"
|
191
|
+
}
|
192
|
+
};
|
193
|
+
expect(I18n.p(0, "inbox", options)).toEqual("default message for no message");
|
194
|
+
expect(I18n.p(1, "inbox", options)).toEqual("Você tem uma mensagem");
|
195
|
+
expect(I18n.p(5, "inbox", options)).toEqual("default message for 5 messages");
|
196
|
+
});
|
197
|
+
|
198
|
+
it("fallback to default locale when I18n.fallbacks is enabled and no translations in sub scope", function() {
|
199
|
+
I18n.locale = "pt-BR";
|
200
|
+
I18n.fallbacks = true;
|
201
|
+
I18n.translations["en"]["mailbox"] = {
|
202
|
+
inbox: I18n.translations["en"].inbox
|
203
|
+
}
|
204
|
+
|
205
|
+
expect(I18n.translations["pt-BR"]["mailbox"]).toEqual(undefined);
|
206
|
+
expect(I18n.p(0, "mailbox.inbox", { count: 0 })).toEqual("You have no messages");
|
207
|
+
expect(I18n.p(1, "mailbox.inbox", { count: 1 })).toEqual("You have 1 message");
|
208
|
+
expect(I18n.p(5, "mailbox.inbox", { count: 5 })).toEqual("You have 5 messages");
|
209
|
+
});
|
210
|
+
|
211
|
+
});
|
@@ -0,0 +1,41 @@
|
|
1
|
+
var I18n = require("../../app/assets/javascripts/i18n");
|
2
|
+
|
3
|
+
describe("Prepare options", function(){
|
4
|
+
beforeEach(function(){
|
5
|
+
I18n.reset();
|
6
|
+
});
|
7
|
+
|
8
|
+
it("merges two objects", function(){
|
9
|
+
var options = I18n.prepareOptions(
|
10
|
+
{name: "Mary Doe"},
|
11
|
+
{name: "John Doe", role: "user"}
|
12
|
+
);
|
13
|
+
|
14
|
+
expect(options.name).toEqual("Mary Doe");
|
15
|
+
expect(options.role).toEqual("user");
|
16
|
+
});
|
17
|
+
|
18
|
+
it("merges multiple objects", function(){
|
19
|
+
var options = I18n.prepareOptions(
|
20
|
+
{name: "Mary Doe"},
|
21
|
+
{name: "John Doe", role: "user"},
|
22
|
+
{age: 33},
|
23
|
+
{email: "mary@doe.com", url: "http://marydoe.com"},
|
24
|
+
{role: "admin", email: "john@doe.com"}
|
25
|
+
);
|
26
|
+
|
27
|
+
expect(options.name).toEqual("Mary Doe");
|
28
|
+
expect(options.role).toEqual("user");
|
29
|
+
expect(options.age).toEqual(33);
|
30
|
+
expect(options.email).toEqual("mary@doe.com");
|
31
|
+
expect(options.url).toEqual("http://marydoe.com");
|
32
|
+
});
|
33
|
+
|
34
|
+
it("returns an empty object when values are null", function(){
|
35
|
+
expect(I18n.prepareOptions(null, null)).toEqual({});
|
36
|
+
});
|
37
|
+
|
38
|
+
it("returns an empty object when values are undefined", function(){
|
39
|
+
expect(I18n.prepareOptions(undefined, undefined)).toEqual({});
|
40
|
+
});
|
41
|
+
});
|