Skip to content

Instantly share code, notes, and snippets.

@pachisaez
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save pachisaez/603432d8b554d6ab3879 to your computer and use it in GitHub Desktop.

Select an option

Save pachisaez/603432d8b554d6ab3879 to your computer and use it in GitHub Desktop.
Internationalization for two languages: EN (english) and ES (spanish), taking EN by default.
class ApplicationController < ActionController::Base
before_filter :set_locale
protected
def set_locale
locale = params[:locale] || session[:locale] ||
(extact_locale_from_accept_language_header=='es' ? 'es' : 'en')
session[:locale] = locale
I18n.locale = locale
end
private
def extact_locale_from_accept_language_header
request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first rescue "en"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment