diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 21f4505f..a434cb67 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -109,7 +109,7 @@ def set_cms_footer_pages end def set_locale - if current_user && current_user.profile && current_user.profile.language.blank? == false + if current_user && current_user.profile && current_user.profile.language.present? if user_language_override? == true I18n.locale = session[:locale].to_sym unless session[:locale].blank? else @@ -119,6 +119,7 @@ def set_locale when "Spanish" I18n.locale = :es end + session[:locale] = I18n.locale.to_s end else I18n.locale = session[:locale].nil? ? :en : session[:locale].to_sym @@ -168,7 +169,7 @@ def missing_profile?(user) end def user_language_override? - if current_user.profile.language.blank? == false + if current_user.profile.language.present? user_lang_abbrv2 = current_user.profile.language_id == 1 ? "en" : "es" return true if session[:locale] != user_lang_abbrv2 else diff --git a/app/controllers/profiles_controller.rb b/app/controllers/profiles_controller.rb index c229fb82..9c2ac37d 100644 --- a/app/controllers/profiles_controller.rb +++ b/app/controllers/profiles_controller.rb @@ -51,8 +51,12 @@ def update redirect_path = profile_path end + old_language_id = @profile.language.id if @profile.language.present? + new_language_id = profile_params[:language_id].to_i if profile_params[:language_id].present? + respond_to do |format| if @profile.context_update(profile_params) + update_locale(new_language_id) unless new_language_id == old_language_id format.html { redirect_to redirect_path, notice: "Profile was successfully updated." } format.json { render :show, status: :ok, location: @profile } else @@ -86,4 +90,15 @@ def show_quiz? !current_user.has_role?(:admin, current_organization) end + def update_locale(language_id=1) + language = Language.find(language_id) + case language.name + when "English" + I18n.locale = :en + when "Spanish" + I18n.locale = :es + end + session[:locale] = I18n.locale.to_s + end + end diff --git a/config/initializers/after_sign_in.rb b/config/initializers/after_sign_in.rb new file mode 100644 index 00000000..2509be25 --- /dev/null +++ b/config/initializers/after_sign_in.rb @@ -0,0 +1,12 @@ +Warden::Manager.after_set_user except: :fetch do |user, auth, opts| + if user.present? && user.profile.present? && user.profile.language.present? + case user.profile.language.name + when "English" + I18n.locale = :en + when "Spanish" + I18n.locale = :es + end + + auth.env["rack.session"][:locale] = I18n.locale.to_s + end +end \ No newline at end of file