Question

When I run Rspec on my models, I get this warning:

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

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.

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.

How do I get rid of the deprecation warning?

Edit: 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
Was it helpful?

Solution 2

What seems to work is adding these lines to my spec_helper:

require 'i18n'
I18n.config.enforce_available_locales = true

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.

OTHER TIPS

There's also a bug reported on i18n in Github regarding this (svenfuchs/i18n#223) and it was said to be fixed in i18n gem version 0.6.9.

So the solution I think is to require '>= 0.6.9' in our Gemfile.

gem 'i18n', '>= 0.6.9'

and do a bundle update.

Then do as below:

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

Ref: https://github.com/rails/rails/issues/13159

Hope it helps :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top