我刚刚更新到rails 4.0.2,我得到了这个警告:

(弃用)I18n。enfort_available_locales将来将默认为true。如果你真的想跳过locale验证,你可以设置I18n。enfort_available_locales = false以避免此消息。

设置为false是否存在安全问题?


当前回答

看起来不是这样的-这是i18n工作方式的前一种行为-当你要求一个未实现/可用的区域时,新行为(true)将引发一个错误。

请参阅添加此警告的提交:https://github.com/svenfuchs/i18n/commit/3b6e56e06fd70f6e4507996b017238505e66608c

其他回答

重要:确保你的应用没有使用I18n 0.6.8,它有一个错误,阻止配置被正确设置。


简短的回答

为了关闭警告,请编辑应用程序。并在Rails::Application主体中包含以下行

config.i18n.enforce_available_locales = true

可能取值为:

错误:如果你 想要跳过区域验证 不要关心locale 正确:如果你 如果传递了无效的区域设置,希望应用程序引发错误(或) 想默认为新的Rails行为(或) 关心locale验证

注意:

旧的默认行为对应于false,而不是true。 如果您正在设置config.i18n.default_locale配置或其他i18n设置,请确保在设置config.i18n之后执行该操作。enforce_available_locales设置。 如果您使用包含I18n特性的第三方gem,通过应用程序配置对象设置变量可能没有效果。在本例中,使用I18n.config.enforce_available_locales将其直接设置为I18n。 警告

例子

require File.expand_path('../boot', __FILE__)

# ...

module YouApplication
  class Application < Rails::Application

    # ...

    config.i18n.enforce_available_locales = true
    # or if one of your gem compete for pre-loading, use
    I18n.config.enforce_available_locales = true

    # ...

  end
end

长回答

现在在Rails 4(>= 4.0.2)和Rails 3.2(>= 3.2.14)中都显示了弃用警告。原因在此提交中进行了解释。

强制可用区域设置 当I18n.config。enfort_available_locales为真,则引发an I18n::InvalidLocale异常,如果传递的区域不可用。 默认设置为nil,将显示弃用错误。 如果设置为false,我们将完全跳过执行可用区域设置(旧行为)。 这已经通过以下方法实现: I18n.config.default_locale = I18n.config.locale = I18n.translate I18n.localize I18n.transliterate

在此更改之前,如果您传递了一个不支持的区域设置,如果该区域设置有效(例如,如果/config/locale文件夹中有相应的区域设置文件),Rails将无声地切换到它,否则区域设置将默认为config.i18n.default_locale配置(默认为:en)。

I18n gem的新版本迫使开发人员更加注意区域管理。

将来,该行为将会改变,如果区域设置无效,Rails应用程序将引发一个错误。

为了准备这样的更改(这可能会破坏几个直到今天还依赖静默默认值的应用程序),警告迫使您在当前过渡期间显式声明要执行哪个验证。

要恢复之前的行为,只需将以下配置设置为false

config.i18n.enforce_available_locales = false

否则,将其设置为true以匹配新的Rails默认值,或者如果您想在域验证上更严格,并避免在无效区域设置的情况下切换到默认值。

config.i18n.enforce_available_locales = true

警告

If you are setting the config.i18n.default_locale configuration or using any of the previously mentioned methods (default_locale=, locale=, translate, etc), make sure to do it after setting the config.i18n.enforce_available_locales setting. Otherwise, the deprecation warning will keep on popping up. (Thanks Fábio Batista). If you use third party gems that include I18n features, setting the variable through may not have effect. In fact, the issue is the same as described in the previous point, just a little bit harder to debug. This issue is a matter of precedence. When you set the config in your Rails app, the value is not immediately assigned to the I18n gem. Rails stores each config in an internal object, loads the dependencies (Railties and third party gems) and then it passes the configuration to the target classes. If you use a gem (or Rails plugin) that calls any of the I18n methods before the config is assigned to I18n, then you'll get the warning. In this case, you need to skip the Rails stack and set the config immediately to the I18n gem by calling I18n.config.enforce_available_locales = true instead of config.i18n.enforce_available_locales = true The issue is easy to prove. Try to generate a new empty Rails app and you will see that setting config.i18n in the application.rb works fine. If in your app it does not, there is an easy way to debug the culprit. Locate the i18n gem in your system, open the i18n.rb file and edit the method enforce_available_locales! to include the statement puts caller.inspect. This will cause the method to print the stacktrace whenever invoked. You will be able to determine which gem is calling it by inspecting the stacktrace (in my case it was Authlogic). ["/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/i18n-0.6.9/lib/i18n.rb:150:in `translate'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/i18n/translator.rb:8:in `translate'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/i18n.rb:79:in `translate'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:68:in `validates_format_of_email_field_options'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:102:in `block in included'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:99:in `class_eval'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:99:in `included'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `include'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `block in acts_as_authentic'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `each'", "/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `acts_as_authentic'", "/Users/weppos/Projects/application/app/models/user.rb:8:in `<class:User>'", "/Users/weppos/Projects/application/app/models/user.rb:1:in `<top (required)>'",

如果你想关心locale,请写入应用程序。rb文件。

config.i18n.enforce_available_locales = true

如果locale验证,你可以写false,并且不关心它。

I18n.config。enforce_available_locales = true在Rails 3.2.16中为我工作(我把它放在config/application.rb)

为了完整起见,请注意,您还可以通过设置I18n来消除警告。在config/application.rb中,enfort_available_locales为true(或false):

require File.expand_path('../boot', __FILE__)
.
.
.
module SampleApp
  class Application < Rails::Application
    .
    .
    .
    I18n.enforce_available_locales = true
    .
    .
    .
  end
end

看起来不是这样的-这是i18n工作方式的前一种行为-当你要求一个未实现/可用的区域时,新行为(true)将引发一个错误。

请参阅添加此警告的提交:https://github.com/svenfuchs/i18n/commit/3b6e56e06fd70f6e4507996b017238505e66608c