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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
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.rbindex 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 Spreeenddef 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_localesenddef available_localesdiff --git a/core/lib/spree/i18n.rb b/core/lib/spree/i18n.rbindex 9967829fd6..67042c57e8 100644--- a/core/lib/spree/i18n.rb+++ b/core/lib/spree/i18n.rb@@ -16,6 +16,18 @@ module SpreeTranslationHelperWrapper.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 translateendenddiff --git a/core/spec/lib/i18n_spec.rb b/core/spec/lib/i18n_spec.rbindex 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' dobefore doI18n.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+ endendit 'translates within the spree scope' dodiff --git a/frontend/config/routes.rb b/frontend/config/routes.rbindex 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 } doroot to: 'home#index'resources :products, only: [:index, :show]diff --git a/frontend/spec/features/locale_spec.rb b/frontend/spec/features/locale_spec.rbindex 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 doendshared_examples 'translates cart page' do- it 'is in french' do+ it 'is in french', retry: 3 doexpect(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 doit 'has localized links', retry: 3 doexpect(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"]')endend