Rails I18n 验证弃用警告 [英] Rails I18n validation deprecation warning

查看:30
本文介绍了Rails I18n 验证弃用警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚更新到 rails 4.0.2 并且收到此警告:

I just updated to rails 4.0.2 and I'm getting this warning:

[已弃用] I18n.enforce_available_locales 将来会默认为 true.如果您真的想跳过对您的语言环境的验证,您可以设置 I18n.enforce_available_locales = false 以避免出现此消息.

[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.

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

Is there any security issue in setting it to false?

推荐答案

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

Important: Make sure your app is not using I18n 0.6.8, it has a bug that prevents the configuration to be set correctly.

为了消除警告,请编辑 application.rb 文件并在 Rails::Application 正文中包含以下行

In order to silence the warning edit the application.rb file and include the following line inside the Rails::Application body

config.i18n.enforce_available_locales = true

可能的值是:

  • :如果你
    • 想跳过区域设置验证
    • 不在乎语言环境
    • 希望应用程序在传递(或)无效区域设置时引发错误
    • 想要默认使用新的 Rails 行为(或)
    • 关心语言环境验证

    注意:

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

      注意事项

    • The old default behavior corresponds to false, not 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.
    • If your use third party gems that include I18n features, setting the variable through the Application config object, may not have an effect. In this case, set it directly to I18n using I18n.config.enforce_available_locales.

      Caveats

    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) 中.原因在此提交中解释.

    强制执行可用的语言环境

    Enforce available locales

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

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

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

    The default is set to nil which will display a deprecation error.

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

    If set to false we'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

    在此更改之前,如果您传递了不受支持的语言环境,并且该语言环境有效(即,如果 /config/locales 文件夹中有相应的语言环境文件),Rails 将自动切换到该语言环境,否则语言环境将默认为 config.i18n.default_locale 配置(默认为 :en).

    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/locales folder), otherwise the locale would default to the config.i18n.default_locale configuration (which defaults to :en).

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

    The new version of the I18n gem, forces developers to be a little bit more conscious of the locale management.

    将来,行为将发生变化,如果区域设置无效,Rails 应用程序将引发错误.

    In the future, the behavior will change and if a locale is invalid, the Rails app will raise an error.

    为准备此类更改(这可能会破坏几个直到今天都依赖静默默认值的应用程序),警告迫使您在当前过渡期内明确声明要执行的验证.

    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.

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

    To restore the previous behavior, simply set the following configuration to false

    config.i18n.enforce_available_locales = false
    

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

    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.

    config.i18n.enforce_available_locales = true
    

    警告

    1. 如果您正在设置 config.i18n.default_locale 配置或使用前面提到的任何方法(default_locale=locale=code>、translate 等),确保在设置 config.i18n.enforce_available_locales 设置后执行.否则,弃用警告将不断弹出.(感谢 Fábio Batista).

    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. Otherwise, the deprecation warning will keep on popping up. (Thanks Fábio Batista).

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

    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.

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

    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.

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

    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
    

    代替

    config.i18n.enforce_available_locales = true
    

    这个问题很容易证明.尝试生成一个新的空 Rails 应用程序,您将看到 application.rb 中的设置 config.i18n 工作正常.

    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.

    如果在您的应用中没有,有一种简单的方法可以调试罪魁祸首.在您的系统中找到 i18n gem,打开 i18n.rb 文件并编辑方法 enforce_available_locales! 以包含语句 puts caller.inspect.

    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.

    这将导致该方法在调用时打印堆栈跟踪.您将能够通过检查堆栈跟踪(在我的情况下是 Authlogic)来确定哪个 gem 正在调用它.

    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)>'",
    

  • 这篇关于Rails I18n 验证弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