I just updated to rails 4.0.2 and I'm getting 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.
Is there any security issue in setting it to false?
Just for completeness, note that you can also get rid of the warning by setting
I18n.enforce_available_locales
totrue
(orfalse
) inconfig/application.rb
:If you want to care about locales write into
appilcation.rb
file.You can write false if locale validation and do not care about that.
Doesn't seem that way - that'd be previous behavior of the way i18n works - new behavior (true) will raise an error when you ask for a locale not implemented/available.
See the commit that added this warning: https://github.com/svenfuchs/i18n/commit/3b6e56e06fd70f6e4507996b017238505e66608c
Important: Make sure your app is not using I18n 0.6.8, it has a bug that prevents the configuration to be set correctly.
Short answer
In order to silence the warning edit the application.rb file and include the following line inside the
Rails::Application
bodyThe possible values are:
Note:
false
, nottrue
.config.i18n.default_locale
configuration or other i18n settings, make sure to do it after setting theconfig.i18n.enforce_available_locales
setting.config
object, may not have an effect. In this case, set it directly toI18n
usingI18n.config.enforce_available_locales
.Caveats
Example
Long answer
The deprecation warning is now displayed both in Rails 4 (>= 4.0.2) and Rails 3.2 (>= 3.2.14). The reason is explained in this commit.
Before this change, if you passed an unsupported locale, Rails would silently switch to it if the locale is valid (i.e. if there is a corresponding locale file in the
/config/locales
folder), otherwise the locale would default to theconfig.i18n.default_locale
configuration (which defaults to :en).The new version of the I18n gem, forces developers to be a little bit more conscious of the locale management.
In the future, the behavior will change and if a locale is invalid, the Rails app will raise an error.
In preparation of such change (that may potentially break several applications that until today were relying on silent defaults), the warning is forcing you to explicitly declare which validation you want to perform, during the current transition period.
To restore the previous behavior, simply set the following configuration to
false
otherwise, set it to true to match the new Rails defaults or if you want to be more rigid on domain validation and avoid switching to the default in case of invalid locale.
Caveat
If you are setting the
config.i18n.default_locale
configuration or using any of the previously mentioned methods (default_locale=
,locale=
,translate
, etc), make sure to do it after setting theconfig.i18n.enforce_available_locales
setting. Otherwise, the deprecation warning will keep on popping up. (Thanks Fábio Batista).If you use third party gems that include I18n features, setting the variable through may not have effect. In fact, the issue is the same as described in the previous point, just a little bit harder to debug.
This issue is a matter of precedence. When you set the config in your Rails app, the value is not immediately assigned to the I18n gem. Rails stores each config in an internal object, loads the dependencies (Railties and third party gems) and then it passes the configuration to the target classes. If you use a gem (or Rails plugin) that calls any of the I18n methods before the config is assigned to I18n, then you'll get the warning.
In this case, you need to skip the Rails stack and set the config immediately to the I18n gem by calling
instead of
The issue is easy to prove. Try to generate a new empty Rails app and you will see that setting
config.i18n
in theapplication.rb
works fine.If in your app it does not, there is an easy way to debug the culprit. Locate the i18n gem in your system, open the
i18n.rb
file and edit the methodenforce_available_locales!
to include the statementputs caller.inspect
.This will cause the method to print the stacktrace whenever invoked. You will be able to determine which gem is calling it by inspecting the stacktrace (in my case it was Authlogic).
I18n.config.enforce_available_locales = true
worked for me in Rails 3.2.16 (I put it in config/application.rb)