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

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

问题描述

我刚刚更新到Rails 4.0.2,并收到以下警告:

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


[不建议使用] I18n.enforce_available_locales默认为将来如此。如果您确实想跳过对语言环境的验证,则可以设置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.

是否存在安全性

推荐答案

重要:请确保您的应用未使用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.

为了使警告静音,请编辑应用程序。 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

可能的值为:


  • false :如果您


    • 想跳过区域设置验证

    • 不在乎语言环境

    • false: if you
      • want to skip the locale validation
      • don't care about locales

      • 希望应用程序在传递无效语言环境时引发错误(或)

      • 希望默认使用新的Rails行为(或)

      • 关心语言环境验证

      注意:


      • 旧的默认行为对应于 false ,而不是 true

      • 如果要设置 config.i18n.default_locale 配置或其他i18n设置,请确保在设置完 config.i18n.enforce_available_locales 设置。

      • 如果您使用包含I18n功能的第三方gem,请通过应用程序设置变量 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)。 此提交中对此原因进行了说明。

      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.


      强制使用可用语言环境

      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

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

      之前此更改,如果您通过了不受支持的语言环境,Rails将会如果区域设置有效,则静默切换到该位置(即如果 / config / locales 文件夹中有相应的语言环境文件),则该语言环境将默认为 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
      



      注意事项



      Caveat


      1. 如果要设置 config.i18n.default_locale 配置或使用前面提到的任何方法( default_locale = locale = 吨等),请确保在设置 config.i18n.enforce_available_locales 设置后执行此操作。否则,弃用警告将继续弹出。 (感谢FábioBatista )。

      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功能的第三方宝石,则通过设置变量可能无效。实际上,问题与上一点中描述的相同,只是调试起来有点困难。

      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),然后将配置传递给目标类。如果在配置分配给I18n之前使用gem(或Rails插件)调用任何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堆栈,并通过调用

      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!来包含以下语句放入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.

      这将导致该方法在调用时打印堆栈跟踪。您可以通过检查stacktrace(在我的情况下是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天全站免登陆