Mau*_*aes 386 ruby ruby-on-rails deprecated rails-i18n ruby-on-rails-4
我刚刚更新到rails 4.0.2并且我收到此警告:
[已弃用] I18n.enforce_available_locales将来默认为true.如果您确实想跳过语言环境的验证,可以设置I18n.enforce_available_locales = false以避免此消息.
将其设置为false是否存在任何安全问题?
Sim*_*tti 614
重要提示:确保您的应用未使用I18n 0.6.8,它有一个错误,无法正确设置配置.
为了使警告静音,请编辑application.rb文件并在Rails::Application正文中包含以下行
config.i18n.enforce_available_locales = true
Run Code Online (Sandbox Code Playgroud)
可能的值是:
注意:
false,而不是true.config.i18n.default_locale配置或其他i18n设置,请务必在设置后进行config.i18n.enforce_available_locales设置.config对象设置变量可能没有效果.在这种情况下,请将其直接设置为I18n使用I18n.config.enforce_available_locales.
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
Run Code Online (Sandbox Code Playgroud)
现在,在Rails 4(> = 4.0.2)和Rails 3.2(> = 3.2.14)中都会显示弃用警告.原因在此提交中进行了解释.
实施可用的区域设置
如果
I18n.config.enforce_available_locales为true,如果传递的语言环境不可用,我们将引发I18n :: InvalidLocale异常.默认设置为
nil将显示弃用错误.如果设置为
false我们将完全跳过强制执行可用的语言环境(旧行为).这已通过以下方法实现:
- I18n.config.default_locale =
- I18n.config.locale =
- I18n.translate
- I18n.localize
- I18n.transliterate
在此更改之前,如果您传递了不受支持的语言环境,如果语言环境有效(即文件/config/locales夹中是否有相应的语言环境文件),Rails将静默切换到它,否则语言环境将默认为config.i18n.default_locale配置(默认为:en ).
I18n gem的新版本迫使开发人员更加关注区域设置管理.
将来,行为将发生变化,如果区域设置无效,Rails应用程序将引发错误.
在准备此类更改时(可能会破坏直到今天依赖于静默默认值的多个应用程序),警告会强制您在当前过渡期间明确声明要执行的验证.
要恢复以前的行为,只需将以下配置设置为 false
config.i18n.enforce_available_locales = false
Run Code Online (Sandbox Code Playgroud)
否则,将其设置为true以匹配新的Rails默认值,或者如果您希望在域验证上更加严格,并避免在无效区域设置的情况下切换到默认值.
config.i18n.enforce_available_locales = true
Run Code Online (Sandbox Code Playgroud)
如果要设置的config.i18n.default_locale配置或使用任何前面提到的方法(default_locale=,locale=,translate等),确保设置后做config.i18n.enforce_available_locales设置.否则,弃用警告将继续弹出.(感谢FábioBatista).
如果您使用包含I18n功能的第三方宝石,则设置变量可能无效.实际上,问题与前一点中描述的相同,只是稍微难以调试.
这个问题是优先事项.当您在Rails应用程序中设置配置时,该值不会立即分配给I18n gem.Rails将每个配置存储在内部对象中,加载依赖项(Railties和第三方gem),然后将配置传递给目标类.如果您在配置分配给I18n之前使用调用任何I18n方法的gem(或Rails插件),那么您将收到警告.
在这种情况下,您需要跳过Rails堆栈并通过调用立即将配置设置为I18n gem
I18n.config.enforce_available_locales = true
Run Code Online (Sandbox Code Playgroud)
代替
config.i18n.enforce_available_locales = true
Run Code Online (Sandbox Code Playgroud)
这个问题很容易证明.尝试生成一个新的空Rails应用程序,你会看到,设置config.i18n在application.rb精品工程.
如果你的应用程序没有,有一个简单的方法来调试罪魁祸首.在系统中找到i18n gem,打开i18n.rb文件并编辑方法enforce_available_locales!以包含该语句puts caller.inspect.

这将导致该方法在调用时打印堆栈跟踪.通过检查堆栈跟踪(在我的情况下,它是Authlogic),您将能够确定哪个gem正在调用它.
["/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)>'",
Run Code Online (Sandbox Code Playgroud)mha*_*rtl 45
为了完整起见,请注意您也可以通过设置I18n.enforce_available_locales为true(或false)来消除警告config/application.rb:
require File.expand_path('../boot', __FILE__)
.
.
.
module SampleApp
class Application < Rails::Application
.
.
.
I18n.enforce_available_locales = true
.
.
.
end
end
Run Code Online (Sandbox Code Playgroud)
Jus*_*tin 10
似乎不是这样 - 这是i18n工作方式的先前行为 - 当您要求未实现/可用的区域设置时,新行为(true)将引发错误.
请参阅添加此警告的提交:https://github.com/svenfuchs/i18n/commit/3b6e56e06fd70f6e4507996b017238505e66608c
| 归档时间: |
|
| 查看次数: |
62196 次 |
| 最近记录: |