diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d04b380..ecb6749 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -23,11 +23,31 @@ # Sets FastGettext locale for every request made def set_gettext_locale - FastGettext.locale = session[:locale] || FastGettext.default_locale + # If there is no locale set in the session and the current_user exists (meaning the user has just logged in) + if session[:locale].nil? && !current_user.nil? + if current_user.language.nil? + if current_user.org.language.nil? + # The User and Org do not have any language specifications so use the default locale + session[:locale] = FastGettext.default_locale + + else + # The User does not have a language specification but the Org does + session[:locale] = current_user.org.language + end + + else + # The User has a language specification + session[:locale] = current_user.language + end + + # This doesn't seem to do anything ... the locale never seems to change + FastGettext.locale = session[:locale] + end end # PATCH /locale/:locale REST method def set_locale_session + if FastGettext.default_available_locales.include?(params[:locale]) session[:locale] = params[:locale] end diff --git a/test/functional/answers_controller_test.rb b/test/functional/answers_controller_test.rb index efc26d5..d375215 100644 --- a/test/functional/answers_controller_test.rb +++ b/test/functional/answers_controller_test.rb @@ -59,7 +59,7 @@ private def put_answer(answer, attributes, referrer) - put answer_path(I18n.locale, answer), attributes, {'HTTP_REFERER': referrer} + put answer_path(FastGettext.locale, answer), attributes, {'HTTP_REFERER': referrer, 'ACCEPT': 'text/javascript'} assert_equal _('Answer was successfully recorded.'), flash[:notice] assert_response :redirect diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index b11597a..826023b 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -17,52 +17,37 @@ # ---------------------------------------------------------------- test "make sure unauthorized users are redirected to the root path" do plan = Plan.first - get plan_path(I18n.locale, plan) + get plan_path(plan) - assert_redirected_to "#{root_path}?locale=#{I18n.locale}" + assert_redirected_to "#{root_path}" end # ---------------------------------------------------------------- - test "we can change the locale by changing the URL" do - plan = Plan.first - - if I18n.available_locales.count > 1 - # Verify that passing a locale in the URL will set the locale - other = I18n.available_locales.last - - get plan_path(other, plan) - assert_redirected_to "#{root_path}?locale=#{I18n.locale}", "Expected the changed locale to appear in the query string" - assert_equal other.to_sym, I18n.locale, "Expected the locale to have been set when passing it in URL" - end - end - - # ---------------------------------------------------------------- - test "a user's language specification is used if no locale is passed in the URL" do - if I18n.available_locales.count > 1 - @user.language = Language.find_by(abbreviation: I18n.available_locales.last) + test "a user's language specification is set in the session" do + if LANGUAGES.count > 1 + @user.language = LANGUAGES.last @user.save! sign_in @user get root_path - assert_equal @user.language.abbreviation.to_s, I18n.locale.to_s, "Expected the locale to have been set to the user's chosen language" - assert "#{plans_path}".starts_with?("/#{@user.language.abbreviation}/"), "Expected the system to use the user's language specification" + + assert_equal @user.language, session[:locale], "Expected the locale to have been set to the user's chosen language" end end # ---------------------------------------------------------------- test "a user's org language specification is used if no locale is passed in the URL and the user has no language setting" do - if I18n.available_locales.count > 1 + if LANGUAGES.count > 1 @user.language = nil - @user.org[:language_id] = Language.find_by(abbreviation: I18n.available_locales.last).id + @user.org[:language_id] = LANGUAGES.last.id @user.save! sign_in @user get root_path - org_lang = Language.find(@user.org[:language_id]).abbreviation - assert_equal org_lang.to_s, I18n.locale.to_s, "Expected the locale to have been set to the org's chosen language" - assert "#{plans_path}".starts_with?("/#{org_lang}/"), "Expected the system to use the org's language specification" + org_lang = Language.find(@user.org[:language_id]) + assert_equal org_lang, session[:locale], "Expected the locale to have been set to the org's chosen language" end end