2014-05-01 102 views
1

當我在我的模型運行Rspec的,我得到這樣的警告:的I18n棄用警告

(已廢棄)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,但我得到了未初始化的常量錯誤。

我該如何擺脫棄用警告?

編輯: 下面是我的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 
+0

你在'application.rb'中寫了什麼確切的代碼?因爲這確實是一個正確的解決方案。這可能會被標記爲重複。 – sevenseacat

+0

你的寶石'i18n'版本是什麼? –

+0

@RajeshCO i18n-0.6.9 – Eva

回答

1

什麼似乎工作就是將這些線到我的spec_helper:

require 'i18n' 
I18n.config.enforce_available_locales = true 

因爲我的測試不使用Rails,應用程序類不受影響,所以enforce_available_locales必須放在spec_helper本身中。第一行擺脫了未初始化的常量錯誤。

2

有也對此(svenfuchs/i18n#223)在Github上報告i18n一個錯誤,據說它被修復在i18n寶石版本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

我已經使用0.6.9版本。 – Eva

+0

然後僅對config/application.rb進行更改。它在我的機器中進行了上述更新。我的應用程序也使用i18n 0.6.9。對你起作用嗎? –

+0

它沒有工作。 – Eva