首页 文章

在没有Rails的情况下使用ActiveModel时的I18n弃用警告

提问于
浏览
1

当我在我的模型上运行Rspec时,我收到此警告:

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

我看到a similar question解决办法是在我的config / application.rb文件中设置 config.i18n.enforce_available_localesI18n.config.enforce_available_locales . 我试过两个,但我仍然得到警告 .

给我弃用警告的测试不使用除ActiveModel之外的任何Rails . 我没有要求默认的spec_helper,而是创建了自己的spec_helper,它根本不涉及任何Rails . 我也尝试在我的自定义spec_helper中设置enforce_available_locales,但是我得到了一个未初始化的常量错误 .

How do I get rid of the deprecation warning?

Edit: 这是我的一次使用enforce_available_locales尝试的config / application.rb中的确切代码

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module Microblog
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
    I18n.config.enforce_available_locales = true
  end
end

2 回答

  • 2

    在Github中 i18n 上也报告了一个关于此事件的错误(svenfuchs/i18n#223),并且据说在 i18n gem版本 0.6.9 中修复了该错误 .

    所以我认为解决方案是在我们的Gemfile中要求'> = 0.6.9' .

    gem 'i18n', '>= 0.6.9'
    

    并做一个 bundle update .

    然后执行以下操作:

    config/application.rb

    I18n.enforce_available_locales = true
    
    # If you set the default_locale option you should do it after the previous line
    # config.i18n.default_locale = :de
    

    参考:https://github.com/rails/rails/issues/13159

    希望能帮助到你 :)

  • 1

    似乎有用的是将这些行添加到我的spec_helper:

    require 'i18n'
    I18n.config.enforce_available_locales = true
    

    因为我的测试不使用Rails,所以Application类不受影响,因此enforce_available_locales必须放在spec_helper本身 . 第一行消除了未初始化的常量错误 .

相关问题