Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/spree/locale_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions spec/controllers/locales_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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