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
Fixed mail store locale fallback
Damian Legawiec8464e6af080e
diff --git a/spree/core/app/mailers/spree/base_mailer.rb b/spree/core/app/mailers/spree/base_mailer.rbindex ebb82cc28c..4e5f883545 100644--- a/spree/core/app/mailers/spree/base_mailer.rb+++ b/spree/core/app/mailers/spree/base_mailer.rb@@ -23,10 +23,13 @@ module Spreereturn yield if locale.blank?previous_fallbacks = Mobility.store_based_fallbacks+ previously_active = @_store_locale_active+ @_store_locale_active = truebeginSpree::Locales::SetFallbackLocaleForStore.new.call(store: store) if storeI18n.with_locale(locale, &block)ensure+ @_store_locale_active = previously_activeMobility.store_based_fallbacks = previous_fallbacksendend@@ -52,7 +55,16 @@ module Spreedef mail(headers = {}, &block)ensure_default_action_mailer_url_host(headers[:store_url])- super if Spree::Config[:send_core_emails]+ return unless Spree::Config[:send_core_emails]++ if @_store_locale_active+ super+ else+ # Subclasses that call `mail` without wrapping their action in+ # `with_store_locale` (e.g. Devise mailers, extensions) still get the+ # store default locale, as `mail` applied before Spree 5.6.+ with_store_locale(current_store) { super }+ endend# @deprecated Each mailer action now wraps its body in {#with_store_locale},@@ -68,6 +80,14 @@ module SpreeI18n.locale = locale if locale.present?end+ protected++ # The "<store> <subject> #<number>" subject line shared by customer-facing+ # order emails, with the optional [RESEND] prefix.+ def order_email_subject(store, subject, number, resend: false)+ "#{resend ? "[#{Spree.t(:resend).upcase}] " : ''}#{store.name} #{subject} ##{number}"+ end+private# this ensures that ActionMailer::Base.default_url_options[:host] is always setdiff --git a/spree/core/spec/mailers/spree/base_mailer_spec.rb b/spree/core/spec/mailers/spree/base_mailer_spec.rbindex ca8052d832..b0f742f973 100644--- a/spree/core/spec/mailers/spree/base_mailer_spec.rb+++ b/spree/core/spec/mailers/spree/base_mailer_spec.rb@@ -72,4 +72,55 @@ describe Spree::BaseMailer, type: :mailer doexpect(Mobility.store_based_fallbacks).to equal(previous_fallbacks)endend++ describe '#mail store locale fallback' do+ # Subclasses that call `mail` directly without `with_store_locale` — Devise+ # mailers, extensions — must still render in the store default locale, as+ # `mail` guaranteed before Spree 5.6 via `set_email_locale`.+ let(:probe_mailer) do+ stub_const('LocaleProbeMailer', Class.new(described_class) do+ cattr_accessor :captured_locale++ def plain_email+ mail(to: '[email protected]', from: '[email protected]', subject: 'probe') do |format|+ format.text do+ self.class.captured_locale = I18n.locale+ render plain: 'probe'+ end+ end+ end++ def wrapped_email+ with_store_locale(current_store, 'fr') do+ mail(to: '[email protected]', from: '[email protected]', subject: 'probe') do |format|+ format.text do+ self.class.captured_locale = I18n.locale+ render plain: 'probe'+ end+ end+ end+ end+ end)+ end++ before do+ I18n.enforce_available_locales = false+ store.update!(default_locale: 'de')+ end++ after do+ I18n.enforce_available_locales = true+ store.update!(default_locale: 'en')+ end++ it 'renders unwrapped mailers in the store default locale' do+ probe_mailer.plain_email.message+ expect(probe_mailer.captured_locale).to eq(:de)+ end++ it 'does not override an explicit with_store_locale wrapping' do+ probe_mailer.wrapped_email.message+ expect(probe_mailer.captured_locale).to eq(:fr)+ end+ endend