i18n-js 3.8.4 → 3.9.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 228b00c6b2733537cc164228e6da21dfee1531b26186258861b2f710b7c80cae
4
- data.tar.gz: 000d759f9753bb29900a7e09eca044f643cbdde5b0779eeb71683805ac780e14
3
+ metadata.gz: '08599ed12ef6edb132d1a71b74e5ca2f3b3c04dc4463286414540f4631676984'
4
+ data.tar.gz: 3dd94b7a9974952a5cb7996c222d1aaeb3e5c756cc811a8890e4d5fa79ffccb3
5
5
  SHA512:
6
- metadata.gz: a180630b6968c7e38723f8d42828e3d9aa6c32d0250e2e28c4a56a644d6a943124bd7183908f5e05616237dc9ebefd69c21a1f0190f2995062745fb12f7b76d9
7
- data.tar.gz: b301cb38ecf02cbf08b92798b0d7457fc00c1903f3fb27ca9c5d821c0c9fdf7f7f500b19b935a1eb3052a066b3057bc763635c4e0e8daf8f7448c500ffc90746
6
+ metadata.gz: 26138b1fafc184b7858a97969130f29f9562cdaab41d1f05c1e31a37064a2f40a31b4d0a349e12ddaa6f769ab229618a854efaee0102f99821edd489b9d1deb5
7
+ data.tar.gz: d763f7ebe8d1bba649c521bba03fa7fb77e4283b90ce6f1a1bb78ebf22dc7146f4e2eb02484f7d9f104d4d5454d90d0622af14128c6b383cd399e52aabaaf08b
data/CHANGELOG.md CHANGED
@@ -18,6 +18,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
18
18
  - Nothing
19
19
 
20
20
 
