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

标签 ruby-on-rails ruby rspec activemodel rails-i18n

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

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

我看到了a similar question解决方案是设置 config.i18n.enforce_available_localesI18n.config.enforce_available_locales 在我的 config/application.rb 文件中。我都试过了,但仍然收到警告。

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

如何消除弃用警告?

编辑: 这是我的 config/application.rb 中的确切代码,来 self 对 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

最佳答案

在 Github 的 i18n 上也报告了一个关于这个的错误 (svenfuchs/i18n#223),据说它在 i18n gem 版本 0.6.9 中被修复

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

gem 'i18n', '>= 0.6.9'

并进行包更新

然后做如下操作:

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

希望对您有所帮助:)

关于ruby-on-rails - 在没有 Rails 的情况下使用 ActiveModel 时的 I18n 弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23401281/

相关文章:

ruby-on-rails - 如何使用 RSpec 测试 WebSockets(使用 Pusher)?

ruby-on-rails - 从主机访问虚拟机上的rails服务器

Rails 中的 SQL 查询生成器

ruby-on-rails - Rails 3 - 如何组织/拆分臃肿的 Controller ?

css - 将鼠标悬停在导航链接上时,Zurb Foundation Rails Topbar Nav 黑色突出显示

ruby - 如何让 Bundler 识别间接依赖的位置声明?

ruby-on-rails - Rails 应用程序的共享文件存储

Ruby 中的 HTML 解析器到 DOM

ruby-on-rails - 在 Rails 集成测试中 stub 第 3 方 API 调用

ruby-on-rails - 测试方法是否返回 `non-null` 值