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

Don't store translations for locales not set as available with I18n#available_locales= #261

Closed
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
4 changes: 4 additions & 0 deletions lib/i18n.rb
Expand Up @@ -286,6 +286,10 @@ def enforce_available_locales!(locale)
end
end

def available_locales_initialized?
config.available_locales_initialized?
end

# making these private until Ruby 1.9.2 can send to protected methods again
# see http://redmine.ruby-lang.org/repositories/revision/ruby-19?rev=24280
private
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) == false &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably reduce this to just !I18n.available_locales.map(&:to_s).include?(locale.to_s)

I18n.available_locales.include?(locale.to_s) == false
return translations
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 @@ -55,6 +55,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

# Returns the current default scope separator. Defaults to '.'
def default_separator
Expand Down
8 changes: 8 additions & 0 deletions test/backend/simple_test.rb
Expand Up @@ -69,6 +69,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