21
+ ## [3.9.0]
22
+
23
+ ### Added
24
+
25
+ - [Ruby] Allow to set custom locales instead of using only `I18n.available_locales`.
26
+ (PR: https://github.com/fnando/i18n-js/pull/617)
27
+
28
+
21
29
  ## [3.8.4]
22
30
 
23
31
  ### Fixed
@@ -509,7 +517,8 @@ And today is not April Fools' Day
509
517
 
510
518
 
511
519
 
512
- [Unreleased]: https://github.com/fnando/i18n-js/compare/v3.8.4...HEAD
520
+ [Unreleased]: https://github.com/fnando/i18n-js/compare/v3.9.0...HEAD
521
+ [3.9.0]: https://github.com/fnando/i18n-js/compare/v3.8.4...v3.9.0
513
522
  [3.8.4]: https://github.com/fnando/i18n-js/compare/v3.8.3...v3.8.4
514
523
  [3.8.3]: https://github.com/fnando/i18n-js/compare/v3.8.2...v3.8.3
515
524
  [3.8.2]: https://github.com/fnando/i18n-js/compare/v3.8.1...v3.8.2
data/README.md CHANGED
@@ -271,6 +271,17 @@ You must disable this feature by setting the option to `false`.
271
271
  To find more examples on how to use the configuration file please refer to the
272
272
  tests.
273
273
 
274
+ #### Available locales
275
+
276
+ By specifying option `js_available_locales` with a list of locales, this list
277
+ would be used instead of default `I18n.available_locales` to generate translations.
278
+
279
+ Example:
280
+
281
+ ```yaml
282
+ js_available_locales: ["de", "en"]
283
+ ```
284
+
274
285
  #### Namespace
275
286
 
276
287
  Setting the `namespace` option will change the namespace of the output
data/lib/i18n/js.rb CHANGED
@@ -81,7 +81,7 @@ module I18n
81
81
 
82
82
  # deep_merge! given result with result for fallback locale
83
83
  def self.merge_with_fallbacks!(result)
84
- I18n.available_locales.each do |locale|
84
+ js_available_locales.each do |locale|
85
85
  fallback_locales = FallbackLocales.new(fallbacks, locale)
86
86
  fallback_locales.each do |fallback_locale|
87
87
  # `result[fallback_locale]` could be missing
@@ -183,7 +183,7 @@ module I18n
183
183
  #
184
184
  # So the input is wrapped by our class for better `#slice`
185
185
  Private::HashWithSymbolKeys.new(translations).
186
- slice(*::I18n.available_locales).
186
+ slice(*::I18n::JS.js_available_locales).
187
187
  to_h
188
188
  end
189
189
  end
@@ -213,6 +213,16 @@ module I18n
213
213
  end
214
214
  end
215
215
 
216
+ # Get all available locales.
217
+ #
218
+ # @return [Array<Symbol>] the locales.
219
+ def self.js_available_locales
220
+ config.fetch(:js_available_locales) do
221
+ # default value
222
+ I18n.available_locales
223
+ end.map(&:to_sym)
224
+ end
225
+
216
226
  def self.sort_translation_keys?
217
227
  @sort_translation_keys ||= (config[:sort_translation_keys]) if config.key?(:sort_translation_keys)
218
228
  @sort_translation_keys = true if @sort_translation_keys.nil?
@@ -33,7 +33,7 @@ module I18n
33
33
  # Saves JSON file containing translations
34
34
  def save!
35
35
  if @file =~ LOCALE_INTERPOLATOR
36
- I18n.available_locales.each do |locale|
36
+ I18n::JS.js_available_locales.each do |locale|
37
37
  write_file(file_for_locale(locale), @translations.slice(locale))
38
38
  end
39
39
  else
@@ -2,6 +2,6 @@
2
2
 
3
3
  module I18n
4
4
  module JS
5
- VERSION = "3.8.4"
5
+ VERSION = "3.9.0"
6
6
  end
7
7
  end
@@ -0,0 +1 @@
1
+ js_available_locales: ["en", "foo"]
@@ -307,21 +307,30 @@ EOS
307
307
  expect(subject[:de][:null_test]).to eql(nil)
308
308
  end
309
309
 
310
- context "when given locale is in `I18n.available_locales` but its translation is missing" do
310
+ context 'when given locale is in `I18n.available_locales` but its translation is missing' do
311
311
  subject { translations[:fr][:fallback_test] }
312
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] }
313
+ let(:available_locales) { %i[fr pirate] }
314
+
316
315
  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"
316
+ allow(::I18n).to receive(:available_locales).and_return(available_locales)
317
+ set_config 'js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml'
319
318
  end
320
- after do
321
- I18n.config.available_locales = old_available_locales
319
+
320
+ it { should eql(nil) }
321
+ end
322
+
323
+ context 'when given locale is in `.js_available_locales` but its translation is missing' do
324
+ subject { translations[:fr][:fallback_test] }
325
+
326
+ let(:available_locales) { %i[fr pirate] }
327
+
328
+ before do
329
+ allow(described_class).to receive(:js_available_locales).and_return(available_locales)
330
+ set_config 'js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml'
322
331
  end
323
332
 
324
- it {should eql(nil)}
333
+ it { should eql(nil) }
325
334
  end
326
335
 
327
336
  context "with I18n::Fallbacks enabled" do
@@ -385,27 +394,67 @@ EOS
385
394
  end
386
395
  end
387
396
 
388
- context "I18n.available_locales" do
397
+ describe '.js_available_locales' do
398
+ subject { described_class.js_available_locales }
399
+
400
+ let(:results) { described_class.scoped_translations('*.admin.*.title') }
401
+ let(:result) { ->(locale) { results[locale][:admin][:show][:title] } }
389
402
 
390
- context "when I18n.available_locales is not set" do
391
- it "should allow all locales" do
392
- result = I18n::JS.scoped_translations("*.admin.*.title")
403
+ context 'when I18n.available_locales is not set' do
404
+ it { expect(subject).to eq ::I18n.available_locales }
393
405
 
