Rails I18n验证弃用警告

升级到Rails 4.0.2后,开发者收到了I18n.enforce_available_locales的弃用警告。这个警告提醒未来默认将强制验证可用的区域设置。将其设为false可以禁用验证,但可能带来安全风险。设置为true将遵循新的Rails行为,即在使用无效区域设置时引发错误。在设置I18n配置时,需注意设置顺序,确保在加载第三方gem之后,以免引起警告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Rails I18n validation deprecation warning

I just updated to rails 4.0.2 and I'm getting this warning: 我刚刚更新到rails 4.0.2并且我收到此警告:

[deprecated] I18n.enforce_available_locales will default to true in the future. [已弃用] I18n.enforce_available_locales将来默认为true。 If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message. 如果您确实想跳过语言环境的验证,可以设置I18n.enforce_available_locales = false以避免此消息。

Is there any security issue in setting it to false? 将其设置为false是否存在任何安全问题?


#1楼

参考:https://stackoom.com/question/1NQw8/Rails-I-n验证弃用警告


#2楼

Doesn't seem that way - that'd be previous behavior of the way i18n works - new behavior (true) will raise an error when you ask for a locale not implemented/available. 似乎不是这样 - 这是i18n工作方式的先前行为 - 当您要求未实现/可用的区域设置时,新行为(true)将引发错误。

See the commit that added this warning: https://github.com/svenfuchs/i18n/commit/3b6e56e06fd70f6e4507996b017238505e66608c 请参阅添加此警告的提交: https//github.com/svenfuchs/i18n/commit/3b6e56e06fd70f6e4507996b017238505e66608c


#3楼

Just for completeness, note that you can also get rid of the warning by setting I18n.enforce_available_locales to true (or false ) in config/application.rb : 为了完整I18n.enforce_available_locales ,请注意您也可以通过在config/application.rb I18n.enforce_available_locales设置为true (或false )来消除警告:

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

#4楼

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


#5楼

Important : Make sure your app is not using I18n 0.6.8, it has a bug that prevents the configuration to be set correctly . 重要提示 :确保您的应用未使用I18n 0.6.8,它有一个错误,无法正确设置配置


Short answer 简短的回答

In order to silence the warning edit the application.rb file and include the following line inside the Rails::Application body 为了使警告静音,编辑application.rb文件并在Rails::Application体中包含以下行

config.i18n.enforce_available_locales = true

The possible values are: 可能的值是:

  • false : if you 错误 :如果你
    • want to skip the locale validation 想跳过语言环境验证
    • don't care about locales 不关心语言环境
  • true : if you 是的 :如果你
    • want the application to raise an error if an invalid locale is passed (or) 希望应用程序在传递无效区域设置时引发错误(或)
    • want to default to the new Rails behaviors (or) 想默认为新的Rails行为(或)
    • care about locale validation 关心区域设置验证

Note: 注意:

  • The old default behavior corresponds to false , not true . 旧的默认行为对应于false ,而不是true
  • If you are setting the config.i18n.default_locale configuration or other i18n settings, make sure to do it after setting the config.i18n.enforce_available_locales setting. 如果要设置config.i18n.default_locale配置或其他i18n设置,请确保在设置config.i18n.enforce_available_locales设置后执行此操作。
  • If your use third party gems that include I18n features, setting the variable through the Application config object, may not have an effect. 如果您使用包含I18n功能的第三方gem,通过Application config对象设置变量可能没有效果。 In this case, set it directly to I18n using I18n.config.enforce_available_locales . 在这种情况下,使用I18n.config.enforce_available_locales将其直接设置为I18n

    Caveats 注意事项

Example

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

Long answer 答案很长

The deprecation warning is now displayed both in Rails 4 (>= 4.0.2) and Rails 3.2 (>= 3.2.14). 现在,在Rails 4(> = 4.0.2)和Rails 3.2(> = 3.2.14)中都会显示弃用警告。 The reason is explained in this commit . 原因在此提交中进行了解释。

Enforce available locales 实施可用的区域设置

