Skip to content

Commit

Permalink
Merge pull request #408 from wjordan/enforce_available_locales_false_fix
Browse files Browse the repository at this point in the history
store translations for unavailable locales if enforce_available_locales is false
  • Loading branch information
radar authored Feb 13, 2018
2 parents 888abcb 596a71d commit 29fe565
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion lib/i18n/backend/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 29,8 @@ def initialized?
# translations will be overwritten by new ones only at the deepest
# level of the hash.
def store_translations(locale, data, options = {})
if I18n.available_locales_initialized? &&
if I18n.enforce_available_locales &&
I18n.available_locales_initialized? &&
!I18n.available_locales.include?(locale.to_sym) &&
!I18n.available_locales.include?(locale.to_s)
return data
Expand Down
19 changes: 15 additions & 4 deletions test/backend/simple_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 70,23 @@ def setup
assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], translations
end

test "simple store_translations: do not store translations for locales not explicitly marked as available" do
test "simple store_translations: do not store translations unavailable locales if enforce_available_locales is true" do
begin
I18n.enforce_available_locales = true
I18n.available_locales = [:en, :es]
store_translations(:fr, :foo => {:bar => 'barfr', :baz => 'bazfr'})
store_translations(:es, :foo => {:bar => 'bares', :baz => 'bazes'})
assert_nil translations[:fr]
assert_equal Hash[:foo, {:bar => 'bares', :baz => 'bazes'}], translations[:es]
ensure
I18n.config.enforce_available_locales = false
end
end

test "simple store_translations: store translations for unavailable locales if enforce_available_locales is false" do
I18n.available_locales = [:en, :es]
store_translations(:fr, :foo => {:bar => 'barfr', :baz => 'bazfr'})
store_translations(:es, :foo => {:bar => 'bares', :baz => 'bazes'})
assert_nil translations[:fr]
assert_equal Hash[:foo, {:bar => 'bares', :baz => 'bazes'}], translations[:es]
assert_equal Hash[:foo, {:bar => 'barfr', :baz => 'bazfr'}], translations[:fr]
end

# reloading translations
Expand Down

0 comments on commit 29fe565

Please sign in to comment.