i18n-js 3.2.1 → 4.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CODEOWNERS +4 -0
  3. data/.github/FUNDING.yml +3 -0
  4. data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
  5. data/.github/ISSUE_TEMPLATE/config.yml +5 -0
  6. data/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
  7. data/.github/PULL_REQUEST_TEMPLATE.md +38 -0
  8. data/.github/dependabot.yml +15 -0
  9. data/.github/workflows/ruby-tests.yml +73 -0
  10. data/.gitignore +13 -7
  11. data/.rubocop.yml +19 -0
  12. data/CHANGELOG.md +45 -386
  13. data/CODE_OF_CONDUCT.md +74 -0
  14. data/CONTRIBUTING.md +79 -0
  15. data/Gemfile +3 -0
  16. data/LICENSE.md +20 -0
  17. data/MIGRATING_FROM_V3_TO_V4.md +191 -0
  18. data/README.md +439 -791
  19. data/Rakefile +10 -20
  20. data/bin/release +81 -0
  21. data/exe/i18n +5 -0
  22. data/i18n-js.gemspec +51 -29
  23. data/lib/guard/i18n-js/templates/Guardfile +10 -0
  24. data/lib/guard/i18n-js/version.rb +13 -0
  25. data/lib/guard/i18n-js.rb +95 -0
  26. data/lib/i18n-js/clean_hash.rb +13 -0
  27. data/lib/i18n-js/cli/check_command.rb +17 -0
  28. data/lib/i18n-js/cli/command.rb +79 -0
  29. data/lib/i18n-js/cli/export_command.rb +95 -0
  30. data/lib/i18n-js/cli/init_command.rb +52 -0
  31. data/lib/i18n-js/cli/lint_scripts_command.rb +157 -0
  32. data/lib/i18n-js/cli/lint_translations_command.rb +155 -0
  33. data/lib/i18n-js/cli/plugins_command.rb +67 -0
  34. data/lib/i18n-js/cli/ui.rb +64 -0
  35. data/lib/i18n-js/cli/version_command.rb +18 -0
  36. data/lib/i18n-js/cli.rb +66 -0
  37. data/lib/i18n-js/embed_fallback_translations_plugin.rb +70 -0
  38. data/lib/i18n-js/export_files_plugin.rb +103 -0
  39. data/lib/i18n-js/lint.js +150645 -0
  40. data/lib/i18n-js/lint.ts +196 -0
  41. data/lib/i18n-js/listen.rb +96 -0
  42. data/lib/i18n-js/plugin.rb +103 -0
  43. data/lib/i18n-js/schema.rb +216 -0
  44. data/lib/i18n-js/sort_hash.rb +12 -0
  45. data/lib/i18n-js/version.rb +5 -0
  46. data/lib/i18n-js.rb +107 -1
  47. data/package.json +5 -20
  48. metadata +153 -181
  49. data/.editorconfig +0 -24
  50. data/.npmignore +0 -27
  51. data/.travis.yml +0 -36
  52. data/Appraisals +0 -32
  53. data/app/assets/javascripts/i18n/filtered.js.erb +0 -23
  54. data/app/assets/javascripts/i18n/shims.js +0 -208
  55. data/app/assets/javascripts/i18n/translations.js +0 -3
  56. data/app/assets/javascripts/i18n.js +0 -1077
  57. data/gemfiles/i18n_0_6.gemfile +0 -7
  58. data/gemfiles/i18n_0_7.gemfile +0 -7
  59. data/gemfiles/i18n_0_8.gemfile +0 -7
  60. data/gemfiles/i18n_0_9.gemfile +0 -7
  61. data/gemfiles/i18n_1_0.gemfile +0 -7
  62. data/gemfiles/i18n_1_1.gemfile +0 -7
  63. data/gemfiles/i18n_1_2.gemfile +0 -7
  64. data/gemfiles/i18n_1_3.gemfile +0 -7
  65. data/gemfiles/i18n_1_4.gemfile +0 -7
  66. data/gemfiles/i18n_1_5.gemfile +0 -7
  67. data/lib/i18n/js/dependencies.rb +0 -59
  68. data/lib/i18n/js/engine.rb +0 -87
  69. data/lib/i18n/js/fallback_locales.rb +0 -70
  70. data/lib/i18n/js/formatters/base.rb +0 -23
  71. data/lib/i18n/js/formatters/js.rb +0 -31
  72. data/lib/i18n/js/formatters/json.rb +0 -13
  73. data/lib/i18n/js/middleware.rb +0 -82
  74. data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
  75. data/lib/i18n/js/segment.rb +0 -75
  76. data/lib/i18n/js/utils.rb +0 -65
  77. data/lib/i18n/js/version.rb +0 -7
  78. data/lib/i18n/js.rb +0 -259
  79. data/lib/rails/generators/i18n/js/config/config_generator.rb +0 -19
  80. data/lib/rails/generators/i18n/js/config/templates/i18n-js.yml +0 -27
  81. data/lib/tasks/export.rake +0 -8
  82. data/spec/fixtures/custom_path.yml +0 -5
  83. data/spec/fixtures/default.yml +0 -5
  84. data/spec/fixtures/erb.yml +0 -5
  85. data/spec/fixtures/except_condition.yml +0 -7
  86. data/spec/fixtures/js_export_dir_custom.yml +0 -7
  87. data/spec/fixtures/js_export_dir_none.yml +0 -6
  88. data/spec/fixtures/js_extend_parent.yml +0 -6
  89. data/spec/fixtures/js_extend_segment.yml +0 -6
  90. data/spec/fixtures/js_file_per_locale.yml +0 -7
  91. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +0 -4
  92. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +0 -6
  93. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +0 -4
  94. data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +0 -4
  95. data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +0 -4
  96. data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +0 -4
  97. data/spec/fixtures/js_file_with_namespace_and_pretty_print.yml +0 -7
  98. data/spec/fixtures/js_sort_translation_keys_false.yml +0 -6
  99. data/spec/fixtures/js_sort_translation_keys_true.yml +0 -6
  100. data/spec/fixtures/json_only.yml +0 -18
  101. data/spec/fixtures/locales.yml +0 -95
  102. data/spec/fixtures/merge_plurals.yml +0 -6
  103. data/spec/fixtures/multiple_conditions.yml +0 -7
  104. data/spec/fixtures/multiple_conditions_per_locale.yml +0 -7
  105. data/spec/fixtures/multiple_files.yml +0 -7
  106. data/spec/fixtures/no_config.yml +0 -2
  107. data/spec/fixtures/no_scope.yml +0 -4
  108. data/spec/fixtures/simple_scope.yml +0 -5
  109. data/spec/js/currency.spec.js +0 -62
  110. data/spec/js/current_locale.spec.js +0 -19
  111. data/spec/js/dates.spec.js +0 -265
  112. data/spec/js/defaults.spec.js +0 -31
  113. data/spec/js/extend.spec.js +0 -110
  114. data/spec/js/interpolation.spec.js +0 -124
  115. data/spec/js/jasmine/MIT.LICENSE +0 -20
  116. data/spec/js/jasmine/jasmine-html.js +0 -190
  117. data/spec/js/jasmine/jasmine.css +0 -166
  118. data/spec/js/jasmine/jasmine.js +0 -2476
  119. data/spec/js/jasmine/jasmine_favicon.png +0 -0
  120. data/spec/js/locales.spec.js +0 -31
  121. data/spec/js/localization.spec.js +0 -48
  122. data/spec/js/numbers.spec.js +0 -170
  123. data/spec/js/placeholder.spec.js +0 -24
  124. data/spec/js/pluralization.spec.js +0 -211
  125. data/spec/js/prepare_options.spec.js +0 -41
  126. data/spec/js/require.js +0 -2083
  127. data/spec/js/specs.html +0 -49
  128. data/spec/js/specs_requirejs.html +0 -72
  129. data/spec/js/translate.spec.js +0 -284
  130. data/spec/js/translations.js +0 -163
  131. data/spec/js/utility_functions.spec.js +0 -20
  132. data/spec/ruby/i18n/js/fallback_locales_spec.rb +0 -84
  133. data/spec/ruby/i18n/js/segment_spec.rb +0 -219
  134. data/spec/ruby/i18n/js/utils_spec.rb +0 -106
  135. data/spec/ruby/i18n/js_spec.rb +0 -663
  136. data/spec/spec_helper.rb +0 -79
  137. data/yarn.lock +0 -131
