From c445c6d1b99c987b7f98943db46f705a3acc400f Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 12 Dec 2013 08:28:30 -0200 Subject: Default I18n.enforce_available_locales to true 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. --- activesupport/lib/active_support/i18n_railtie.rb | 14 +++++--- .../test/application/initializers/i18n_test.rb | 42 +++++++++++++++++++--- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/activesupport/lib/active_support/i18n_railtie.rb b/activesupport/lib/active_support/i18n_railtie.rb index dcdea70443..ac9bca44b6 100644 --- a/activesupport/lib/active_support/i18n_railtie.rb +++ b/activesupport/lib/active_support/i18n_railtie.rb @@ -8,6 +8,8 @@ module I18n 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. @@ -31,10 +33,11 @@ module I18n 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 @@ -49,6 +52,9 @@ module I18n 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 } diff --git a/railties/test/application/initializers/i18n_test.rb b/railties/test/application/initializers/i18n_test.rb index 2a64cd8ba7..bc34897cdf 100644 --- a/railties/test/application/initializers/i18n_test.rb +++ b/railties/test/application/initializers/i18n_test.rb @@ -184,14 +184,48 @@ en: 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 -- cgit v1.2.3 From 6802196a6b7db928f9dc70fe5a4f3b61a3ea33ce Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 16 Dec 2013 16:04:07 -0200 Subject: Disable available locales checks to avoid warnings running the tests --- actionmailer/test/abstract_unit.rb | 3 +++ actionpack/test/abstract_unit.rb | 3 +++ actionview/test/abstract_unit.rb | 3 +++ activerecord/test/cases/helper.rb | 3 +++ activesupport/test/abstract_unit.rb | 3 +++ 5 files changed, 15 insertions(+) diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb index aa18c512c7..cf790c7487 100644 --- a/actionmailer/test/abstract_unit.rb +++ b/actionmailer/test/abstract_unit.rb @@ -20,6 +20,9 @@ ActionMailer::Base.send(:include, ActionView::Layouts) # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Disable available locale checks to avoid warnings running the test suite. +I18n.enforce_available_locales = false + # Bogus template processors ActionView::Template.register_template_handler :haml, lambda { |template| "Look its HAML!".inspect } ActionView::Template.register_template_handler :bak, lambda { |template| "Lame backup".inspect } diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 0e163a93eb..87db9a3de4 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -43,6 +43,9 @@ Thread.abort_on_exception = true # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Disable available locale checks to avoid warnings running the test suite. +I18n.enforce_available_locales = false + # Register danish language for testing I18n.backend.store_translations 'da', {} I18n.backend.store_translations 'pt-BR', {} diff --git a/actionview/test/abstract_unit.rb b/actionview/test/abstract_unit.rb index eef0abb609..9928da4774 100644 --- a/actionview/test/abstract_unit.rb +++ b/actionview/test/abstract_unit.rb @@ -42,6 +42,9 @@ Thread.abort_on_exception = true # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Disable available locale checks to avoid warnings running the test suite. +I18n.enforce_available_locales = false + # Register danish language for testing I18n.backend.store_translations 'da', {} I18n.backend.store_translations 'pt-BR', {} diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 40be378797..3758224b0c 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -20,6 +20,9 @@ Thread.abort_on_exception = true # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Disable available locale checks to avoid warnings running the test suite. +I18n.enforce_available_locales = false + # Connect to the database ARTest.connect diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 4600855998..1dfa3833f0 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -24,6 +24,9 @@ Thread.abort_on_exception = true # Show backtraces for deprecated behavior for quicker cleanup. ActiveSupport::Deprecation.debug = true +# Disable available locale checks to avoid warnings running the test suite. +I18n.enforce_available_locales = false + # Skips the current run on Rubinius using Minitest::Assertions#skip def rubinius_skip(message = '') skip message if RUBY_ENGINE == 'rbx' -- cgit v1.2.3 From 8e21ae37ad9fef6b7393a84f9b5f2e18a831e49a Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 16 Dec 2013 17:12:37 -0200 Subject: Add changelog and upgrading notice related to I18n enforce_available_locales handling --- activesupport/CHANGELOG.md | 18 +++++++++++++----- guides/source/upgrading_ruby_on_rails.md | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 6a13db346d..c830ee61e6 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,16 @@ +* Default the new `I18n.enforce_available_locales` config to `true`, meaning + `I18n` will make sure that all locales passed to it must be declared in the + `available_locales` list. + + To disable it add the following configuration to your application: + + config.i18n.enforce_available_locales = false + + This also ensures I18n configuration is properly initialized taking the new + option into account, to avoid their deprecations while booting up the app. + + *Carlos Antonio da Silva*, *Yves Senn* + * Introduce Module#concerning: a natural, low-ceremony way to separate responsibilities within a class. @@ -49,11 +62,6 @@ *Mario Visic* -* Ensure `config.i18n.enforce_available_locales` is set before any other - configuration option. - - *Yves Senn* - * Added `Date#all_week/month/quarter/year` for generating date ranges. *Dmitriy Meremyanin* diff --git a/guides/source/upgrading_ruby_on_rails.md b/guides/source/upgrading_ruby_on_rails.md index ca5623bf73..1c233b4d82 100644 --- a/guides/source/upgrading_ruby_on_rails.md +++ b/guides/source/upgrading_ruby_on_rails.md @@ -156,6 +156,23 @@ end ActiveRecord::FixtureSet.context_class.send :include, FixtureFileHelpers ``` +### I18n enforcing available locales + +Rails 4.1 now defaults the I18n option `enforce_available_locales` to `true`, +meaning that it will make sure that all locales passed to it must be declared in +the `available_locales` list. + +To disable it (and allow I18n to accept *any* locale option) add the following +configuration to your application: + +```ruby +config.i18n.enforce_available_locales = false +``` + +Note that this option was added as a security measure, to ensure user input could +not be used as locale information unless previously known, so it's recommended not +to disable this option unless you have a strong reason for doing so. + Upgrading from Rails 3.2 to Rails 4.0 ------------------------------------- -- cgit v1.2.3 From 7c858b03a9de3c600594ca3c88540b14d835dd91 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Mon, 16 Dec 2013 18:03:12 -0200 Subject: Require I18n >= 0.6.9 The option enforce_available_locales is only available on latest versions, so require the last available one which has the option + other related fixes and should not have backward compatibility issues. --- activesupport/activesupport.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activesupport/activesupport.gemspec b/activesupport/activesupport.gemspec index 0427022dc6..f3625e8b79 100644 --- a/activesupport/activesupport.gemspec +++ b/activesupport/activesupport.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |s| s.rdoc_options.concat ['--encoding', 'UTF-8'] - s.add_dependency 'i18n', '~> 0.6', '>= 0.6.4' + s.add_dependency 'i18n', '~> 0.6', '>= 0.6.9' s.add_dependency 'json', '~> 1.7', '>= 1.7.7' s.add_dependency 'tzinfo', '~> 1.1' s.add_dependency 'minitest', '~> 5.1' -- cgit v1.2.3