Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Support locale switching when locale is passed as a param

Damian Legawiec0ac9af8e5678
diff --git a/core/app/services/spree/build_localized_url.rb b/core/app/services/spree/build_localized_url.rb
index 68b2670cb5..90bbb5eaf2 100644
--- a/core/app/services/spree/build_localized_url.rb
+++ b/core/app/services/spree/build_localized_url.rb
@@ -1,39 +1,99 @@
+require 'uri'
+
module Spree
class BuildLocalizedUrl
prepend Spree::ServiceModule::Base
- LOCALE_REGEX = /^\/[A-Za-z]{2}\/|^\/[A-Za-z]{2}$/
+ LOCALE_REGEX = /^\/[A-Za-z]{2}\/|^\/[A-Za-z]{2}$/.freeze
+
+ SUPPORTED_PATHS_REGEX = /\/(products|t\/|cart|checkout|addresses|content)/.freeze
def call(url:, locale:, default_locale: nil)
run :initialize_url_object
run :generate_new_path
+ run :append_locale_param
run :build_url
end
protected
def initialize_url_object(url:, locale:, default_locale:)
- success(url: URI(url), locale: locale, default_locale: default_locale)
+ success(
+ url: URI(url),
+ locale: locale,
+ default_locale_supplied: default_locale_supplied?(locale, default_locale)
+ )
end
- def generate_new_path(url:, locale:, default_locale:)
- match = url.path.match(LOCALE_REGEX)[0].gsub('/', '') if url.path.match(LOCALE_REGEX)
-
- # we've found previously used locale in the URL, so we need to replace it
- # or if it's the default store locale remove it completely
+ def generate_new_path(url:, locale:, default_locale_supplied:)
+ unless supported_path?(url.path)
+ return success(
+ url: url,
+ locale: locale,
+ path: cleanup_path(url.path),
+ default_locale_supplied: default_locale_supplied,
+ locale_added_to_path: false
+ )
+ end
- new_path = if default_locale.present? && default_locale.to_s == locale.to_s
- match.present? ? url.path.gsub(LOCALE_REGEX, '/') : url.path
+ new_path = if default_locale_supplied
+ maches_locale_regex?(url.path) ? url.path.gsub(LOCALE_REGEX, '/') : url.path
else
- match.present? ? url.path.gsub(LOCALE_REGEX, "/#{locale}/") : "/#{locale}#{url.path}"
+ maches_locale_regex?(url.path) ? url.path.gsub(LOCALE_REGEX, "/#{locale}/") : "/#{locale}#{url.path}"
end
- success(url: url, path: new_path.chomp('/').gsub('//', '/'))
+
+ success(
+ url: url,
+ locale: locale,
+ path: cleanup_path(new_path),
+ default_locale_supplied: default_locale_supplied,
+ locale_added_to_path: true
+ )
end
- def build_url(url:, path:)
- builder_class = url.scheme == 'http' ? URI::HTTP : URI::HTTPS
- localized_url = builder_class.build(host: url.host, port: url.port, path: path, query: url.query).to_s
+ def append_locale_param(url:, locale:, path:, default_locale_supplied:, locale_added_to_path:)
+ return success(url: url, path: path, query: url.query) if locale_added_to_path
+
+ query_params = Rack::Utils.parse_nested_query(url.query)
+
+ if default_locale_supplied
+ query_params.delete('locale')
+ else
+ query_params.merge!('locale' => locale)
+ end
+
+ query_string = query_params.any? ? query_params.to_query : nil
+
+ success(url: url, path: path, query: query_string)
+ end
+
+ def build_url(url:, path:, query:)
+ localized_url = builder_class(url).build(host: url.host, port: url.port, path: path, query: query).to_s
success(localized_url)
end
+
+ private
+
+ def supported_path?(path)
+ return true if path.blank? || path == '/' || maches_locale_regex?(path)
+
+ path.match(SUPPORTED_PATHS_REGEX)
+ end
+
+ def maches_locale_regex?(path)
+ path.match(LOCALE_REGEX)[0].gsub('/', '') if path.match(LOCALE_REGEX)
+ end
+
+ def default_locale_supplied?(locale, default_locale)
+ default_locale.present? && default_locale.to_s == locale.to_s
+ end
+
+ def cleanup_path(path)
+ path.chomp('/').gsub('//', '/')
+ end
+
+ def builder_class(url)
+ url.scheme == 'http' ? URI::HTTP : URI::HTTPS
+ end
end
end
diff --git a/core/spec/services/spree/build_localized_url_spec.rb b/core/spec/services/spree/build_localized_url_spec.rb
index f7c858cf4b..045e726b15 100644
--- a/core/spec/services/spree/build_localized_url_spec.rb
+++ b/core/spec/services/spree/build_localized_url_spec.rb
@@ -99,6 +99,52 @@ module Spree
end
end
+ context 'not supported path' do
+ context 'default store locale' do
+ let(:locale) { 'en' }
+
+ context 'http with port' do
+ let(:url) { 'http://example.com:3000/account?locale=fr' }
+
+ it { expect(result).to eq('http://example.com:3000/account') }
+ end
+
+ context 'https with no trailing slash' do
+ let(:url) { 'https://example.com/login/?locale=fr' }
+
+ it { expect(result).to eq('https://example.com/login') }
+ end
+
+ context 'with parameters and port' do
+ let(:url) { 'https://example.com:3000/login?locale=fr&utm_source=google' }
+
+ it { expect(result).to eq('https://example.com:3000/login?utm_source=google') }
+ end
+ end
+
+ context 'non-default locale' do
+ let(:locale) { 'de' }
+
+ context 'http with port' do
+ let(:url) { 'http://example.com:3000/login/' }
+
+ it { expect(result).to eq('http://example.com:3000/login?locale=de') }
+ end
+
+ context 'https with no trailing slash' do
+ let(:url) { 'https://example.com/account' }
+
+ it { expect(result).to eq('https://example.com/account?locale=de') }
+ end
+
+ context 'with parameters and port' do
+ let(:url) { 'https://example.com:3000/login?locale=fr&utm_source=google' }
+
+ it { expect(result).to eq('https://example.com:3000/login?locale=de&utm_source=google') }
+ end
+ end
+ end
+
context 'default_locale is nil' do
let(:default_locale) { nil }
let(:locale) { 'de' }
diff --git a/frontend/app/controllers/spree/locale_controller.rb b/frontend/app/controllers/spree/locale_controller.rb
index 665fb02034..2a5dc1c9c3 100644
--- a/frontend/app/controllers/spree/locale_controller.rb
+++ b/frontend/app/controllers/spree/locale_controller.rb
@@ -1,5 +1,3 @@
-require 'uri'
-
module Spree
class LocaleController < Spree::StoreController
def index
@@ -10,7 +8,7 @@ module Spree
new_locale = (params[:switch_to_locale] || params[:locale]).to_s
if new_locale.present? && supported_locale?(new_locale)
- if request.env['HTTP_REFERER'].present? && request.env['HTTP_REFERER'] != request.env['REQUEST_URI']
+ if should_build_new_url?
redirect_to BuildLocalizedUrl.call(
url: request.env['HTTP_REFERER'],
locale: new_locale,
@@ -23,5 +21,11 @@ module Spree
redirect_to root_path
end
end
+
+ private
+
+ def should_build_new_url?
+ request.env['HTTP_REFERER'].present? && request.env['HTTP_REFERER'] != request.env['REQUEST_URI']
+ end
end
end