Skip to content

Commit

Permalink
store translations for unavailable locales if enforce_available_local…
Browse files Browse the repository at this point in the history
…es is false

Fixes #404
  • Loading branch information
wjordan committed Feb 13, 2018
1 parent 888abcb commit 596a71d
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 596a71d

Please sign in to comment.