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.

Fixed locale fallback for slug resources in API v3

Damian Legawiec2349eee4bf1c
diff --git a/api/app/controllers/spree/api/v3/store/products_controller.rb b/api/app/controllers/spree/api/v3/store/products_controller.rb
index d901cdca65..41a7487fed 100644
--- a/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -14,12 +14,13 @@ module Spree
end
# Find product by slug or prefix_id with i18n scope for SEO-friendly URLs
+ # Falls back to default locale if product is not found in the current locale
def find_resource
id = params[:id]
if id.to_s.start_with?('prod_')
scope.find_by!(prefix_id: id)
else
- scope.i18n.find_by!(slug: id)
+ find_with_fallback_default_locale { scope.i18n.find_by!(slug: id) }
end
end
diff --git a/api/app/controllers/spree/api/v3/store/taxons_controller.rb b/api/app/controllers/spree/api/v3/store/taxons_controller.rb
index f407f849ce..2540fa8ccf 100644
--- a/api/app/controllers/spree/api/v3/store/taxons_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/taxons_controller.rb
@@ -14,12 +14,13 @@ module Spree
end
# Find taxon by permalink or prefix_id with i18n scope for SEO-friendly URLs
+ # Falls back to default locale if taxon is not found in the current locale
def find_resource
id = params[:id]
if id.to_s.start_with?('txn_')
scope.find_by!(prefix_id: id)
else
- scope.i18n.find_by!(permalink: id)
+ find_with_fallback_default_locale { scope.i18n.find_by!(permalink: id) }
end
end
end
diff --git a/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index 59ac274ec1..1b158a82a9 100644
--- a/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -211,6 +211,31 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
expect(response).to have_http_status(:not_found)
end
+
+ context 'locale fallback' do
+ let!(:english_only_product) do
+ create(:product, stores: [store], status: 'active', name: 'English Only', slug: 'english-only')
+ end
+
+ it 'falls back to default locale when product has no translation in requested locale' do
+ request.headers['x-spree-locale'] = 'fr'
+ get :show, params: { id: 'english-only' }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(english_only_product.prefix_id)
+ # Name returns English since no French translation exists
+ expect(json_response['name']).to eq('English Only')
+ end
+
+ it 'returns translated content when translation exists' do
+ request.headers['x-spree-locale'] = 'fr'
+ get :show, params: { id: 'english-product' }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(translated_product.prefix_id)
+ expect(json_response['name']).to eq('Produit Français')
+ end
+ end
end
context 'error handling' do
diff --git a/api/spec/controllers/spree/api/v3/store/taxons_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/taxons_controller_spec.rb
index d3f1ba53ed..394655060e 100644
--- a/api/spec/controllers/spree/api/v3/store/taxons_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v3/store/taxons_controller_spec.rb
@@ -113,6 +113,31 @@ RSpec.describe Spree::Api::V3::Store::TaxonsController, type: :controller do
expect(response).to have_http_status(:not_found)
end
+
+ context 'locale fallback' do
+ let!(:english_only_taxon) do
+ create(:taxon, taxonomy: taxonomy, name: 'Electronics')
+ end
+
+ it 'falls back to default locale when taxon has no translation in requested locale' do
+ request.headers['x-spree-locale'] = 'fr'
+ get :show, params: { id: english_only_taxon.permalink }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(english_only_taxon.prefix_id)
+ # Name returns English since no French translation exists
+ expect(json_response['name']).to eq('Electronics')
+ end
+
+ it 'returns translated content when translation exists' do
+ request.headers['x-spree-locale'] = 'fr'
+ get :show, params: { id: translated_taxon.permalink }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['id']).to eq(translated_taxon.prefix_id)
+ expect(json_response['name']).to eq('Vêtements')
+ end
+ end
end
context 'error handling' do