首页 文章

Rails I18n验证弃用警告

提问于
浏览
384

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

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

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

4 回答

  • 612

    Important :确保您的应用未使用I18n 0.6.8,它有bug that prevents the configuration to be set correctly .


    简答

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

    config.i18n.enforce_available_locales = true
    

    可能的值是:

    • false :如果你

    • 想要跳过语言环境验证

    • 不关心语言环境

    • true :如果你

    • 希望应用程序在传递无效区域设置时引发错误(或)

    • 想要默认为新的Rails行为(或)

    • 关心区域设置验证

    注意:

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

    • 如果要设置 config.i18n.default_locale 配置或其他i18n设置,请确保在设置 config.i18n.enforce_available_locales 设置后执行此操作 .

    • 如果您使用包含I18n功能的第三方宝石,通过Application config 对象设置变量可能没有效果 . 在这种情况下,使用 I18n.config.enforce_available_locales 将其直接设置为 I18n .

    警告

    示例

    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)中都会显示弃用警告 . 原因在this commit中解释 .

    强制实施可用语言环境当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 配置(默认为:恩) .

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

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

    在准备此类更改(可能会破坏直到今天依赖于静默默认值的多个应用程序)时,警告会强制您在当前过渡期间明确声明要执行的验证 .

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

    config.i18n.enforce_available_locales = false
    

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

    config.i18n.enforce_available_locales = true
    

    警告

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

    • 如果您使用包含I18n功能的第三方宝石,则设置变量可能无效 . 实际上,问题与前一点中描述的相同,只是稍微难以调试 .

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

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

    I18n.config.enforce_available_locales = true
    

    代替

    config.i18n.enforce_available_locales = true
    

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

    如果你的应用程序没有,有一个简单的方法来调试罪魁祸首 . 在系统中找到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)>'",
    
  • 15

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

  • 45

    为了完整起见,请注意您也可以通过在 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
    
  • 10

    似乎没那样 - 这是i18n工作方式的先前行为 - 当您要求未实现/可用的区域设置时,新行为(true)将引发错误 .

    请参阅添加此警告的提交:https://github.com/svenfuchs/i18n/commit/3b6e56e06fd70f6e4507996b017238505e66608c

相关问题