Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store translations for unavailable locales if enforce_available_locales is false #408

Merged
merged 1 commit into from Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/i18n/backend/simple.rb
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
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