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
Fix emails setting locale
Damian Legawiecbc290aa44601
diff --git a/spree/core/app/mailers/spree/base_mailer.rb b/spree/core/app/mailers/spree/base_mailer.rbindex 6e9d9e9fb4..ebb82cc28c 100644--- a/spree/core/app/mailers/spree/base_mailer.rb+++ b/spree/core/app/mailers/spree/base_mailer.rb@@ -8,6 +8,29 @@ module Spreehelper_method :current_store+ # Render an email in the given locale, with the store's translation fallbacks+ # active, and restore both afterwards. Controllers set these fallbacks per+ # request via `set_fallback_locale`, but mailers run in background jobs where+ # that never happens — so without this, translatable attributes (store name,+ # product names, taxon names, …) return nil under a non-default locale and+ # leave e.g. the footer blank. Setting the fallbacks here mirrors a request,+ # so reads fall back to the store's default-locale value.+ #+ # @param store [Spree::Store]+ # @param locale [String, Symbol, nil] defaults to the store's default locale+ def with_store_locale(store, locale = nil, &block)+ locale = locale.presence || store&.default_locale+ return yield if locale.blank?++ previous_fallbacks = Mobility.store_based_fallbacks+ begin+ Spree::Locales::SetFallbackLocaleForStore.new.call(store: store) if store+ I18n.with_locale(locale, &block)+ ensure+ Mobility.store_based_fallbacks = previous_fallbacks+ end+ end+def from_addresscurrent_store.mail_from_addressend@@ -29,10 +52,22 @@ module Spreedef mail(headers = {}, &block)ensure_default_action_mailer_url_host(headers[:store_url])- set_email_localesuper if Spree::Config[:send_core_emails]end+ # @deprecated Each mailer action now wraps its body in {#with_store_locale},+ # which also activates the store's translation fallbacks and restores the+ # previous locale afterwards. This method mutates `I18n.locale` for the rest+ # of the thread without restoring it. Will be removed in Spree 6.0.+ def set_email_locale+ Spree::Deprecation.warn(+ 'Spree::BaseMailer#set_email_locale is deprecated and will be removed in Spree 6.0. ' \+ 'Wrap the mailer action body in `with_store_locale(store, locale) { ... }` instead.'+ )+ locale = @order&.locale.presence || @order&.store&.default_locale || current_store&.default_locale+ I18n.locale = locale if locale.present?+ end+private# this ensures that ActionMailer::Base.default_url_options[:host] is always set@@ -46,13 +81,5 @@ module SpreeActionMailer::Base.default_url_options ||= {}ActionMailer::Base.default_url_options[:host] = host_urlend-- # Sets the I18n locale for the email.- # Prefers the order's locale (the language the customer used),- # falls back to the store's default locale.- def set_email_locale- locale = @order&.locale.presence || @order&.store&.default_locale || current_store&.default_locale- I18n.locale = locale if locale.present?- endendenddiff --git a/spree/core/app/mailers/spree/export_mailer.rb b/spree/core/app/mailers/spree/export_mailer.rbindex 3c76b73c9b..24150fcfb5 100644--- a/spree/core/app/mailers/spree/export_mailer.rb+++ b/spree/core/app/mailers/spree/export_mailer.rb@@ -3,13 +3,15 @@ module Spreedef export_done(export)@export = export- mail(- to: @export.user.email,- subject: Spree.t('export_mailer.export_done.subject', export_number: @export.number).to_s,- from: from_address,- reply_to: reply_to_address,- store_url: current_store.url- )+ with_store_locale(@export.store) do+ mail(+ to: @export.user.email,+ subject: Spree.t('export_mailer.export_done.subject', export_number: @export.number).to_s,+ from: from_address,+ reply_to: reply_to_address,+ store_url: current_store.url+ )+ endenddef current_storediff --git a/spree/core/app/mailers/spree/invitation_mailer.rb b/spree/core/app/mailers/spree/invitation_mailer.rbindex 4caf0ebf18..35862f944a 100644--- a/spree/core/app/mailers/spree/invitation_mailer.rb+++ b/spree/core/app/mailers/spree/invitation_mailer.rb@@ -3,22 +3,26 @@ module Spree# invitation email, sending email to the invited to let them know they have been invited to join a store/account/vendordef invitation_email(invitation)@invitation = invitation- mail(to: invitation.email,- from: from_address,- reply_to: reply_to_address,- subject: Spree.t('invitation_mailer.invitation_email.subject',- resource_name: invitation.resource&.name))+ with_store_locale(invitation.store) do+ mail(to: invitation.email,+ from: from_address,+ reply_to: reply_to_address,+ subject: Spree.t('invitation_mailer.invitation_email.subject',+ resource_name: invitation.resource&.name))+ endend# sending email to the inviter to let them know the invitee has accepted the invitationdef invitation_accepted(invitation)@invitation = invitation- mail(to: invitation.inviter.email,- from: from_address,- reply_to: reply_to_address,- subject: Spree.t('invitation_mailer.invitation_accepted.subject',- invitee_name: invitation.invitee&.name,- resource_name: invitation.resource&.name))+ with_store_locale(invitation.store) do+ mail(to: invitation.inviter.email,+ from: from_address,+ reply_to: reply_to_address,+ subject: Spree.t('invitation_mailer.invitation_accepted.subject',+ invitee_name: invitation.invitee&.name,+ resource_name: invitation.resource&.name))+ endendendenddiff --git a/spree/core/app/mailers/spree/report_mailer.rb b/spree/core/app/mailers/spree/report_mailer.rbindex 0a8c116189..12f521af28 100644--- a/spree/core/app/mailers/spree/report_mailer.rb+++ b/spree/core/app/mailers/spree/report_mailer.rb@@ -3,12 +3,14 @@ module Spreedef report_done(report)@report = report- mail(- to: @report.user.email,- subject: Spree.t('report_mailer.report_done.subject', report_name: @report.human_name).to_s,- from: from_address,- reply_to: reply_to_address- )+ with_store_locale(@report.store) do+ mail(+ to: @report.user.email,+ subject: Spree.t('report_mailer.report_done.subject', report_name: @report.human_name).to_s,+ from: from_address,+ reply_to: reply_to_address+ )+ endendprivatediff --git a/spree/core/app/mailers/spree/webhook_mailer.rb b/spree/core/app/mailers/spree/webhook_mailer.rbindex 22c856c181..6962dc3752 100644--- a/spree/core/app/mailers/spree/webhook_mailer.rb+++ b/spree/core/app/mailers/spree/webhook_mailer.rb@@ -6,12 +6,14 @@ module Spree@endpoint = webhook_endpoint@current_store = webhook_endpoint.store- mail(- to: @current_store.new_order_notifications_email.presence || @current_store.mail_from_address,- from: from_address,- subject: Spree.t('webhook_mailer.endpoint_disabled.subject', endpoint_name: @endpoint.name || @endpoint.url),- store_url: @current_store.formatted_url- )+ with_store_locale(webhook_endpoint.store) do+ mail(+ to: @current_store.new_order_notifications_email.presence || @current_store.mail_from_address,+ from: from_address,+ subject: Spree.t('webhook_mailer.endpoint_disabled.subject', endpoint_name: @endpoint.name || @endpoint.url),+ store_url: @current_store.formatted_url+ )+ endendendenddiff --git a/spree/core/app/views/layouts/spree/base_mailer.html.erb b/spree/core/app/views/layouts/spree/base_mailer.html.erbindex bcbedb6691..2a67a074dc 100644--- a/spree/core/app/views/layouts/spree/base_mailer.html.erb+++ b/spree/core/app/views/layouts/spree/base_mailer.html.erb@@ -1,5 +1,5 @@<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">-<html>+<html lang="<%= I18n.locale %>"><head><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta name="x-apple-disable-message-reformatting"/>diff --git a/spree/core/lib/spree/core/previews/export_preview.rb b/spree/core/lib/spree/core/previews/export_preview.rbindex 95c22f9651..1e2bb6992d 100644--- a/spree/core/lib/spree/core/previews/export_preview.rb+++ b/spree/core/lib/spree/core/previews/export_preview.rb@@ -9,16 +9,17 @@ class Spree::ExportPreview < ActionMailer::Previewprivate# Reuse the most recent export, or build an in-memory example so the preview- # works on a database that has never run an export. The record is never saved,- # so no `export.created` side effects (attachment upload, generate job) fire.+ # works on a database that has never run an export. When the preview toolbar+ # requests a locale, always use the example so its store carries that locale.+ # The example is never saved, so no `export.created` side effects fire.def export- Spree::Export.last || example_export+ (locale.blank? && Spree::Export.last) || example_exportenddef example_exportexport = Spree::Exports::Products.new(id: 0,- store: Spree::Store.default,+ store: Spree::PreviewData.store(locale),user: Spree::PreviewData.admin_user,format: :csv)@@ -29,4 +30,8 @@ class Spree::ExportPreview < ActionMailer::Preview)exportend++ def locale+ @params[:locale]+ endenddiff --git a/spree/core/lib/spree/core/previews/invitation_preview.rb b/spree/core/lib/spree/core/previews/invitation_preview.rbindex 623ccb3c90..10d69a6170 100644--- a/spree/core/lib/spree/core/previews/invitation_preview.rb+++ b/spree/core/lib/spree/core/previews/invitation_preview.rb@@ -3,19 +3,20 @@ require_relative 'preview_data'# Preview Spree invitation emails at /rails/mailers/spree/invitationclass Spree::InvitationPreview < ActionMailer::Previewdef invitation_email- Spree::InvitationMailer.invitation_email(Spree::Invitation.pending.last || example_invitation)+ Spree::InvitationMailer.invitation_email((locale.blank? && Spree::Invitation.pending.last) || example_invitation)enddef invitation_accepted- Spree::InvitationMailer.invitation_accepted(Spree::Invitation.accepted.last || example_invitation(accepted: true))+ Spree::InvitationMailer.invitation_accepted((locale.blank? && Spree::Invitation.accepted.last) || example_invitation(accepted: true))endprivate# Build an in-memory invitation so the preview works on a database with no- # invitations. Never saved, so no records are created.+ # invitations. When the preview toolbar requests a locale, its store carries+ # that locale. Never saved, so no records are created.def example_invitation(accepted: false)- store = Spree::Store.default+ store = Spree::PreviewData.store(locale)admin = Spree::PreviewData.admin_userSpree::Invitation.new(id: 0,@@ -29,4 +30,8 @@ class Spree::InvitationPreview < ActionMailer::Previewexpires_at: 7.days.from_now)end++ def locale+ @params[:locale]+ endenddiff --git a/spree/core/lib/spree/core/previews/preview_data.rb b/spree/core/lib/spree/core/previews/preview_data.rbindex 2e5fc6dea1..9c4db5fd78 100644--- a/spree/core/lib/spree/core/previews/preview_data.rb+++ b/spree/core/lib/spree/core/previews/preview_data.rb@@ -1,5 +1,5 @@-# Shared fallbacks for ActionMailer previews. Everything returned here is-# in-memory (never saved) so opening a mailer preview stays read-only.+# Shared helpers for ActionMailer previews. Everything here is in-memory (never+# saved) so opening a mailer preview stays read-only.module Spreemodule PreviewDatamodule_function@@ -10,5 +10,23 @@ module SpreeSpree.admin_user_class.first ||Spree.admin_user_class.new(email: '[email protected]', first_name: 'Alex', last_name: 'Doe')end++ # The store previews render for. When the preview toolbar's locale dropdown+ # sets `?locale=`, return an unsaved copy of the default store with that+ # `default_locale`, so mailers that render in `store.default_locale` honour+ # the toggle without mutating the real store.+ #+ # @param locale [String, Symbol, nil] the `?locale=` param+ # @return [Spree::Store]+ def store(locale = nil)+ store = Spree::Store.default+ return store if locale.blank? || store.nil?++ dup = store.dup+ dup.id = store.id # keep prefixed-id / URL helpers working+ dup.default_locale = locale.to_s.downcase+ dup.readonly!+ dup+ endendenddiff --git a/spree/core/lib/spree/core/previews/report_preview.rb b/spree/core/lib/spree/core/previews/report_preview.rbindex cf704ae90f..d23d340778 100644--- a/spree/core/lib/spree/core/previews/report_preview.rb+++ b/spree/core/lib/spree/core/previews/report_preview.rb@@ -9,14 +9,15 @@ class Spree::ReportPreview < ActionMailer::Previewprivate# Reuse the most recent report, or build an in-memory example so the preview- # works on a database that has never run a report. The record is never saved,- # so no `report.created` side effects (attachment upload, generate job) fire.+ # works on a database that has never run a report. When the preview toolbar+ # requests a locale, always use the example so its store carries that locale.+ # The example is never saved, so no `report.created` side effects fire.def report- Spree::Report.last || example_report+ (locale.blank? && Spree::Report.last) || example_reportenddef example_report- store = Spree::Store.default+ store = Spree::PreviewData.store(locale)report = Spree::Reports::SalesTotal.new(id: 0,store: store,@@ -32,4 +33,8 @@ class Spree::ReportPreview < ActionMailer::Preview)reportend++ def locale+ @params[:locale]+ endenddiff --git a/spree/core/lib/spree/core/previews/webhook_preview.rb b/spree/core/lib/spree/core/previews/webhook_preview.rbindex 467bbd9242..0ddee6db8e 100644--- a/spree/core/lib/spree/core/previews/webhook_preview.rb+++ b/spree/core/lib/spree/core/previews/webhook_preview.rb@@ -1,3 +1,5 @@+require_relative 'preview_data'+# Preview Spree webhook notification emails at /rails/mailers/spree/webhookclass Spree::WebhookPreview < ActionMailer::Previewdef endpoint_disabled@@ -7,15 +9,16 @@ class Spree::WebhookPreview < ActionMailer::Previewprivate# Reuse the most recent endpoint, or build an in-memory disabled example so the- # preview works on a database with no webhook endpoints. Never saved, so the- # admin webhook list stays clean.+ # preview works on a database with no webhook endpoints. When the preview+ # toolbar requests a locale, always use the example so its store carries that+ # locale. Never saved, so the admin webhook list stays clean.def webhook_endpoint- Spree::WebhookEndpoint.last || example_endpoint+ (locale.blank? && Spree::WebhookEndpoint.last) || example_endpointenddef example_endpointSpree::WebhookEndpoint.new(- store: Spree::Store.default,+ store: Spree::PreviewData.store(locale),name: 'Example endpoint',url: 'https://example.com/webhooks/spree',active: false,@@ -24,4 +27,8 @@ class Spree::WebhookPreview < ActionMailer::Previewsubscriptions: ['order.completed'])end++ def locale+ @params[:locale]+ endenddiff --git a/spree/core/spec/mailers/spree/base_mailer_spec.rb b/spree/core/spec/mailers/spree/base_mailer_spec.rbnew file mode 100644index 0000000000..ca8052d832--- /dev/null+++ b/spree/core/spec/mailers/spree/base_mailer_spec.rb@@ -0,0 +1,75 @@+require 'spec_helper'++describe Spree::BaseMailer, type: :mailer do+ let!(:store) { @default_store }++ describe '#set_email_locale (deprecated)' do+ subject(:mailer) { described_class.new }++ before do+ store.update!(default_locale: 'de')+ allow(mailer).to receive(:current_store).and_return(store)+ end+ after { I18n.locale = :en }++ it 'emits a deprecation warning' do+ expect(Spree::Deprecation).to receive(:warn).with(/set_email_locale is deprecated/)+ mailer.set_email_locale+ end++ it 'still sets I18n.locale from the store default for backwards compatibility' do+ allow(Spree::Deprecation).to receive(:warn)+ I18n.locale = :en+ mailer.set_email_locale+ expect(I18n.locale).to eq(:de)+ end+ end++ describe '#with_store_locale' do+ subject(:mailer) { described_class.new }++ before do+ I18n.enforce_available_locales = false+ store.update!(name: 'Acme', default_locale: 'en')+ end+ after { I18n.enforce_available_locales = true }++ it 'renders the block in the given locale' do+ captured = nil+ mailer.with_store_locale(store, 'de') { captured = I18n.locale }+ expect(captured).to eq(:de)+ end++ # Store#name is translatable; without the store fallbacks active (as in a+ # background job) it returns nil under a non-default locale, blanking the+ # footer. `with_store_locale` activates those fallbacks like a request does.+ it 'activates store translation fallbacks so translatable attributes are not blank' do+ resolved = mailer.with_store_locale(store, 'de') { store.name }+ expect(resolved).to eq('Acme')+ end++ it 'restores the previous locale and fallbacks afterwards' do+ I18n.locale = :en+ previous_fallbacks = Mobility.store_based_fallbacks+ mailer.with_store_locale(store, 'de') { :noop }+ expect(I18n.locale).to eq(:en)+ expect(Mobility.store_based_fallbacks).to equal(previous_fallbacks)+ end++ it 'defaults to the store default locale when none is given' do+ store.update!(default_locale: 'fr')+ captured = nil+ mailer.with_store_locale(store) { captured = I18n.locale }+ expect(captured).to eq(:fr)+ end++ # Regression: the early no-locale return must not touch the thread's+ # fallbacks — clobbering them to nil crashes every later translated read+ # on the same (worker) thread.+ it 'leaves the fallbacks untouched when no locale can be resolved' do+ previous_fallbacks = Mobility.store_based_fallbacks+ mailer.with_store_locale(nil) { :noop }+ expect(Mobility.store_based_fallbacks).to equal(previous_fallbacks)+ end+ end+enddiff --git a/spree/emails/app/mailers/spree/newsletter_mailer.rb b/spree/emails/app/mailers/spree/newsletter_mailer.rbindex b612160cd0..d12b520948 100644--- a/spree/emails/app/mailers/spree/newsletter_mailer.rb+++ b/spree/emails/app/mailers/spree/newsletter_mailer.rb@@ -5,7 +5,9 @@ module Spreestore = subscriber.store || Spree::Current.store || Spree::Store.defaultbase_url = redirect_url.presence || store.storefront_url@confirm_email_url = append_token(base_url, @subscriber.verification_token)- mail(to: @subscriber.email, from: from_address, subject: Spree.t('newsletter_mailer.email_confirmation.subject'))+ with_store_locale(store) do+ mail(to: @subscriber.email, from: from_address, subject: Spree.t('newsletter_mailer.email_confirmation.subject'))+ endendprivatediff --git a/spree/emails/app/mailers/spree/order_mailer.rb b/spree/emails/app/mailers/spree/order_mailer.rbindex 9187aa0f4d..5749a80375 100644--- a/spree/emails/app/mailers/spree/order_mailer.rb+++ b/spree/emails/app/mailers/spree/order_mailer.rb@@ -5,24 +5,30 @@ module Spreedef confirm_email(order, resend = false)@order = order.respond_to?(:id) ? order : Spree::Order.find(order)current_store = @order.store- subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')- subject += "#{current_store.name} #{Spree.t('order_mailer.confirm_email.subject')} ##{@order.number}"- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ with_store_locale(current_store, @order.locale) do+ subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')+ subject += "#{current_store.name} #{Spree.t('order_mailer.confirm_email.subject')} ##{@order.number}"+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ endenddef store_owner_notification_email(order)@order = order.respond_to?(:id) ? order : Spree::Order.find(order)current_store = @order.store- subject = Spree.t('order_mailer.store_owner_notification_email.subject', store_name: current_store.name)- mail(to: current_store.new_order_notifications_email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ with_store_locale(current_store) do+ subject = Spree.t('order_mailer.store_owner_notification_email.subject', store_name: current_store.name)+ mail(to: current_store.new_order_notifications_email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ endenddef cancel_email(order, resend = false)@order = order.respond_to?(:id) ? order : Spree::Order.find(order)current_store = @order.store- subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')- subject += "#{current_store.name} #{Spree.t('order_mailer.cancel_email.subject')} ##{@order.number}"- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ with_store_locale(current_store, @order.locale) do+ subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')+ subject += "#{current_store.name} #{Spree.t('order_mailer.cancel_email.subject')} ##{@order.number}"+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ endenddef payment_link_email(order_id)@@ -30,8 +36,10 @@ module Spree@current_store = @order.store@checkout_payment_url = spree.checkout_state_url(@order.token, :payment, host: @current_store.storefront_url)- mail(to: @order.email, from: from_address, subject: Spree.t('order_mailer.payment_link_email.subject', number: @order.number),- store_url: @current_store.storefront_url, reply_to: reply_to_address)+ with_store_locale(@current_store, @order.locale) do+ mail(to: @order.email, from: from_address, subject: Spree.t('order_mailer.payment_link_email.subject', number: @order.number),+ store_url: @current_store.storefront_url, reply_to: reply_to_address)+ endendendenddiff --git a/spree/emails/app/mailers/spree/reimbursement_mailer.rb b/spree/emails/app/mailers/spree/reimbursement_mailer.rbindex c2b9337f8d..9036fd2eb9 100644--- a/spree/emails/app/mailers/spree/reimbursement_mailer.rb+++ b/spree/emails/app/mailers/spree/reimbursement_mailer.rb@@ -6,9 +6,11 @@ module Spree@reimbursement = reimbursement.respond_to?(:id) ? reimbursement : Spree::Reimbursement.find(reimbursement)@order = @reimbursement.ordercurrent_store = @reimbursement.store || Spree::Store.default- subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')- subject += "#{current_store.name} #{Spree.t('reimbursement_mailer.reimbursement_email.subject')} ##{@order.number}"- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ with_store_locale(current_store, @order.locale) do+ subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')+ subject += "#{current_store.name} #{Spree.t('reimbursement_mailer.reimbursement_email.subject')} ##{@order.number}"+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ endendendenddiff --git a/spree/emails/app/mailers/spree/shipment_mailer.rb b/spree/emails/app/mailers/spree/shipment_mailer.rbindex 49a2bb7af9..af4f3b4312 100644--- a/spree/emails/app/mailers/spree/shipment_mailer.rb+++ b/spree/emails/app/mailers/spree/shipment_mailer.rb@@ -7,9 +7,11 @@ module Spree@shipment = shipment.respond_to?(:id) ? shipment : Spree::Shipment.find(shipment)@order = @shipment.ordercurrent_store = @shipment.store- subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')- subject += "#{current_store.name} #{Spree.t('shipment_mailer.shipped_email.subject')} ##{@order.number}"- mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ with_store_locale(current_store, @order.locale) do+ subject = (resend ? "[#{Spree.t(:resend).upcase}] " : '')+ subject += "#{current_store.name} #{Spree.t('shipment_mailer.shipped_email.subject')} ##{@order.number}"+ mail(to: @order.email, from: from_address, subject: subject, store_url: current_store.storefront_url, reply_to: reply_to_address)+ endendendenddiff --git a/spree/emails/lib/spree/emails/previews/newsletter_preview.rb b/spree/emails/lib/spree/emails/previews/newsletter_preview.rbindex f48b86d86a..4cb76edf64 100644--- a/spree/emails/lib/spree/emails/previews/newsletter_preview.rb+++ b/spree/emails/lib/spree/emails/previews/newsletter_preview.rb@@ -1,20 +1,27 @@+require 'spree/core/previews/preview_data'+# Preview Spree newsletter emails at /rails/mailers/spree/newsletterclass Spree::NewsletterPreview < ActionMailer::Previewdef email_confirmation- subscriber = Spree::NewsletterSubscriber.unverified.last || example_subscriber+ subscriber = (locale.blank? && Spree::NewsletterSubscriber.unverified.last) || example_subscriberSpree::NewsletterMailer.email_confirmation(subscriber)endprivate# Build an in-memory subscriber so the preview works on a database with no- # newsletter subscribers. Never saved, so no records are created.+ # newsletter subscribers. When the preview toolbar requests a locale, its+ # store carries that locale. Never saved, so no records are created.def example_subscribersubscriber = Spree::NewsletterSubscriber.new(email: '[email protected]',- store: Spree::Store.default+ store: Spree::PreviewData.store(locale))subscriber.verification_token ||= 'preview-token'subscriberend++ def locale+ @params[:locale]+ endenddiff --git a/spree/emails/lib/spree/emails/previews/order_preview.rb b/spree/emails/lib/spree/emails/previews/order_preview.rbindex cffe4468ab..f1ab8fbfa0 100644--- a/spree/emails/lib/spree/emails/previews/order_preview.rb+++ b/spree/emails/lib/spree/emails/previews/order_preview.rb@@ -1,14 +1,28 @@# Preview Spree order emails at /rails/mailers/spree/orderclass Spree::OrderPreview < ActionMailer::Previewdef confirm_email- Spree::OrderMailer.confirm_email(Spree::Order.complete.last)+ Spree::OrderMailer.confirm_email(order)enddef cancel_email- Spree::OrderMailer.cancel_email(Spree::Order.complete.last)+ Spree::OrderMailer.cancel_email(order)enddef store_owner_notification_email- Spree::OrderMailer.store_owner_notification_email(Spree::Order.complete.last)+ Spree::OrderMailer.store_owner_notification_email(order)+ end++ private++ # The most recent complete order, with its locale overridden in memory when the+ # preview toolbar requests one (the change is never saved).+ def order+ order = Spree::Order.complete.last+ order.locale = locale if order && locale.present?+ order+ end++ def locale+ @params[:locale]&.downcaseendenddiff --git a/spree/emails/lib/spree/emails/previews/reimbursement_preview.rb b/spree/emails/lib/spree/emails/previews/reimbursement_preview.rbindex edbc03aeb0..006f6c9c23 100644--- a/spree/emails/lib/spree/emails/previews/reimbursement_preview.rb+++ b/spree/emails/lib/spree/emails/previews/reimbursement_preview.rb@@ -1,6 +1,14 @@# Preview Spree reimbursement emails at /rails/mailers/spree/reimbursementclass Spree::ReimbursementPreview < ActionMailer::Previewdef reimbursement_email- Spree::ReimbursementMailer.reimbursement_email(Spree::Reimbursement.last)+ reimbursement = Spree::Reimbursement.last+ reimbursement.order.locale = locale if reimbursement && locale.present?+ Spree::ReimbursementMailer.reimbursement_email(reimbursement)+ end++ private++ def locale+ @params[:locale]&.downcaseendenddiff --git a/spree/emails/lib/spree/emails/previews/shipment_preview.rb b/spree/emails/lib/spree/emails/previews/shipment_preview.rbindex f06eb301a0..f2b03fc093 100644--- a/spree/emails/lib/spree/emails/previews/shipment_preview.rb+++ b/spree/emails/lib/spree/emails/previews/shipment_preview.rb@@ -1,6 +1,14 @@# Preview Spree shipment emails at /rails/mailers/spree/shipmentclass Spree::ShipmentPreview < ActionMailer::Previewdef shipped_email- Spree::ShipmentMailer.shipped_email(Spree::Shipment.shipped.last)+ shipment = Spree::Shipment.shipped.last+ shipment.order.locale = locale if shipment && locale.present?+ Spree::ShipmentMailer.shipped_email(shipment)+ end++ private++ def locale+ @params[:locale]&.downcaseendenddiff --git a/spree/emails/spec/mailers/spree/order_mailer_spec.rb b/spree/emails/spec/mailers/spree/order_mailer_spec.rbindex a79e7af785..dbdd919846 100644--- a/spree/emails/spec/mailers/spree/order_mailer_spec.rb+++ b/spree/emails/spec/mailers/spree/order_mailer_spec.rb@@ -272,6 +272,28 @@ describe Spree::OrderMailer, type: :mailer docancel_email = described_class.cancel_email(order)expect(cancel_email).to have_body_text('Resumo da Pedido [CANCELADA]')end++ it 'translates the subject in the order locale' do+ I18n.backend.store_translations :'pt-BR', {+ spree: { order_mailer: { confirm_email: { subject: 'Confirmação do Pedido' } } }+ }+ confirmation_email = described_class.confirm_email(order)+ expect(confirmation_email.subject).to include('Confirmação do Pedido')+ end+ end++ context 'locale is not leaked after sending' do+ before do+ I18n.enforce_available_locales = false+ order.update_column(:locale, 'pt-BR')+ end++ after { I18n.enforce_available_locales = true }++ it 'restores the previous I18n.locale' do+ I18n.locale = :en+ expect { described_class.confirm_email(order).message }.not_to change(I18n, :locale).from(:en)+ endendcontext 'with preference :send_core_emails set to false' dodiff --git a/spree/emails/spec/mailers/spree/shipment_mailer_spec.rb b/spree/emails/spec/mailers/spree/shipment_mailer_spec.rbindex ef86e8ea6a..057fbc218b 100644--- a/spree/emails/spec/mailers/spree/shipment_mailer_spec.rb+++ b/spree/emails/spec/mailers/spree/shipment_mailer_spec.rb@@ -61,6 +61,14 @@ describe Spree::ShipmentMailer, type: :mailer doshipped_email = described_class.shipped_email(shipment)expect(shipped_email).to have_body_text('Caro Cliente,')end++ specify 'translates the subject in the order locale' do+ I18n.backend.store_translations :'pt-BR', {+ spree: { shipment_mailer: { shipped_email: { subject: 'Notificação de Envio' } } }+ }+ shipped_email = described_class.shipped_email(shipment)+ expect(shipped_email.subject).to include('Notificação de Envio')+ endendendend