重要提示:确保您的应用未使用 I18n 0.6.8,它有一个 bug that prevents the configuration to be set correctly。
简答
为了消除警告,请编辑 application.rb 文件并在 Rails::Application 正文中包含以下行
config.i18n.enforce_available_locales = true
可能的值是:
-
false:如果你
-
真:如果你
- 希望应用程序在传递(或)无效的语言环境时引发错误
- 想要默认使用新的 Rails 行为(或)
- 关心区域设置验证
注意:
- 旧的默认行为对应于
false,而不是true。
- 如果您正在设置
config.i18n.default_locale 配置或其他i18n 设置,请确保在设置config.i18n.enforce_available_locales 设置后进行。
- 如果您使用包含 I18n 功能的第三方 gem,通过 Application
config 对象设置变量可能没有效果。在这种情况下,使用I18n.config.enforce_available_locales 将其直接设置为I18n。
注意事项
示例
require File.expand_path('../boot', __FILE__)
# ...
module YouApplication
class Application < Rails::Application
# ...
config.i18n.enforce_available_locales = true
# or if one of your gem compete for pre-loading, use
I18n.config.enforce_available_locales = true
# ...
end
end
长答案
现在在 Rails 4 (>= 4.0.2) 和 Rails 3.2 (>= 3.2.14) 中都会显示弃用警告。原因在this commit中有解释。
强制使用可用的语言环境
当I18n.config.enforce_available_locales 为真时,我们将提出一个
如果传递的语言环境不可用,则 I18n::InvalidLocale 异常。
默认设置为nil,这将显示弃用错误。
如果设置为 false,我们将完全跳过强制可用的语言环境(旧行为)。
这已通过以下方法实现:
- I18n.config.default_locale=
- I18n.config.locale=
- I18n.translate
- I18n.localize
- I18n.transliterate
在此更改之前,如果您传递了一个不受支持的语言环境,如果该语言环境有效(即如果在 /config/locales 文件夹中存在相应的语言环境文件),Rails 将静默切换到该语言环境,否则该语言环境将默认为 @ 987654339@ 配置(默认为 :en)。
新版本的 I18n gem 迫使开发人员更加注意语言环境管理。
将来,行为会发生变化,如果语言环境无效,Rails 应用程序将引发错误。
在准备此类更改(这可能会破坏直到今天还依赖静默默认设置的多个应用程序)时,警告会强制您在当前过渡期间明确声明要执行的验证。
要恢复以前的行为,只需将以下配置设置为false
config.i18n.enforce_available_locales = false
否则,将其设置为 true 以匹配新的 Rails 默认值,或者如果您希望在域验证上更加严格,并避免在区域设置无效的情况下切换到默认值。
config.i18n.enforce_available_locales = true
警告
如果您正在设置 config.i18n.default_locale 配置或使用前面提到的任何方法(default_locale=、locale=、translate 等),请确保在设置 config.i18n.enforce_available_locales环境。否则,弃用警告将不断弹出。 (感谢Fábio Batista)。
-
如果您使用包含 I18n 功能的第三方 gem,则通过设置变量可能无效。实际上,问题与上一点描述的相同,只是调试起来有点困难。
这个问题是优先事项。当您在 Rails 应用程序中设置配置时,该值不会立即分配给 I18n gem。 Rails 将每个配置存储在一个内部对象中,加载依赖项(Railties 和第三方 gem),然后将配置传递给目标类。如果您在将配置分配给 I18n 之前使用调用任何 I18n 方法的 gem(或 Rails 插件),那么您将收到警告。
在这种情况下,您需要跳过 Rails 堆栈并通过调用立即将配置设置为 I18n gem
I18n.config.enforce_available_locales = true
而不是
config.i18n.enforce_available_locales = true
这个问题很容易证明。尝试生成一个新的空 Rails 应用程序,您会发现在 application.rb 中设置 config.i18n 可以正常工作。
如果在您的应用中没有,有一种简单的方法可以调试罪魁祸首。在系统中找到 i18n gem,打开 i18n.rb 文件并编辑方法 enforce_available_locales! 以包含语句 puts caller.inspect。
这将导致该方法在调用时打印堆栈跟踪。您将能够通过检查堆栈跟踪(在我的例子中是 Authlogic)来确定哪个 gem 正在调用它。
["/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/i18n-0.6.9/lib/i18n.rb:150:in `translate'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/i18n/translator.rb:8:in `translate'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/i18n.rb:79:in `translate'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:68:in `validates_format_of_email_field_options'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:102:in `block in included'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:99:in `class_eval'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/email.rb:99:in `included'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `include'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `block in acts_as_authentic'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `each'",
"/Users/weppos/.rvm/gems/ruby-2.0.0-p247@application/gems/authlogic-3.1.0/lib/authlogic/acts_as_authentic/base.rb:37:in `acts_as_authentic'",
"/Users/weppos/Projects/application/app/models/user.rb:8:in `<class:User>'",
"/Users/weppos/Projects/application/app/models/user.rb:1:in `<top (required)>'",