@@ -1,663 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe I18n::JS do
4
-
5
- describe '.config_file_path' do
6
- let(:default_path) { I18n::JS::DEFAULT_CONFIG_PATH }
7
- let(:new_path) { File.join("tmp", default_path) }
8
-
9
- subject { described_class.config_file_path }
10
-
11
- context "when it is not set" do
12
- it { should eq default_path }
13
- end
14
- context "when it is set already" do
15
- before { described_class.config_file_path = new_path }
16
-
17
- it { should eq new_path }
18
- end
19
- end
20
-
21
- context "exporting" do
22
- before do
23
- stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path)
24
- end
25
-
26
- it "exports messages to default path when configuration file doesn't exist" do
27
- I18n::JS.export
28
- file_should_exist "translations.js"
29
- end
30
-
31
- it "exports messages using custom output path" do
32
- set_config "custom_path.yml"
33
- allow(I18n::JS::Segment).to receive(:new).with("tmp/i18n-js/all.js", translations, {js_extend: true, sort_translation_keys: true, json_only: false}).and_call_original
34
- allow_any_instance_of(I18n::JS::Segment).to receive(:save!).with(no_args)
35
- I18n::JS.export
36
- end
37
-
38
- it "sets default scope to * when not specified" do
39
- set_config "no_scope.yml"
40
- allow(I18n::JS::Segment).to receive(:new).with("tmp/i18n-js/no_scope.js", translations, {js_extend: true, sort_translation_keys: true, json_only: false}).and_call_original
41
- allow_any_instance_of(I18n::JS::Segment).to receive(:save!).with(no_args)
42
- I18n::JS.export
43
- end
44
-
45
- it "exports to multiple files" do
46
- set_config "multiple_files.yml"
47
- I18n::JS.export
48
-
49
- file_should_exist "all.js"
50
- file_should_exist "tudo.js"
51
- end
52
-
53
- it "ignores an empty config file" do
54
- set_config "no_config.yml"
55
- I18n::JS.export
56
-
57
- file_should_exist "translations.js"
58
- end
59
-
60
- it "exports to a JS file per available locale" do
61
- set_config "js_file_per_locale.yml"
62
- I18n::JS.export
63
-
64
- file_should_exist "en.js"
65
- file_should_exist "fr.js"
66
-
67
- en_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "en.js"))
68
- expect(en_output).to eq(<<EOS
69
- I18n.translations || (I18n.translations = {});
70
- I18n.translations["en"] = I18n.extend((I18n.translations["en"] || {}), {"admin":{"edit":{"title":"Edit"},"show":{"note":"more details","title":"Show"}},"date":{"abbr_day_names":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"abbr_month_names":[null,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"day_names":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"},"month_names":[null,"January","February","March","April","May","June","July","August","September","October","November","December"]}});
71
- EOS
72
- )
73
- fr_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "fr.js"))
74
- expect(fr_output).to eq(<<EOS
75
- I18n.translations || (I18n.translations = {});
76
- I18n.translations["fr"] = I18n.extend((I18n.translations["fr"] || {}), {"admin":{"edit":{"title":"Editer"},"show":{"note":"plus de détails","title":"Visualiser"}},"date":{"abbr_day_names":["dim","lun","mar","mer","jeu","ven","sam"],"abbr_month_names":[null,"jan.","fév.","mar.","avr.","mai","juin","juil.","août","sept.","oct.","nov.","déc."],"day_names":["dimanche","lundi","mardi","mercredi","jeudi","vendredi","samedi"],"formats":{"default":"%d/%m/%Y","long":"%e %B %Y","long_ordinal":"%e %B %Y","only_day":"%e","short":"%e %b"},"month_names":[null,"janvier","février","mars","avril","mai","juin","juillet","août","septembre","octobre","novembre","décembre"]}});
77
- EOS
78
- )
79
- end
80
-
81
- it "exports with multiple conditions" do
82
- set_config "multiple_conditions.yml"
83
- I18n::JS.export
84
-
85
- file_should_exist "bitsnpieces.js"
86
- end
87
-
88
- it "exports with multiple conditions to a JS file per available locale" do
89
- allow(::I18n).to receive(:available_locales){ [:en, :fr] }
90
-
91
- set_config "multiple_conditions_per_locale.yml"
92
-
93
- result = I18n::JS.translation_segments
94
- expect(result.map(&:file)).to eql(["tmp/i18n-js/bits.%{locale}.js"])
95
-
96
- result.map(&:save!)
97
-
98
- en_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "bits.en.js"))
99
- expect(en_output).to eq(<<EOS
100
- I18n.translations || (I18n.translations = {});
101
- I18n.translations["en"] = I18n.extend((I18n.translations["en"] || {}), {"date":{"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"}},"number":{"currency":{"format":{"delimiter":",","format":"%u%n","precision":2,"separator":".","unit":"$"}}}});
102
- EOS
103
- )
104
- fr_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "bits.fr.js"))
105
- expect(fr_output).to eq(<<EOS
106
- I18n.translations || (I18n.translations = {});
107
- I18n.translations["fr"] = I18n.extend((I18n.translations["fr"] || {}), {"date":{"formats":{"default":"%d/%m/%Y","long":"%e %B %Y","long_ordinal":"%e %B %Y","only_day":"%e","short":"%e %b"}},"number":{"currency":{"format":{"format":"%n %u","precision":2,"unit":"€"}}}});
108
- EOS
109
- )
110
- end
111
-
112
- it "exports as json only" do
113
- set_config "json_only.yml"
114
- I18n::JS.export
115
- en_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "json_only.en.js"))
116
- fr_output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "json_only.fr.js"))
117
- expect(en_output).to eq (<<EOS
118
- {"en":{"date":{"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"}},"number":{"currency":{"format":{"delimiter":",","format":"%u%n","precision":2,"separator":".","unit":"$"}}}}}
119
- EOS
120
- ).chop
121
- expect(fr_output).to eq (<<EOS
122
- {"fr":{"date":{"formats":{"default":"%d/%m/%Y","long":"%e %B %Y","long_ordinal":"%e %B %Y","only_day":"%e","short":"%e %b"}},"number":{"currency":{"format":{"format":"%n %u","precision":2,"unit":"€"}}}}}
123
- EOS
124
- ).chop
125
- expect(JSON.parse(en_output).keys.first).to eq 'en'
126
- end
127
-
128
- it "exports with :except condition" do
129
- set_config "except_condition.yml"
130
- I18n::JS.export
131
-
132
- file_should_exist "trimmed.js"
133
- end
134
-
135
- it "calls .export_i18n_js" do
136
- allow(described_class).to receive(:export_i18n_js)
137
- I18n::JS.export
138
- expect(described_class).to have_received(:export_i18n_js).once
139
- end
140
- end
141
-
142
- context "filters" do
143
- it "filters translations using scope *.date.formats" do
144
- result = I18n::JS.filter(translations, "*.date.formats")
145
- expect(result[:en][:date].keys).to eql([:formats])
146
- expect(result[:fr][:date].keys).to eql([:formats])
147
- end
148
-
149
- it "filters translations using scope [*.date.formats, *.number.currency.format]" do
150
- result = I18n::JS.scoped_translations(["*.date.formats", "*.number.currency.format"])
151
- expect(result[:en].keys.collect(&:to_s).sort).to eql(%w[ date number ])
152
- expect(result[:fr].keys.collect(&:to_s).sort).to eql(%w[ date number ])
153
- end
154
-
155
- it "filters translations using multi-star scope" do
156
- result = I18n::JS.scoped_translations("*.*.formats")
157
-
158
- expect(result[:en].keys.collect(&:to_s).sort).to eql(%w[ date time ])
159
- expect(result[:fr].keys.collect(&:to_s).sort).to eql(%w[ date time ])
160
-
161
- expect(result[:en][:date].keys).to eql([:formats])
162
- expect(result[:en][:time].keys).to eql([:formats])
163
-
164
- expect(result[:fr][:date].keys).to eql([:formats])
165
- expect(result[:fr][:time].keys).to eql([:formats])
166
- end
167
-
168
- it "filters translations using alternated stars" do
169
- result = I18n::JS.scoped_translations("*.admin.*.title")
170
-
171
- expect(result[:en][:admin].keys.collect(&:to_s).sort).to eql(%w[ edit show ])
172
- expect(result[:fr][:admin].keys.collect(&:to_s).sort).to eql(%w[ edit show ])
173
-
174
- expect(result[:en][:admin][:show][:title]).to eql("Show")
175
- expect(result[:fr][:admin][:show][:title]).to eql("Visualiser")
176
-
177
- expect(result[:en][:admin][:edit][:title]).to eql("Edit")
178
- expect(result[:fr][:admin][:edit][:title]).to eql("Editer")
179
- end
180
-
181
- describe ".filtered_translations" do
182
- subject do
183
- I18n::JS.filtered_translations
184
- end
185
-
186
- let!(:old_sort_translation_keys) { I18n::JS.sort_translation_keys? }
187
- before { I18n::JS.sort_translation_keys = sort_translation_keys_value }
188
- after { I18n::JS.sort_translation_keys = old_sort_translation_keys }
189
- before { expect(I18n::JS.sort_translation_keys?).to eq(sort_translation_keys_value) }
190
-
191
- let(:sorted_hash) do
192
- {sorted: :hash}
193
- end
194
- before do
195
- allow(I18n::JS::Utils).
196
- to receive(:deep_key_sort).
197
- and_return(sorted_hash)
198
- end
199
-
200
- shared_examples_for ".filtered_translations" do
201
- subject do
202
- I18n::JS.filtered_translations
203
- end
204
-
205
- # This example is to prevent the regression from
206
- # PR https://github.com/fnando/i18n-js/pull/318
207
- it {should be_a(Hash)}
208
- # Might need to test the keys... or not
209
- end
210
-
211
- context "when translation keys SHOULD be sorted" do
212
- let(:sort_translation_keys_value) { true }
213
-
214
- it_behaves_like ".filtered_translations"
215
- it {should eq(sorted_hash)}
216
- end
217
- context "when translation keys should NOT be sorted" do
218
- let(:sort_translation_keys_value) { false }
219
-
220
- it_behaves_like ".filtered_translations"
221
- it {should_not eq(sorted_hash)}
222
- end
223
- end
224
- end
225
-
226
- context "exceptions" do
227
- it "does not include scopes listed in the exceptions list" do
228
- result = I18n::JS.scoped_translations("*", ['de.*', '*.admin', '*.*.currency'])
229
-
230
- expect(result[:de]).to be_empty
231
-
232
- expect(result[:en][:admin]).to be_nil
233
- expect(result[:fr][:admin]).to be_nil
234
- expect(result[:ja][:admin]).to be_nil
235
-
236
- expect(result[:en][:number][:currency]).to be_nil
237
- expect(result[:fr][:number][:currency]).to be_nil
238
- end
239
-
240
- it "does not include scopes listed in the exceptions list and respects the 'only' option" do
241
- result = I18n::JS.scoped_translations("fr.*", ['*.admin', '*.*.currency'])
242
-
243
- expect(result[:en]).to be_nil
244
- expect(result[:de]).to be_nil
245
- expect(result[:ja]).to be_nil
246
-
247
- expect(result[:fr][:admin]).to be_nil
248
- expect(result[:fr][:number][:currency]).to be_nil
249
-
250
- expect(result[:fr][:time][:am]).to be_a(String)
251
- end
252
-
253
- it "does exclude absolute scopes listed in the exceptions list" do
254
- result = I18n::JS.scoped_translations("*", ['de', 'en.admin', 'fr.number.currency'])
255
-
256
- expect(result[:de]).to be_nil
257
-
258
- expect(result[:en]).to be_a(Hash)
259
- expect(result[:en][:admin]).to be_nil
260
-
261
- expect(result[:fr][:number]).to be_a(Hash)
262
- expect(result[:fr][:number][:currency]).to be_nil
263
- end
264
-
265
- it "does not exclude non-absolute scopes listed in the exceptions list" do
266
- result = I18n::JS.scoped_translations("*", ['admin', 'currency'])
267
-
268
- expect(result[:en][:admin]).to be_a(Hash)
269
- expect(result[:fr][:admin]).to be_a(Hash)
270
- expect(result[:ja][:admin]).to be_a(Hash)
271
-
272
- expect(result[:en][:number][:currency]).to be_a(Hash)
273
- expect(result[:fr][:number][:currency]).to be_a(Hash)
274
- end
275
- end
276
-
277
- context "fallbacks" do
278
- subject(:translations) do
279
- I18n::JS.translation_segments.first.translations
280
- end
281
-
282
- it "exports without fallback when disabled" do
283
- set_config "js_file_per_locale_without_fallbacks.yml"
284
- expect(subject[:fr][:fallback_test]).to eql(nil)
285
- expect(subject[:fr][:null_test]).to eql(nil)
286
- expect(subject[:de][:null_test]).to eql(nil)
287
- end
288
-
289
- it "exports with default_locale as fallback when enabled" do
290
- set_config "js_file_per_locale_with_fallbacks_enabled.yml"
291
- expect(subject[:fr][:fallback_test]).to eql("Success")
292
- expect(subject[:fr][:null_test]).to eql("fallback for null")
293
- expect(subject[:de][:null_test]).to eql("fallback for null")
294
- end
295
-
296
- it "exports with default_locale as fallback when enabled with :default_locale" do
297
- set_config "js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml"
298
- expect(subject[:fr][:fallback_test]).to eql("Success")
299
- expect(subject[:fr][:null_test]).to eql("fallback for null")
300
- expect(subject[:de][:null_test]).to eql("fallback for null")
301
- end
302
-
303
- it "exports with given locale as fallback" do
304
- set_config "js_file_per_locale_with_fallbacks_as_locale.yml"
305
- expect(subject[:fr][:fallback_test]).to eql("Erfolg")
306
- expect(subject[:fr][:null_test]).to eql(nil)
307
- expect(subject[:de][:null_test]).to eql(nil)
308
- end
309
-
310
- context "when given locale is in `I18n.available_locales` but its translation is missing" do
311
- subject { translations[:fr][:fallback_test] }
312
-
313
- let(:new_locale) { :pirate }
314
- let!(:old_available_locales) { I18n.config.available_locales }
315
- let!(:new_available_locales) { I18n.config.available_locales + [new_locale] }
316
- before do
317
- I18n.config.available_locales = new_available_locales
318
- set_config "js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml"
319
- end
320
- after do
321
- I18n.config.available_locales = old_available_locales
322
- end
323
-
324
- it {should eql(nil)}
325
- end
326
-
327
- context "with I18n::Fallbacks enabled" do
328
- let(:backend_with_fallbacks) { backend_class_with_fallbacks.new }
329
- let!(:old_backebad) { I18n.backend }
330
-
331
- before do
332
- I18n::JS.backend = backend_with_fallbacks
333
- I18n.fallbacks[:fr] = [:de, :en]
334
- end
335
- after { I18n::JS.backend = old_backebad }
336
-
337
- it "exports with defined locale as fallback when enabled" do
338
- set_config "js_file_per_locale_with_fallbacks_enabled.yml"
339
- expect(subject[:fr][:fallback_test]).to eql("Erfolg")
340
- end
341
-
342
- it "exports with defined locale as fallback when enabled with :default_locale" do
343
- set_config "js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml"
344
- expect(subject[:fr][:fallback_test]).to eql("Success")
345
- end
346
-
347
- it "exports with Fallbacks as Hash" do
348
- set_config "js_file_per_locale_with_fallbacks_as_hash.yml"
349
- expect(subject[:fr][:fallback_test]).to eql("Erfolg")
350
- end
351
- end
352
- end
353
-
354
- context "namespace and pretty_print options" do
355
-
356
- before do
357
- stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path)
358
- set_config "js_file_with_namespace_and_pretty_print.yml"
359
- end
360
-
361
- it "exports with defined locale as fallback when enabled" do
362
- I18n::JS.export
363
- file_should_exist "en.js"
364
- output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "en.js"))
365
- expect(output).to match(/^#{
366
- <<EOS
367
- Foo.translations || (Foo.translations = {});
368
- Foo.translations["en"] = {
369
- "number": {
370
- "format": {
371
- EOS
372
- }.+#{
373
- <<EOS
374
- "edit": {
375
- "title": "Edit"
376
- }
377
- },
378
- "foo": "Foo",
379
- "fallback_test": "Success"
380
- };
381
- EOS
382
- }$/)
383
- end
384
- end
385
-
386
- context "I18n.available_locales" do
387
-
388
- context "when I18n.available_locales is not set" do
389
- it "should allow all locales" do
390
- result = I18n::JS.scoped_translations("*.admin.*.title")
391
-
392
- expect(result[:en][:admin][:show][:title]).to eql("Show")
393
- expect(result[:fr][:admin][:show][:title]).to eql("Visualiser")
394
- expect(result[:ja][:admin][:show][:title]).to eql("Ignore me")
395
- end
396
- end
397
-
398
- context "when I18n.available_locales is set" do
399
- before { allow(::I18n).to receive(:available_locales){ [:en, :fr] } }
400
-
401
- it "should ignore non-valid locales" do
402
- result = I18n::JS.scoped_translations("*.admin.*.title")
403
-
404
- expect(result[:en][:admin][:show][:title]).to eql("Show")
405
- expect(result[:fr][:admin][:show][:title]).to eql("Visualiser")
406
- expect(result.keys.include?(:ja)).to eql(false)
407
- end
408
- end
409
- end
410
-
411
- context "general" do
412
- it "sets export directory" do
413
- expect(I18n::JS::DEFAULT_EXPORT_DIR_PATH).to eql("public/javascripts")
414
- end
415
-
416
- it "sets empty hash as configuration when no file is found" do
417
- expect(I18n::JS.config_file_exists?).to eql(false)
418
- expect(I18n::JS.config).to eql({})
419
- end
420
-
421
- it "executes erb in config file" do
422
- set_config "erb.yml"
423
-
424
- config_entry = I18n::JS.config[:translations].first
425
- expect(config_entry["only"]).to eq("*.date.formats")
426
- end
427
- end
428
-
429
- describe "i18n.js exporting" do
430
- after { begin described_class.send(:remove_instance_variable, :@export_i18n_js_dir_path); rescue; end }
431
-
432
- describe ".export_i18n_js with global variable" do
433
- before do
434
- allow(FileUtils).to receive(:mkdir_p).and_call_original
435
- allow(FileUtils).to receive(:cp).and_call_original
436
-
437
- allow(described_class).to receive(:export_i18n_js_dir_path).and_return(export_i18n_js_dir_path)
438
- I18n::JS.export_i18n_js
439
- end
440
-
441
- context 'when .export_i18n_js_dir_path returns something' do
442
- let(:export_i18n_js_dir_path) { temp_path }
443
-
444
- it "does create the folder before copying" do
445
- expect(FileUtils).to have_received(:mkdir_p).with(export_i18n_js_dir_path).once
446
- end
447
- it "does copy the file with FileUtils.cp" do
448
- expect(FileUtils).to have_received(:cp).once
449
- end
450
- it "exports the file" do
451
- expect(File).to be_file(File.join(I18n::JS.export_i18n_js_dir_path, "i18n.js"))
452
- end
453
- end
454
-
455
- context 'when .export_i18n_js_dir_path is set to nil' do
456
- let(:export_i18n_js_dir_path) { nil }
457
-
458
- it "does NOT create the folder before copying" do
459
- expect(FileUtils).to_not have_received(:mkdir_p)
460
- end
461
- it "does NOT copy the file with FileUtils.cp" do
462
- expect(FileUtils).to_not have_received(:cp)
463
- end
464
- end
465
- end
466
-
467
- describe ".export_i18n_js with config" do
468
-
469
- let(:export_action) do
470
- allow(FileUtils).to receive(:mkdir_p).and_call_original
471
- allow(FileUtils).to receive(:cp).and_call_original
472
- I18n::JS.export_i18n_js
473
- end
474
-
475
- context 'when :export_i18n_js set in config' do
476
- before { set_config "js_export_dir_custom.yml"; export_action }
477
- let(:export_i18n_js_dir_path) { temp_path }
478
- let(:config_export_path) { "tmp/i18n-js/foo" }
479
-
480
- it "does create the folder before copying" do
481
- expect(FileUtils).to have_received(:mkdir_p).with(config_export_path).once
482
- end
483
- it "does copy the file with FileUtils.cp" do
484
- expect(FileUtils).to have_received(:cp).once
485
- end
486
- it "exports the file" do
487
- expect(File).to be_file(File.join(config_export_path, "i18n.js"))
488
- end
489
- end
490
-
491
- context 'when .export_i18n_js_dir_path is set to false' do
492
- before { set_config "js_export_dir_none.yml"; export_action }
493
-
494
- it "does NOT create the folder before copying" do
495
- expect(FileUtils).to_not have_received(:mkdir_p)
496
- end
497
-
498
- it "does NOT copy the file with FileUtils.cp" do
499
- expect(FileUtils).to_not have_received(:cp)
500
- end
501
- end
502
- end
503
-
504
- describe '.export_i18n_js_dir_path' do
505
- let(:default_path) { I18n::JS::DEFAULT_EXPORT_DIR_PATH }
506
- let(:new_path) { File.join("tmp", default_path) }
507
- after { described_class.send(:remove_instance_variable, :@export_i18n_js_dir_path) }
508
-
509
- subject { described_class.export_i18n_js_dir_path }
510
-
511
- context "when it is not set" do
512
- it { should eq default_path }
513
- end
514
- context "when it is set to another path already" do
515
- before { described_class.export_i18n_js_dir_path = new_path }
516
-
517
- it { should eq new_path }
518
- end
519
- context "when it is set to nil already" do
520
- before { described_class.export_i18n_js_dir_path = nil }
521
-
522
- it { should eq :none }
523
- end
524
- end
525
- end
526
-
527
- describe "translation key sorting" do
528
-
529
- describe ".sort_translation_keys?" do
530
- after { described_class.send(:remove_instance_variable, :@sort_translation_keys) }
531
- subject { described_class.sort_translation_keys? }
532
-
533
-
534
- context "set with config" do
535
-
536
- context 'when :sort_translation_keys is not set in config' do
537
- before :each do
538
- set_config "default.yml"
539
- end
540
-
541
- it { should eq true }
542
- end
543
-
544
- context 'when :sort_translation_keys set to true in config' do
545
- before :each do
546
- set_config "js_sort_translation_keys_true.yml"
547
- end
548
-
549
- it { should eq true }
550
- end
551
-
552
- context 'when :sort_translation_keys set to false in config' do
553
- before :each do
554
- set_config "js_sort_translation_keys_false.yml"
555
- end
556
-
557
- it { should eq false }
558
- end
559
- end
560
-
561
- context 'set by .sort_translation_keys' do
562
-
563
- context "when it is not set" do
564
- it { should eq true }
565
- end
566
-
567
- context "when it is set to true" do
568
- before { described_class.sort_translation_keys = true }
569
-
570
- it { should eq true }
571
- end
572
-
573
- context "when it is set to false" do
574
- before { described_class.sort_translation_keys = false }
575
-
576
- it { should eq false }
577
- end
578
- end
579
- end
580
-
581
- context "exporting" do
582
- subject do
583
- I18n::JS.export
584
- file_should_exist "en.js"
585
- File.read(File.join(I18n::JS.export_i18n_js_dir_path, "en.js"))
586
- end
587
-
588
- before do
589
- stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path)
590
- end
591
-
592
- context 'sort_translation_keys is true' do
593
- before :each do
594
- set_config "js_sort_translation_keys_true.yml"
595
- end
596
-
597
- it "exports with the keys sorted" do
598
- expect(subject).to eq <<EOS
599
- I18n.translations || (I18n.translations = {});
600
- I18n.translations["en"] = I18n.extend((I18n.translations["en"] || {}), {"admin":{"edit":{"title":"Edit"},"show":{"note":"more details","title":"Show"}},"date":{"abbr_day_names":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"abbr_month_names":[null,"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"day_names":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"formats":{"default":"%Y-%m-%d","long":"%B %d, %Y","short":"%b %d"},"month_names":[null,"January","February","March","April","May","June","July","August","September","October","November","December"]},"fallback_test":"Success","foo":"Foo","merge_plurals":{"one":"Apple","other":"Apples"},"null_test":"fallback for null","number":{"currency":{"format":{"delimiter":",","format":"%u%n","precision":2,"separator":".","unit":"$"}},"format":{"delimiter":",","precision":3,"separator":"."}},"time":{"am":"am","formats":{"default":"%a, %d %b %Y %H:%M:%S %z","long":"%B %d, %Y %H:%M","short":"%d %b %H:%M"},"pm":"pm"}});
601
- EOS
602
- end
603
- end
604
- end
605
- end
606
-
607
- describe "js_extend option" do
608
- before do
609
- stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path)
610
- end
611
-
612
- it "exports with js_extend option at parent level" do
613
- set_config "js_extend_parent.yml"
614
- I18n::JS.export
615
-
616
- file_should_exist "js_extend_parent.js"
617
-
618
- output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "js_extend_parent.js"))
619
- expect(output).to eq(<<EOS
620
- I18n.translations || (I18n.translations = {});
621
- I18n.translations[\"en\"] = {\"date\":{\"formats\":{\"default\":\"%Y-%m-%d\",\"long\":\"%B %d, %Y\",\"short\":\"%b %d\"}}};
622
- I18n.translations[\"fr\"] = {\"date\":{\"formats\":{\"default\":\"%d/%m/%Y\",\"long\":\"%e %B %Y\",\"long_ordinal\":\"%e %B %Y\",\"only_day\":\"%e\",\"short\":\"%e %b\"}}};
623
- EOS
624
- )
625
- end
626
-
627
- it "exports with js_extend option at segment level" do
628
- set_config "js_extend_segment.yml"
629
- I18n::JS.export
630
-
631
- file_should_exist "js_extend_segment.js"
632
-
633
- output = File.read(File.join(I18n::JS.export_i18n_js_dir_path, "js_extend_segment.js"))
634
- expect(output).to eq(<<EOS
635
- I18n.translations || (I18n.translations = {});
636
- I18n.translations[\"en\"] = {\"date\":{\"formats\":{\"default\":\"%Y-%m-%d\",\"long\":\"%B %d, %Y\",\"short\":\"%b %d\"}}};
637
- I18n.translations[\"fr\"] = {\"date\":{\"formats\":{\"default\":\"%d/%m/%Y\",\"long\":\"%e %B %Y\",\"long_ordinal\":\"%e %B %Y\",\"only_day\":\"%e\",\"short\":\"%e %b\"}}};
638
- EOS
639
- )
640
- end
641
- end
642
-
643
- describe "merging plural keys" do
644
- before do
645
- stub_const('I18n::JS::DEFAULT_EXPORT_DIR_PATH', temp_path)
646
- end
647
-
648
- subject do
649
- File.read(File.join(I18n::JS.export_i18n_js_dir_path, "merge_plurals.js"))
650
- end
651
-
652
- it "should correctly merge the plural keys" do
653
- set_config "merge_plurals.yml"
654
- I18n::JS.export
655
- file_should_exist "merge_plurals.js"
656
-
657
- expect(subject).to eq <<EOS
658
- I18n.translations || (I18n.translations = {});
659
- I18n.translations[\"en\"] = I18n.extend((I18n.translations[\"en\"] || {}), {\"merge_plurals\":{\"one\":\"Apple\",\"other\":\"Apples\"}});\nI18n.translations[\"fr\"] = I18n.extend((I18n.translations[\"fr\"] || {}), {\"merge_plurals\":{\"one\":\"Pomme\",\"other\":\"Pommes\",\"zero\":\"Pomme\"}});
660
- EOS
661
- end
662
- end
663
- end