title: Rails I18n: setting initial locale from HTTP_ACCEPT_LANGUAGE
body: <p>Thanks everyone for your help.</p>
  
  <p><strong>WHAT I AM DOING:</strong>
  I'm designing a corporate website which must be available in multiple languages</p>
  
  <p><strong>WHAT I HAVE DONE:</strong>
  A rails application with several static pages. I used Ryan Bates example on using I18n features in rails, in particular using routes to set the locale:</p>
  
  <pre><code>MyApp::Application.routes.draw do
  
    scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
      root to: 'static_pages#home'
  
      match '/about', to: 'static_pages#about'
      match '/contact', to: 'static_pages#contact'
    end
  
    match '*path', to: redirect("/#{I18n.locale}/%{path}")  
    match '', to: redirect("/#{I18n.locale}")
  </code></pre>
  
  <p>My ApplicationController looks as follows:</p>
  
  <pre><code>class ApplicationController &lt; ActionController::Base
    protect_from_forgery
    before_filter :set_locale
  
    private
      def set_locale      
          I18n.locale = params[:locale] || I18n.default_locale
      end    
  
      def default_url_options(options = {})
          {locale: I18n.locale}
      end       
  end
  </code></pre>
  
  <p>My log at server start displays the following:</p>
  
  <pre><code>Started GET "/en" for 127.0.0.1 at 2012-11-14 22:41:01 +0400
  Processing by StaticPagesController#home as HTML
    Parameters: {"locale"=&gt;"en"}
    Rendered static_pages/home.en.html.erb within layouts/application (1.0ms)
  Compiled custom.css  (5905ms)  (pid 9864)
  Compiled application.css  (15ms)  (pid 9864)
    Rendered layouts/_shim.html.erb (0.0ms)
    Rendered layouts/_header.html.erb (12.0ms)
    Rendered layouts/_footer.html.erb (2.0ms)
  Completed 200 OK in 6741ms (Views: 6740.4ms | ActiveRecord: 0.0ms)
  </code></pre>
  
  <p><strong>WHAT I AM TRYING TO DO:</strong>
  I would like that when a user does not specify the locale in the url (e.g. types only www.example.com), rails get the accepted locales from <code>request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first</code>
  and redirects the visitor to his preferred language. At this point I can't figure out where my I18n.locale is set in the first place.</p>