Skip to content

Instantly share code, notes, and snippets.

@apeniche
Created September 2, 2015 20:48
Show Gist options
  • Save apeniche/d150dd7218dd35c26963 to your computer and use it in GitHub Desktop.
Save apeniche/d150dd7218dd35c26963 to your computer and use it in GitHub Desktop.
Change language
// This goes in the application controller
before_action :set_locale
def set_locale
if current_user and current_user.locale
I18n.locale = current_user.locale[0..1]
else
if session[:lang]
I18n.locale = session[:lang]
else
I18n.locale = request.env["HTTP_ACCEPT_LANGUAGE"] || :en
end
end
end
// This is the action, can go in any controller, of course we would need to add a route for this.
def change_language
if params[:lang]
I18n.locale = params[:lang]
if current_user
current_user.locale = params[:lang]
current_user.save
else
session[:lang] = params[:lang]
end
end
if request.env["HTTP_REFERER"]
redirect_to :back
else
redirect_to(:controller => 'home', :action => 'index')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment