Skip to content

Commit

Permalink
Default I18n.enforce_available_locales to true
Browse files Browse the repository at this point in the history
We will default this option to true from now on to ensure users properly
handle their list of available locales whenever necessary. This option
was added as a security measure and thus Rails will follow it defaulting
to secure option.

Also improve the handling of I18n config options in its railtie, taking
the new enforce_available_locales option into account, by setting it as
the last one in the process. This ensures no other configuration will
trigger a deprecation warning due to that setting.
  • Loading branch information
carlosantoniodasilva committed Dec 17, 2013
1 parent aaa5463 commit c445c6d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
14 changes: 10 additions & 4 deletions activesupport/lib/active_support/i18n_railtie.rb
Expand Up @@ -8,6 +8,8 @@ class Railtie < Rails::Railtie
config.i18n.railties_load_path = []
config.i18n.load_path = []
config.i18n.fallbacks = ActiveSupport::OrderedOptions.new
# Enforce I18n to check the available locales when setting a locale.
config.i18n.enforce_available_locales = true

# Set the i18n configuration after initialization since a lot of
# configuration is still usually done in application initializers.
Expand All @@ -31,10 +33,11 @@ def self.initialize_i18n(app)

fallbacks = app.config.i18n.delete(:fallbacks)

if app.config.i18n.has_key?(:enforce_available_locales)
# this option needs to be set before `default_locale=` to work properly.
I18n.enforce_available_locales = app.config.i18n.delete(:enforce_available_locales)
end
# Avoid issues with setting the default_locale by disabling available locales
# check while configuring.
enforce_available_locales = app.config.i18n.delete(:enforce_available_locales)
enforce_available_locales = I18n.enforce_available_locales unless I18n.enforce_available_locales.nil?
I18n.enforce_available_locales = false

app.config.i18n.each do |setting, value|
case setting
Expand All @@ -49,6 +52,9 @@ def self.initialize_i18n(app)

init_fallbacks(fallbacks) if fallbacks && validate_fallbacks(fallbacks)

# Restore avalable locales check so it will take place from now on.
I18n.enforce_available_locales = enforce_available_locales

reloader = ActiveSupport::FileUpdateChecker.new(I18n.load_path.dup){ I18n.reload! }
app.reloaders << reloader
ActionDispatch::Reloader.to_prepare { reloader.execute_if_updated }
Expand Down
42 changes: 38 additions & 4 deletions railties/test/application/initializers/i18n_test.rb
Expand Up @@ -184,14 +184,48 @@ class Foo < ActiveRecord::Base
assert_fallbacks ca: [:ca, :"es-ES", :es, :'en-US', :en]
end

test "config.i18n.enforce_available_locales is set before config.i18n.default_locale is" do
test "config.i18n.enforce_available_locales is set to true by default and avoids I18n warnings" do
add_to_config <<-RUBY
config.i18n.default_locale = :it
config.i18n.enforce_available_locales = true
RUBY

assert_raises(I18n::InvalidLocale) do
load_app
output = capture(:stderr) { load_app }
assert_no_match %r{deprecated.*enforce_available_locales}, output
assert_equal true, I18n.enforce_available_locales

assert_raise I18n::InvalidLocale do
I18n.locale = :es
end
end

test "disable config.i18n.enforce_available_locales" do
add_to_config <<-RUBY
config.i18n.enforce_available_locales = false
config.i18n.default_locale = :fr
RUBY

output = capture(:stderr) { load_app }
assert_no_match %r{deprecated.*enforce_available_locales}, output
assert_equal false, I18n.enforce_available_locales

assert_nothing_raised do
I18n.locale = :es
end
end

test "default config.i18n.enforce_available_locales does not override I18n.enforce_available_locales" do
I18n.enforce_available_locales = false

add_to_config <<-RUBY
config.i18n.default_locale = :fr
RUBY

output = capture(:stderr) { load_app }
assert_no_match %r{deprecated.*enforce_available_locales}, output
assert_equal false, I18n.enforce_available_locales

assert_nothing_raised do
I18n.locale = :es
end
end
end
Expand Down

0 comments on commit c445c6d

Please sign in to comment.