Manos Manos - 3 years ago 179
Ruby Question

I18n configuration under config/application.rb is ignored

I am trying to setup Internationalization in my app.
I have setup

config/application.rb
as follows:

class Application < Rails::Application

I18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
I18n.enforce_available_locales = false

I18n.available_locales = [:en, :fr]

I18n.default_locale = :fr
end


In my
application_controller.rb
I am trying to setup the locale based on a subdomain:

before_action :set_locale

def set_locale
puts(I18n.default_locale)
I18n.locale = extract_locale_from_subdomain || I18n.default_locale
end

def extract_locale_from_subdomain
parsed_locale = request.subdomains.first
I18n.available_locales.map(&:to_s).include?(parsed_locale) ? parsed_locale : nil
puts(parsed_locale)
end


The first
puts
prints
en
and the second one correctly prints the subdomain, eg
gr
,
fr
or whatever I set.

It seems that my configuration in
config/application.rb
is ignored.

I have added
127.0.0.1 fr.app.local
into my
etc/hosts
so I can test it.

Answer Source

You need to edit the config object in your config/application.rb file.

I18n.default_locale = :fr won't give you what you want. You need to do config.i18n.default_locale = :fr

Try this:

class Application < Rails::Application
  config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
  config.i18n.enforce_available_locales = false
  config.i18n.available_locales = [:en, :fr]
  config.i18n.default_locale = :fr
end

You can learn more about configuring Rails components here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download