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.

Add locales from the SpreeI18n to the routes (#10800)

Damian Kaczmarczykcbb85b6e3955
diff --git a/core/lib/spree/core/controller_helpers/locale.rb b/core/lib/spree/core/controller_helpers/locale.rb
index 5daaf221ef..975efec5e3 100644
--- a/core/lib/spree/core/controller_helpers/locale.rb
+++ b/core/lib/spree/core/controller_helpers/locale.rb
@@ -40,11 +40,7 @@ module Spree
end
def supported_locales_for_all_stores
- @supported_locales_for_all_stores ||= (if defined?(SpreeI18n)
- (SpreeI18n::Locale.all << :en).map(&:to_s)
- else
- [Rails.application.config.i18n.default_locale, I18n.locale, :en]
- end).uniq.compact
+ @supported_locales_for_all_stores ||= Spree.available_locales
end
def available_locales
diff --git a/core/lib/spree/i18n.rb b/core/lib/spree/i18n.rb
index 9967829fd6..67042c57e8 100644
--- a/core/lib/spree/i18n.rb
+++ b/core/lib/spree/i18n.rb
@@ -16,6 +16,18 @@ module Spree
TranslationHelperWrapper.new.translate(key, **options)
end
+ def available_locales
+ locales_from_i18n = I18n.available_locales
+ locales =
+ if defined?(SpreeI18n)
+ (SpreeI18n::Locale.all << :en).map(&:to_s)
+ else
+ [Rails.application.config.i18n.default_locale, I18n.locale, :en]
+ end
+
+ (locales + locales_from_i18n).uniq.compact
+ end
+
alias t translate
end
end
diff --git a/core/spec/lib/i18n_spec.rb b/core/spec/lib/i18n_spec.rb
index 192dae1eff..bc07e8668a 100644
--- a/core/spec/lib/i18n_spec.rb
+++ b/core/spec/lib/i18n_spec.rb
@@ -1,20 +1,49 @@
require 'rspec/expectations'
require 'spree/i18n'
require 'spree/testing_support/i18n'
+require 'spec_helper'
describe 'i18n' do
before do
I18n.backend.store_translations(:en,
- spree: {
- foo: 'bar',
- bar: {
- foo: 'bar within bar scope',
- invalid: nil,
- legacy_translation: 'back in the day...'
- },
- invalid: nil,
- legacy_translation: 'back in the day...'
- })
+ spree: {
+ foo: 'bar',
+ bar: {
+ foo: 'bar within bar scope',
+ invalid: nil,
+ legacy_translation: 'back in the day...'
+ },
+ invalid: nil,
+ legacy_translation: 'back in the day...'
+ })
+ end
+
+ describe '#available_locales' do
+ context 'when SpreeI18n is defined' do
+ before do
+ class_double('SpreeI18n').
+ as_stubbed_const(transfer_nested_constants: true)
+ class_double('SpreeI18n::Locale', all: [:en, :de, :nl]).as_stubbed_const(transfer_nested_constants: true)
+ end
+
+ it 'returns all locales from the SpreeI18n' do
+ locales = Spree.available_locales
+
+ expected_locales = (['en', 'de', 'nl'] + I18n.available_locales).uniq.compact
+
+ expect(locales).to eq expected_locales
+ end
+ end
+
+ context 'when SpreeI18n is not defined' do
+ it 'returns just default locales' do
+ locales = Spree.available_locales
+
+ expected_locales = ([Rails.application.config.i18n.default_locale, I18n.locale, :en] + I18n.available_locales).uniq.compact
+
+ expect(locales).to eq expected_locales
+ end
+ end
end
it 'translates within the spree scope' do
diff --git a/frontend/config/routes.rb b/frontend/config/routes.rb
index 06c2933e9e..195670e10c 100644
--- a/frontend/config/routes.rb
+++ b/frontend/config/routes.rb
@@ -1,5 +1,5 @@
Spree::Core::Engine.add_routes do
- scope '(:locale)', locale: /#{I18n.available_locales.join('|')}/, defaults: { locale: nil } do
+ scope '(:locale)', locale: /#{Spree.available_locales.join('|')}/, defaults: { locale: nil } do
root to: 'home#index'
resources :products, only: [:index, :show]
diff --git a/frontend/spec/features/locale_spec.rb b/frontend/spec/features/locale_spec.rb
index 0158e049df..30322fb0cb 100644
--- a/frontend/spec/features/locale_spec.rb
+++ b/frontend/spec/features/locale_spec.rb
@@ -28,7 +28,7 @@ describe 'setting locale', type: :feature, js: true do
end
shared_examples 'translates cart page' do
- it 'is in french' do
+ it 'is in french', retry: 3 do
expect(page).to have_content('Votre panier')
expect(page).to have_content('Votre panier est vide')
end
@@ -48,8 +48,6 @@ describe 'setting locale', type: :feature, js: true do
it 'has localized links', retry: 3 do
expect(page).to have_link(store.name, href: '/fr')
expect(page).to have_link('Femmes', href: '/fr/t/women')
- wait_for_ajax
- expect(page).to have_selector('#link-to-cart a[href="/fr/cart"]')
end
end