Search code examples
ruby-on-railsruby-on-rails-4rails-i18n

How to setup locales for Rails 4


i have a rather specific setup regarding rails internationalization. I'm using rails-i18n gem, but that shouldn't matter. It worked perfectly with Rails 3. Here is my config from config/application.rb

config.i18n.default_locale = :en
config.i18n.locale = :hr

Let me explain:

  • locale is set to :hr (Croatian) because I mostly make localized applications in Croatian language
  • default locale is set to :en because I'm often using gems like rails-admin which have English translations included. It plays nicely in production where missing (Croatian) translations fallback to English. That's fine, all admins understand English :)

And the question is: how to make it work with Rails 4?

It seems that Rails 4 ignores config.i18n.locale, and it always use :en locale.

So far, I've been using before_action to set I18n.locale = :hr but that doesn't work in Rails console or Rack middleware...

Thanks in advance,

Danijel


Solution

  • I found a simple solution and I'm posting it here...

    Insert into config/application.rb

      config.i18n.default_locale = :hr
      config.i18n.available_locales = [:hr, :en]
      config.i18n.fallbacks = [:en]
    

    Remove or comment the following line from config/environments/production.rb

      # config.i18n.fallbacks = true
    

    or change it to:

      config.i18n.fallbacks = [:en]