Refactor locale with around method

This commit is contained in:
Sebastian Serth
2021-06-25 15:42:21 +02:00
parent e7cf4ef5db
commit 1b71163a77
3 changed files with 19 additions and 10 deletions

View File

@ -8,7 +8,8 @@ class ApplicationController < ActionController::Base
after_action :verify_authorized, except: %i[welcome] after_action :verify_authorized, except: %i[welcome]
around_action :mnemosyne_trace 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) protect_from_forgery(with: :exception, prepend: true)
rescue_from Pundit::NotAuthorizedError, with: :render_not_authorized rescue_from Pundit::NotAuthorizedError, with: :render_not_authorized
rescue_from ActionController::InvalidAuthenticityToken, with: :render_csrf_error rescue_from ActionController::InvalidAuthenticityToken, with: :render_csrf_error
@ -71,12 +72,13 @@ class ApplicationController < ActionController::Base
end end
private :render_error private :render_error
def set_locale def switch_locale(&action)
session[:locale] = params[:custom_locale] || params[:locale] || session[:locale] session[:locale] = params[:custom_locale] || params[:locale] || session[:locale]
I18n.locale = session[:locale] || I18n.default_locale locale = session[:locale] || I18n.default_locale
Sentry.set_extras(locale: I18n.locale) I18n.with_locale(locale, &action)
Sentry.set_extras(locale: locale)
end end
private :set_locale private :switch_locale
def welcome def welcome
# Show root page # Show root page

View File

@ -59,6 +59,8 @@ describe ApplicationController do
context "with a 'locale' value in the session" do context "with a 'locale' value in the session" do
it 'sets this locale' do it 'sets this locale' do
session[:locale] = locale 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) expect(I18n).to receive(:locale=).with(locale)
get :welcome get :welcome
end end
@ -67,7 +69,7 @@ describe ApplicationController do
context "without a 'locale' value in the session" do context "without a 'locale' value in the session" do
it 'sets the default locale' do it 'sets the default locale' do
expect(session[:locale]).to be_blank 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 get :welcome
end end
end end

View File

@ -85,9 +85,12 @@ describe SessionsController do
end end
it 'sets the specified locale' do 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 perform_request
expect(I18n.locale).to eq(locale) expect(i18n.locale.to_sym).to eq(locale)
end end
it 'assigns the exercise' do it 'assigns the exercise' do
@ -107,7 +110,8 @@ describe SessionsController do
end end
context 'when LTI outcomes are supported' do 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 before do
allow(controller).to receive(:lti_outcome_service?).and_return(true) allow(controller).to receive(:lti_outcome_service?).and_return(true)
@ -118,7 +122,8 @@ describe SessionsController do
end end
context 'when LTI outcomes are not supported' do 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 before do
allow(controller).to receive(:lti_outcome_service?).and_return(false) allow(controller).to receive(:lti_outcome_service?).and_return(false)