Skip to content

Instantly share code, notes, and snippets.

@fguillen
Created August 17, 2010 10:01
Show Gist options
  • Save fguillen/529148 to your computer and use it in GitHub Desktop.
Save fguillen/529148 to your computer and use it in GitHub Desktop.
# application_controller.rb
before_filter :set_locale
private
def set_locale
new_locale = params[:locale] || cookies[:locale] || extract_locale_from_accept_language_header || I18n.default_locale
if I18n.available_locales.include?( new_locale.to_sym )
I18n.locale = new_locale
cookies[:locale] = {:value => new_locale, :expires => 10.years.from_now}
end
end
def extract_locale_from_accept_language_header
return nil if !request.env['HTTP_ACCEPT_LANGUAGE']
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment