From 1b71163a7731c8e8de2c455ebd6379587dc25e5d Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 25 Jun 2021 15:42:21 +0200 Subject: [PATCH] Refactor locale with around method --- app/controllers/application_controller.rb | 12 +++++++----- spec/controllers/application_controller_spec.rb | 4 +++- spec/controllers/sessions_controller_spec.rb | 13 +++++++++---- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6c50b021..327437a7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -8,7 +8,8 @@ class ApplicationController < ActionController::Base after_action :verify_authorized, except: %i[welcome] around_action :mnemosyne_trace - before_action :set_sentry_context, :set_locale, :allow_iframe_requests, :load_embed_options + around_action :switch_locale + before_action :set_sentry_context, :allow_iframe_requests, :load_embed_options protect_from_forgery(with: :exception, prepend: true) rescue_from Pundit::NotAuthorizedError, with: :render_not_authorized rescue_from ActionController::InvalidAuthenticityToken, with: :render_csrf_error @@ -71,12 +72,13 @@ class ApplicationController < ActionController::Base end private :render_error - def set_locale + def switch_locale(&action) session[:locale] = params[:custom_locale] || params[:locale] || session[:locale] - I18n.locale = session[:locale] || I18n.default_locale - Sentry.set_extras(locale: I18n.locale) + locale = session[:locale] || I18n.default_locale + I18n.with_locale(locale, &action) + Sentry.set_extras(locale: locale) end - private :set_locale + private :switch_locale def welcome # Show root page diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 014b6266..d9783d41 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -59,6 +59,8 @@ describe ApplicationController do context "with a 'locale' value in the session" do it 'sets this locale' do session[:locale] = locale + # The around block first sets the default language and then the language requested + expect(I18n).to receive(:locale=).with(I18n.default_locale) expect(I18n).to receive(:locale=).with(locale) get :welcome end @@ -67,7 +69,7 @@ describe ApplicationController do context "without a 'locale' value in the session" do it 'sets the default locale' do expect(session[:locale]).to be_blank - expect(I18n).to receive(:locale=).with(I18n.default_locale) + expect(I18n).to receive(:locale=).with(I18n.default_locale).at_least(:once) get :welcome end end diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 8030f4df..7388f7ef 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -85,9 +85,12 @@ describe SessionsController do end it 'sets the specified locale' do - expect(controller).to receive(:set_locale).and_call_original + expect(controller).to receive(:switch_locale).and_call_original + i18n = instance_double 'i18n', locale: locale.to_s + allow(I18n).to receive(:locale=).with(I18n.default_locale).and_call_original + allow(I18n).to receive(:locale=).with(locale.to_s).and_return(i18n) perform_request - expect(I18n.locale).to eq(locale) + expect(i18n.locale.to_sym).to eq(locale) end it 'assigns the exercise' do @@ -107,7 +110,8 @@ describe SessionsController do end context 'when LTI outcomes are supported' do - let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer) } + # The expected message should be localized in the requested localization + let(:message) { I18n.t('sessions.create_through_lti.session_with_outcome', consumer: consumer, locale: locale) } before do allow(controller).to receive(:lti_outcome_service?).and_return(true) @@ -118,7 +122,8 @@ describe SessionsController do end context 'when LTI outcomes are not supported' do - let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer) } + # The expected message should be localized in the requested localization + let(:message) { I18n.t('sessions.create_through_lti.session_without_outcome', consumer: consumer, locale: locale) } before do allow(controller).to receive(:lti_outcome_service?).and_return(false)