Ruby-on-rails Rails I18n 验证弃用警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20361428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:05:51  来源:igfitidea点击:

Rails I18n validation deprecation warning

ruby-on-railsrubyruby-on-rails-4deprecatedrails-i18n

提问by Mauricio Moraes

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. 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 将来将默认为 true。如果您真的想跳过对您的语言环境的验证,您可以设置 I18n.enforce_available_locales = false 以避免出现此消息。

Is there any security issue in setting it to false?

将其设置为 false 是否有任何安全问题?

回答by Simone Carletti

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::Applicationbody

为了消除警告编辑 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)
    • care about locale validation
  • :如果你
    • 想跳过区域设置验证
    • 不在乎语言环境
  • 真的:如果你
    • 如果传递了无效的语言环境,希望应用程序引发错误(或)
    • 想要默认为新的 Rails 行为(或)
    • 关心区域设置验证

Note:

笔记:

  • The old default behavior corresponds to false, not true.
  • If you are setting the config.i18n.default_localeconfiguration or other i18n settings, make sure to do it after setting the config.i18n.enforce_available_localessetting.
  • If your use third party gems that include I18n features, setting the variable through the Application configobject, may not have an effect. In this case, set it directly to I18nusing I18n.config.enforce_available_locales.

    Caveats

  • 旧的默认行为对应于false,而不是true
  • 如果您正在设置config.i18n.default_locale配置或其他 i18n 设置,请确保在设置config.i18n.enforce_available_locales设置后进行。
  • 如果您使用包含 I18n 功能的第三方 gem,则通过 Applicationconfig对象设置变量可能没有效果。在这种情况下,直接将其设置为I18nusing I18n.config.enforce_available_locales

    注意事项

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). The reason is explained in this commit.

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

Enforce available locales

When I18n.config.enforce_available_localesis true we'll raise an I18n::InvalidLocale exception if the passed locale is unavailable.

The default is set to nilwhich will display a deprecation error.

If set to falsewe'll skip enforcing available locales altogether (old behaviour).

This has been implemented in the following methods :

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

强制执行可用的语言环境

I18n.config.enforce_available_locales为 true 时,如果传递的语言环境不可用,我们将引发 I18n::InvalidLocale 异常。

默认设置为nil将显示弃用错误。

如果设置为false我们将完全跳过强制执行可用的语言环境(旧行为)。

这已在以下方法中实现:

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

Before this change, if you passed an unsupported locale, Rails would silently switch to it if the locale is valid (i.e. if there is a corresponding locale file in the /config/localesfolder), otherwise the locale would default to the config.i18n.default_localeconfiguration (which defaults to :en).

在此更改之前,如果您传递了不受支持的语言环境,并且该语言环境有效(即文件/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_localeconfiguration 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_localessetting. Otherwise, the deprecation warning will keep on popping up. (Thanks Fábio Batista).

  2. 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.i18nin the application.rbworks 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.rbfile 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)>'",
    
  1. 如果要设置的config.i18n.default_locale配置或使用任何前面提到的方法(default_locale=locale=translate等),确保设置后做config.i18n.enforce_available_locales设置。否则,弃用警告将不断弹出。(感谢法比奥巴蒂斯塔)。

  2. 如果您使用包含 I18n 功能的第三方 gem,则设置变量 through 可能无效。事实上,这个问题和上一点描述的一样,只是调试起来有点困难。

    这个问题是一个优先事项。当您在 Rails 应用程序中设置配置时,该值不会立即分配给 I18n gem。Rails 将每个配置存储在一个内部对象中,加载依赖项(Railties 和第三方 gem),然后将配置传递给目标类。如果您使用 gem(或 Rails 插件)在将配置分配给 I18n 之前调用任何 I18n 方法,那么您将收到警告。

    在这种情况下,您需要跳过 Rails 堆栈并通过调用立即将配置设置为 I18n gem

    I18n.config.enforce_available_locales = true
    

    代替

    config.i18n.enforce_available_locales = true
    

    这个问题很容易证明。尝试生成一个新的空 Rails 应用程序,您将config.i18napplication.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)>'",
    

回答by mhartl

Just for completeness, note that you can also get rid of the warning by setting I18n.enforce_available_localesto true(or false) in config/application.rb:

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

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

回答by SpeedyWizard

I18n.config.enforce_available_locales = trueworked for me in Rails 3.2.16 (I put it in config/application.rb)

I18n.config.enforce_available_locales = true在 Rails 3.2.16 中对我来说有效(我把它放在 config/application.rb 中)

回答by Justin

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

回答by Foram Thakral

If you want to care about locales write into appilcation.rbfile.

如果您想关心语言环境,请写入appilcation.rb文件。

config.i18n.enforce_available_locales = true

You can write false if locale validation and do not care about that.

如果区域设置验证,您可以写 false 并且不关心它。