Skip to content

Commit

Permalink
[BUG] Set language on user sign in and language preference update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Reis committed Feb 21, 2017
1 parent 79a9e59 commit b052cbe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 12 additions & 0 deletions config/initializers/after_sign_in.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b052cbe

Please sign in to comment.