Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Thiago Arrais http;//thiagoarrais.blogspot.com

« Newer Snippets
Older Snippets »
1 total  XML / RSS feed 

Setting Globalize locale based on the Accept-Language HTTP Header Field

This code sets the Globalize locale based on the http accept-language header field. Other options are also considered, in order or precedence:

- Explicetely locale set on URL
- Previous language selection (stored on user session)
- Accept-Language field contents
- Default locale

  before_filter :set_locale
  
  def set_locale
    default_locale = 'en-US'
    request_language = request.env['HTTP_ACCEPT_LANGUAGE']
    request_language = request_language.nil? ? nil : request_language[/[^,;]+/]
    
    @locale = params[:locale] || session[:locale] ||
              request_language || default_locale
    session[:locale] = @locale
    begin
      Locale.set @locale
    rescue
      @locale = default_locale
      Locale.set @locale
    end
    
  end
« Newer Snippets
Older Snippets »
1 total  XML / RSS feed