Skip to content

Commit

Permalink
Fix/i18n locale issue (#11197)
Browse files Browse the repository at this point in the history
* Revert "Fix Duplicate Locales Coming From spree_i18n (#10871)"

* Map to_sym so unique works.

* Fix test.

* Hard code values for result.

* Static code expected results.
  • Loading branch information
MatthewKennedy committed Aug 3, 2021
1 parent 30c688e commit 41295bd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
7 changes: 6 additions & 1 deletion core/lib/spree/i18n.rb
Expand Up @@ -18,7 +18,12 @@ def translate(key, options = {})

def available_locales
locales_from_i18n = I18n.available_locales
locales = [Rails.application.config.i18n.default_locale, I18n.locale, :en]
locales =
if defined?(SpreeI18n)
(SpreeI18n::Locale.all << :en).map(&:to_sym)
else
[Rails.application.config.i18n.default_locale, I18n.locale, :en]
end

(locales + locales_from_i18n).uniq.compact
end
Expand Down
31 changes: 27 additions & 4 deletions core/spec/lib/i18n_spec.rb
Expand Up @@ -19,12 +19,35 @@
end

describe '#available_locales' do
it 'returns locales defined in Spree and all other installed gems' do
locales = Spree.available_locales
context 'when SpreeI18n is defined' do
before do
class_double('SpreeI18n').
as_stubbed_const(transfer_nested_constants: true)
class_double('SpreeI18n::Locale', all: ['en',:en, :de, :nl]).as_stubbed_const(transfer_nested_constants: true)
end

it 'returns all locales from the SpreeI18n' do
locales = Spree.available_locales
expected_locales = [:en, :de, :nl, :ar, :az, :bg, :ca, :cs, :da, :el, :es, :fa, :fi, :fr, :hu, :id, :it, :ja, :"pt-BR", :ro, :ru, :sk, :tr, :"zh-CN", :"zh-TW", :pl, :uk, :vi]

expect(locales).to eq expected_locales
end

it 'returns an array with the string "en" removed' do
locales = Spree.available_locales

expected_locales = ([Rails.application.config.i18n.default_locale, I18n.locale, :en] + I18n.available_locales).uniq.compact
expect(locales).not_to include('en')
end
end

context 'when SpreeI18n is not defined' do
it 'returns just default locales' do
locales = Spree.available_locales

expect(locales).to eq expected_locales
expected_locales = [:en, :ar, :az, :bg, :ca, :cs, :da, :de, :el, :es, :fa, :fi, :fr, :hu, :id, :it, :ja, :nl, :"pt-BR", :ro, :ru, :sk, :tr, :"zh-CN", :"zh-TW", :pl, :uk, :vi]

expect(locales).to eq expected_locales
end
end
end

Expand Down

0 comments on commit 41295bd

Please sign in to comment.