diff --git a/app/controllers/spree/locale_controller.rb b/app/controllers/spree/locale_controller.rb index 10a55beb8..608ad796f 100644 --- a/app/controllers/spree/locale_controller.rb +++ b/app/controllers/spree/locale_controller.rb @@ -5,7 +5,13 @@ def index end def set - redirect_to root_path(locale: params[:switch_to_locale]) + if request.referrer + path = spree.routes.recognize_path(request.referrer) + path[:locale] = params[:switch_to_locale] + redirect_to url_for(path) + else + redirect_to root_path(locale: params[:switch_to_locale]) + end end end end diff --git a/spec/controllers/locales_controller_spec.rb b/spec/controllers/locales_controller_spec.rb index 61ba5816b..4d2979df1 100644 --- a/spec/controllers/locales_controller_spec.rb +++ b/spec/controllers/locales_controller_spec.rb @@ -20,3 +20,21 @@ end end end + +RSpec.describe Spree::LocaleController, type: :controller do + routes { Spree::Core::Engine.routes } + + before do + reset_spree_preferences + SpreeI18n::Config.available_locales = [:en, :es] + end + + describe "#set" do + it 'redirects to referrer' do + root = request.url + request.env['HTTP_REFERER'] = "#{root}/products/some-product" + post :set, params: { switch_to_locale: :es } + expect(response).to redirect_to("#{root}/es/products/some-product") + end + end +end