在不使用 Rails 的情况下使用 ActiveModel 时的 I18n 弃用警告 [英] I18n deprecation warning when using ActiveModel without Rails
问题描述
当我在我的模型上运行 Rspec 时,我收到此警告:
When I run Rspec on my models, I get 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.
我看到了一个类似的问题,解决方案是设置config.i18n.enforce_available_locales
或I18n.config.enforce_available_locales
在我的 config/application.rb 文件中.我两个都试过了,但还是收到警告.
I saw a similar question where the solution was to set config.i18n.enforce_available_locales
or
I18n.config.enforce_available_locales
in my config/application.rb file. I tried both, but I still get the warning.
给出弃用警告的测试不使用除 ActiveModel 之外的任何 Rails.我不需要默认的 spec_helper,而是创建了自己的 spec_helper,它根本不涉及任何 Rails.我还尝试在我的自定义 spec_helper 中设置enforce_available_locales,但出现未初始化的常量错误.
The test that gives me the deprecation warning does not use any Rails except for ActiveModel. Instead of requiring the default spec_helper, I created my own spec_helper that does not involve any Rails at all. I also tried setting enforce_available_locales in my custom spec_helper, but I got an uninitialized constant error.
如何消除弃用警告?
这是我的 config/application.rb 中的确切代码,来自我对 enforce_available_locales 的一次尝试
Here's the exact code in my config/application.rb from one of my attempts with enforce_available_locales
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
推荐答案
似乎有效的是将这些行添加到我的 spec_helper 中:
What seems to work is adding these lines to my spec_helper:
require 'i18n'
I18n.config.enforce_available_locales = true
因为我的测试没有使用 Rails,所以 Application 类没有受到影响,所以enforce_available_locales 必须放在 spec_helper 本身中.第一行去掉了未初始化的常量错误.
Because my tests don't use Rails, the Application class goes untouched, so enforce_available_locales must go in the spec_helper itself. The first line gets rid of the uninitialized constant error.
这篇关于在不使用 Rails 的情况下使用 ActiveModel 时的 I18n 弃用警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!