When I18n.config.enforce_available_locales is true we'll raise an I18n::InvalidLocale exception if the passed locale is unavailable. I18n.config.enforce_available_locales为true时,如果传递的语言环境不可用,我们将引发I18n :: InvalidLocale异常。

The default is set to nil which will display a deprecation error. 默认设置为nil ,将显示弃用错误。

If set to false we'll skip enforcing available locales altogether (old behaviour). 如果设置为false我们将完全跳过强制执行可用的语言环境(旧行为)。

This has been implemented in the following methods : 这已通过以下方法实现:

  • I18n.config.default_locale= I18n.config.default_locale =
  • I18n.config.locale= I18n.config.locale =
  • I18n.translate I18n.translate
  • I18n.localize I18n.localize
  • I18n.transliterate I18n.transliterate

Before this change, if you passed an unsupported locale, Rails would silently switch to it if the locale is valid (ie if there is a corresponding locale file in the /config/locales folder), otherwise the locale would default to the config.i18n.default_locale configuration (which defaults to :en). 在此更改之前,如果您传递了不受支持的语言环境,如果语言环境有效(即如果/config/locales文件夹中有相应的语言/config/locales文件),Rails将静默切换到它,否则语言环境将默认为config.i18n.default_locale配置(默认为:en)。

The new version of the I18n gem, forces developers to be a little bit more conscious of the locale management. I18n gem的新版本迫使开发人员更加关注区域设置管理。

In the future, the behavior will change and if a locale is invalid, the Rails app will raise an error. 将来,行为将发生变化,如果区域设置无效,Rails应用程序将引发错误。

In preparation of such change (that may potentially break several applications that until today were relying on silent defaults), the warning is forcing you to explicitly declare which validation you want to perform, during the current transition period. 在准备此类更改时(可能会破坏直到今天依赖于静默默认值的多个应用程序),警告会强制您在当前过渡期间明确声明要执行的验证。

To restore the previous behavior, simply set the following configuration to false 要恢复以前的行为,只需将以下配置设置为false

config.i18n.enforce_available_locales = false

otherwise, set it to true to match the new Rails defaults or if you want to be more rigid on domain validation and avoid switching to the default in case of invalid locale. 否则,将其设置为true以匹配新的Rails默认值,或者如果您希望在域验证上更加严格,并避免在无效区域设置的情况下切换到默认值。

config.i18n.enforce_available_locales = true

Caveat 警告

  1. 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. 如果要设置config.i18n.default_locale配置或使用前面提到的任何方法( default_locale=locale=translate等),请确保在设置config.i18n.enforce_available_locales设置后执行此操作。 Otherwise, the deprecation warning will keep on popping up. 否则,弃用警告将继续弹出。 (Thanks Fábio Batista ). (谢谢FábioBatista )。

  2. If you use third party gems that include I18n features, setting the variable through may not have effect. 如果您使用包含I18n功能的第三方宝石,则设置变量可能无效。 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应用程序中设置配置时,该值不会立即分配给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. Rails将每个配置存储在内部对象中,加载依赖项(Railties和第三方gem),然后将配置传递给目标类。 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. 如果您在配置分配给I18n之前使用调用任何I18n方法的gem(或Rails插件),那么您将收到警告。

    In this case, you need to skip the Rails stack and set the config immediately to the I18n gem by calling 在这种情况下,您需要跳过Rails堆栈并通过调用立即将配置设置为I18n gem

     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. 尝试生成一个新的空Rails应用程序,您将看到在application.rb中设置config.i18n工作正常。

    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! 在系统中找到i18n gem,打开i18n.rb文件并编辑enforce_available_locales!方法enforce_available_locales! to include the statement puts caller.inspect . 包括语句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). 通过检查堆栈跟踪(在我的情况下,它是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)>'", 

#6楼

If you want to care about locales write into appilcation.rb file. 如果你想关注locales写入appilcation.rb文件。

config.i18n.enforce_available_locales = true

You can write false if locale validation and do not care about that. 如果区域设置验证,您可以写错,而不关心它。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值