简体   繁体   中英

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

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]

You can add the code below:

class ApplicationController
    ...

    before_filter :set_locale

    ...

    private

    def set_locale
        I18n.locale = :hr
    end 

end 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM