Skip to content

Commit

Permalink
Merge pull request #391 from stereobooster/dont-store-tr-for-locales-…
Browse files Browse the repository at this point in the history
…not-set-as-available

Don't store translations for locales not set as available
  • Loading branch information
radar committed Nov 9, 2017
2 parents 11b671e + 2b09acb commit 4e9b237
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/i18n.rb
Expand Up @@ -293,6 +293,10 @@ def enforce_available_locales!(locale)
end
end

def available_locales_initialized?
config.available_locales_initialized?
end

private

# Any exceptions thrown in translate will be sent to the @@exception_handler
Expand Down
5 changes: 5 additions & 0 deletions lib/i18n/backend/simple.rb
Expand Up @@ -29,6 +29,11 @@ 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? &&
!I18n.available_locales.include?(locale.to_sym) &&
!I18n.available_locales.include?(locale.to_s)
return data
end
locale = locale.to_sym
translations[locale] ||= {}
data = data.deep_symbolize_keys
Expand Down
5 changes: 5 additions & 0 deletions lib/i18n/config.rb
Expand Up @@ -57,6 +57,11 @@ def available_locales=(locales)
@@available_locales = nil if @@available_locales.empty?
@@available_locales_set = nil
end

# Returns true if the available_locales have been initialized
def available_locales_initialized?
( !!defined?(@@available_locales) && !!@@available_locales )
end

# Clears the available locales set so it can be recomputed again after I18n
# gets reloaded.
Expand Down
8 changes: 8 additions & 0 deletions test/backend/simple_test.rb
Expand Up @@ -70,6 +70,14 @@ 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
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]
end

# reloading translations

test "simple reload_translations: unloads translations" do
Expand Down

0 comments on commit 4e9b237

Please sign in to comment.