diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b64f11bb4..6779d223a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,21 @@ class ApplicationController < ActionController::Base self.responder = ApplicationResponder respond_to :html + before_action :set_locale + # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + private + + def set_locale + if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym) + session[:locale] = params[:locale] + end + + session[:locale] ||= I18n.default_locale + + I18n.locale = session[:locale] + end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 499265f5a..31314a357 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -10,10 +10,16 @@ module ApplicationHelper end def home_page? - request.fullpath == '/' + # Using path because fullpath yields false negatives since it contains + # parameters too + request.path == '/' end def header_css home_page? ? '' : 'results' end + + def available_locales_to_switch + I18n.available_locales - [I18n.locale] + end end diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 4353e164a..b76b2735e 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -4,6 +4,14 @@