Not setting default_locale in Rails 3.1.3
application.rb:
config.i18n.default_locale = :en
I have got 3 locales (en,es and ca) And when i go to my console:
I18n.locale => :ca # I guess the first one in the array
I am using Gettext. Somebody got a clue what it could be?
Bundle:
Using rake (0.9.2.2)
Using Ascii85 (1.0.1)
Using ZenTest (4.5.0)
Using multi_json (1.0.4)
Using activesupport (3.1.3)
Using builder (3.0.0)
Using i18n (0.6.0)
Using activemodel (3.1.3)
Using erubis (2.7.0)
Using rack (1.3.6)
Using rack-cache (1.1)
Using rack-mount (0.8.3)
Using rack-test (0.6.1)
Using hike (1.2.1)
Using tilt (1.3.3)
Using sprockets (2.0.3)
Using actionpack (3.1.3)
Using mime-types (1.17.2)
Using polyglot (0.3.3)
Using treetop (1.4.10)
Using mail (2.3.0)
Using actionmailer (3.1.3)
Using arel (2.2.1)
Using tzinfo (0.3.31)
Using activerecord (3.1.3)
Using activeresource (3.1.3)
Using addressable (2.2.6)
Using adyen (1.3.0)
Using airbrake (3.0.5)
Using ansi (1.3.0)
Using autotest (4.4.6)
Using xml-simple (1.1.1)
Using aws-ses (0.4.4)
Using bcrypt-ruby (3.0.1)
Using bluecloth (2.2.0)
Using bulksms (0.5.2)
Using bundler (1.0.21)
Using highline (1.6.2)
Using net-ssh (2.2.1)
Using net-scp (1.0.4)
Using net-sftp (2.0.5)
Using net-ssh-gateway (1.1.0)
Using capistrano (2.9.0)
Using capistrano-ext (1.2.1)
Using nokogiri (1.5.0)
Using ffi (1.0.9)
Using childprocess (0.2.2)
Using json_pure (1.6.1)
Using rubyzip (0.9.4)
Using selenium-webdriver (2.8.0)
Using xpath (0.1.4)
Using capybara (1.1.1)
Using carrierwave (0.5.8)
Using chunky_png (1.2.5)
Using coffee-script-source (1.1.2)
Using execjs (1.2.9)
Using coffee-script (2.2.0)
Using fssm (0.2.7)
Using sass (3.1.10)
Using compass (0.12.alpha.0)
Using compass-960-plugin (0.10.4)
Using rest-client (1.6.7)
Using couchrest (1.1.2)
Using couchrest_model (1.1.2)
Using daemons (1.0.10)
Using diff-lcs (1.1.3)
Using e164 (0.3.1)
Using excon (0.7.12)
Using factory_girl (2.3.0)
Using rack-ssl (1.3.2)
Using json (1.6.5)
Using rdoc (3.12)
Using thor (0.14.6)
Using railties (3.1.3)
Using factory_girl_rails (1.4.0)
Using multipart-post (1.1.3)
Using faraday (0.7.5)
Using faraday_middleware (0.7.0)
Using fast_gettext (0.6.2)
Using formatador (0.2.1)
Using ruby-hmac (0.4.0)
Using fog (1.1.1)
Using rails (3.1.3)
Using formtastic (2.0.0.rc5)
Using gem_plugin (0.2.3)
Using geocoder (1.1.0)
Using geokit (1.6.0)
Using locale (2.0.5)
Using gettext (2.1.0)
Using gettext_i18n_rails (0.3.0)
Using gravatar (1.0)
Using guard (1.0.0)
Using guard-rspec (0.6.0)
Using haml (3.1.4)
Using haml-rails (0.3.4)
Using hashie (1.1.0)
Using jquery-rails (1.0.17)
Using kaminari (0.12.4)
Using kgio (2.6.0)
Using launchy (2.0.5)
Using libv8 (3.3.10.2)
Using metaclass (0.0.1)
Using subexec (0.1.0)
Using mini_magick (3.3)
Using mocha (0.10.0)
Using money (4.0.1)
Using mongrel (1.2.0.pre2)
Using multi_xml (0.4.1)
Using nifty-generators (0.4.6)
Using ruby-rc4 (0.1.3)
Using pdf-reader (1.0.0)
Using ttfunk (1.0.3)
Using prawn (0.12.0)
Using rack-protection (1.1.4)
Using raindrops (0.8.0)
Using redis (2.2.2)
Using redis-namespace (1.0.3)
Using sinatra (1.3.1)
Using vegas (0.1.8)
Using resque (1.19.0)
Using resque_mailer (2.0.2)
Using rspec-core (2.8.0)
Using rspec-expectations (2.8.0)
Using rspec-mocks (2.8.0)
Using rspec (2.8.0)
Using rspec-rails (2.8.1)
Using ruby-ole (1.2.11.2)
Using sexp_processor (3.0.7)
Using ruby_parser (2.3.1)
Using sass-rails (3.1.5)
Using simple_oauth (0.1.5)
Using spreadsheet (0.6.5.9)
Using state_machine (1.0.2)
Using steak (2.0.0)
Using therubyracer (0.9.8)
Using turn (0.8.3)
Using twitter (1.7.2)
Using uglifier (1.0.3)
Using unicorn (4.1.1)
Using useragent (0.4.6)
Did you uncomment the following line ?
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
Rails will take translated text from this location only. But this is for Rails specific translation gem. If you want to set default locale for gettext you need to set as below
Add it to environment.rb
I18n.supported_locales = Dir[File.join(RAILS_ROOT, "locale/*")].collect{|v| File.basename(v)}
Test in a rails console
::I18n.locale = 'en'
You can verify this by the following gettext method
GetText.locale.to_s
Basically I set languages by setting I18n.locale = 'en'
or any other locale. You can set the default language based on browser preferred language also. You can also force the language to set locale in (gettext) by passing lang='ca', etc. All you need is to use :before_filter
. Call the below method in that filter
def set_default_language
languages = get_browser_preferred_languages_hash(request.env["HTTP_ACCEPT_LANGUAGE"])
@locale = languages.blank? ? 'en' : app_preferred_language(languages, params[:lang])[0..1]
I18n.locale = @locale || 'en'
end
def get_browser_preferred_languages_hash(languages)
# no language accepted
return [] if languages.blank?
# parse Accept-Language
accepted = languages.split(",")
accepted = accepted.map { |l| l.strip[0..1]}
end
def app_preferred_language(languages,forced_lang)
unless forced_lang.blank?
return forced_lang.class == Array ? forced_lang[0] : forced_lang
end
return 'en' if languages.blank?
@matched_lang = ''
languages.each do |lang|
@matched_lang = support_languages_hash.values.detect{|ele| ele[0..1] == lang}
break if !@matched_lang.blank?
end
return @matched_lang.blank? ? 'en' : @matched_lang
end
def support_languages_hash
GetText.locale
{ _('English') => 'en-US',
_('Spanish') => 'es-419',
_('Catalan') => 'ca' }
end