在没有 Rails 的情况下,使用 ActiveModel 时出现了 I18n 废弃警告。

32 浏览
0 Comments

在没有 Rails 的情况下,使用 ActiveModel 时出现了 I18n 废弃警告。

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

[不推荐使用] I18n.enforce_available_locales 在将来将默认为 true。如果你真的想跳过你的区域设置的验证,你可以将 I18n.enforce_available_locales = false,以避免这个消息。

我在 类似的问题 中看到了解决方案,其中的解决方法是在我的 config/application.rb 文件中设置 config.i18n.enforce_available_locales

I18n.config.enforce_available_locales。我试过两种方法,但我仍然收到警告信息。

给我带来走后期警告的测试并不使用任何的 Rails,仅使用 ActiveModel。我没有引入默认的 spec_helper,而是创建了自己的 spec_helper,其中不涉及任何 Rails。我还尝试在我的自定义 spec_helper 中设置 enforce_available_locales,但我得到了未初始化的常量错误。

我怎样才能消除这个过时警告?

编辑:

这是我在 config/application.rb 中一个使用 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

admin 更改状态以发布 2023年5月24日
0
0 Comments

在我的spec_helper中加入以下这些行似乎能起作用:

require 'i18n'
I18n.config.enforce_available_locales = true

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

0
0 Comments

在Github中也有一个有关于的bug报告 (svenfuchs/i18n#223),并且据说在版本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

希望能有所帮助 🙂

0