394
- expect(result[:en][:admin][:show][:title]).to eql("Show")
395
- expect(result[:fr][:admin][:show][:title]).to eql("Visualiser")
396
- expect(result[:ja][:admin][:show][:title]).to eql("Ignore me")
406
+ it 'should allow all locales' do
407
+ expect(result.call(:en)).to eql('Show')
408
+ expect(result.call(:fr)).to eql('Visualiser')
409
+ expect(result.call(:ja)).to eql('Ignore me')
397
410
  end
398
411
  end
399
412
 
400
- context "when I18n.available_locales is set" do
401
- before { allow(::I18n).to receive(:available_locales){ [:en, :fr] } }
413
+ context 'when I18n.available_locales is set' do
414
+ let(:available_locales) { %i[en fr] }
415
+
416
+ before { allow(::I18n).to receive(:available_locales).and_return(available_locales) }
417
+
418
+ it { expect(subject).to eq available_locales }
419
+
420
+ it 'should ignore non-valid locales' do
421
+ expect(result.call(:en)).to eql('Show')
422
+ expect(result.call(:fr)).to eql('Visualiser')
423
+ expect(results).not_to include(:ja)
424
+ end
425
+
426
+ context 'when :js_available_locales set in config' do
427
+ before { set_config 'js_available_locales_custom.yml' }
402
428
 
403
- it "should ignore non-valid locales" do
404
- result = I18n::JS.scoped_translations("*.admin.*.title")
429
+ it { expect(subject).to eq %i[en foo] }
430
+
431
+ it 'should ignore non-valid locales' do
432
+ expect(result.call(:en)).to eql('Show')
433
+ expect(results).not_to include(:fr, :ja)
434
+ end
435
+ end
436
+ end
437
+ end
438
+
439
+ context 'I18n.available_locales' do
440
+ let(:results) { described_class.scoped_translations('*.admin.*.title') }
441
+ let(:result) { ->(locale) { results[locale][:admin][:show][:title] } }
442
+
443
+ context 'when I18n.available_locales is not set' do
444
+ it 'should allow all locales' do
445
+ expect(result.call(:en)).to eql('Show')
446
+ expect(result.call(:fr)).to eql('Visualiser')
447
+ expect(result.call(:ja)).to eql('Ignore me')
448
+ end
449
+ end
450
+
451
+ context 'when I18n.available_locales is set' do
452
+ before { allow(::I18n).to receive(:available_locales){ [:en, :fr] } }
405
453
 
406
- expect(result[:en][:admin][:show][:title]).to eql("Show")
407
- expect(result[:fr][:admin][:show][:title]).to eql("Visualiser")
408
- expect(result.keys.include?(:ja)).to eql(false)
454
+ it 'should ignore non-valid locales' do
455
+ expect(result.call(:en)).to eql('Show')
456
+ expect(result.call(:fr)).to eql('Visualiser')
457
+ expect(results).not_to include(:ja)
409
458
  end
410
459
  end
411
460
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18n-js
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.8.4
4
+ version: 3.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-27 00:00:00.000000000 Z
11
+ date: 2021-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -152,6 +152,7 @@ files:
152
152
  - spec/fixtures/default.yml
153
153
  - spec/fixtures/erb.yml
154
154
  - spec/fixtures/except_condition.yml
155
+ - spec/fixtures/js_available_locales_custom.yml
155
156
  - spec/fixtures/js_export_dir_custom.yml
156
157
  - spec/fixtures/js_export_dir_none.yml
157
158
  - spec/fixtures/js_extend_parent.yml
@@ -236,6 +237,7 @@ test_files:
236
237
  - spec/fixtures/default.yml
237
238
  - spec/fixtures/erb.yml
238
239
  - spec/fixtures/except_condition.yml
240
+ - spec/fixtures/js_available_locales_custom.yml
239
241
  - spec/fixtures/js_export_dir_custom.yml
240
242
  - spec/fixtures/js_export_dir_none.yml
241
243
  - spec/fixtures/js_extend_parent.yml