提交 ae196e85 编写于 作者: C Carlos Antonio da Silva

Merge pull request #13341 from carlosantoniodasilva/ca-i18n

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.
......@@ -20,6 +20,9 @@
# 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 }
......
......@@ -43,6 +43,9 @@ def env
# 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', {}
......
......@@ -42,6 +42,9 @@ def env
# 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', {}
......
......@@ -20,6 +20,9 @@
# 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
......
* 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*
......
......@@ -20,7 +20,7 @@
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'
......
......@@ -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.
......@@ -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
......@@ -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 }
......
......@@ -24,6 +24,9 @@
# 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'
......
......@@ -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
-------------------------------------
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册