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.

support preferred locale for admin mailers

Damian Legawiec434b2446b036
diff --git a/spree/core/app/mailers/spree/admin_user_mailer.rb b/spree/core/app/mailers/spree/admin_user_mailer.rb
index fe78bb9d19..2191b6875d 100644
--- a/spree/core/app/mailers/spree/admin_user_mailer.rb
+++ b/spree/core/app/mailers/spree/admin_user_mailer.rb
@@ -8,7 +8,7 @@ module Spree
@current_store = store
@reset_url = password_reset_url(token, store, redirect_url)
- with_store_locale(store) do
+ with_store_locale(store, preferred_locale(admin_user, store)) do
mail(
to: admin_user.email,
from: from_address,
@@ -24,7 +24,7 @@ module Spree
@current_store = store
@confirmation_url = confirmation_url(token, store)
- with_store_locale(store) do
+ with_store_locale(store, preferred_locale(admin_user, store)) do
mail(
to: admin_user.email,
from: from_address,
@@ -37,6 +37,20 @@ module Spree
private
+ # Locale chain for admin auth emails:
+ # the admin's own dashboard language (persisted by the profile/language
+ # switcher via `PATCH /api/v3/admin/me`) → the store's configured admin
+ # locale → nil, which lets with_store_locale fall back to the store's
+ # default (storefront) locale. Blank or unavailable values fall through.
+ def preferred_locale(admin_user, store)
+ [admin_user.try(:selected_locale), store&.preferred_admin_locale]
+ .find { |locale| available_locale?(locale) }
+ end
+
+ def available_locale?(locale)
+ locale.present? && I18n.available_locales.map(&:to_s).include?(locale.to_s)
+ end
+
# The dashboard SPA passes a validated redirect URL (token appended as a
# query param); the legacy Devise flow links to its own edit-password route
# when the host app installed it.
diff --git a/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb b/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb
index cf2b00a1f7..991c6205b0 100644
--- a/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb
+++ b/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb
@@ -24,6 +24,63 @@ describe Spree::AdminUserMailer, type: :mailer do
expect(message.body.encoded).to include("https://admin.example.com/reset-password?token=#{token}")
end
+
+ context 'when the admin has a dashboard language set' do
+ around do |example|
+ previous = I18n.available_locales
+ I18n.available_locales = previous | [:pl]
+ I18n.backend.store_translations(
+ :pl,
+ spree: { admin_user_mailer: { password_reset_email: { subject: 'Instrukcja resetu hasła' } } }
+ )
+ example.run
+ ensure
+ I18n.available_locales = previous
+ I18n.backend.reload!
+ end
+
+ # @default_store is shared across the suite — examples that set its
+ # admin locale must put it back or they leak into later examples.
+ after do
+ store.preferred_admin_locale = nil
+ store.save!
+ end
+
+ it 'renders in the admin selected locale instead of the store default' do
+ admin_user.update!(selected_locale: 'pl')
+
+ message = described_class.password_reset_email(admin_user, token, store)
+
+ expect(message.subject).to eq("#{store.name} Instrukcja resetu hasła")
+ end
+
+ it 'falls back to the store admin locale when the admin has none' do
+ store.preferred_admin_locale = 'pl'
+ store.save!
+
+ message = described_class.password_reset_email(admin_user, token, store)
+
+ expect(message.subject).to eq("#{store.name} Instrukcja resetu hasła")
+ end
+
+ it 'prefers the admin selected locale over the store admin locale' do
+ store.preferred_admin_locale = 'pl'
+ store.save!
+ admin_user.update!(selected_locale: 'en')
+
+ message = described_class.password_reset_email(admin_user, token, store)
+
+ expect(message.subject).to eq("#{store.name} #{Spree.t('admin_user_mailer.password_reset_email.subject', locale: :en)}")
+ end
+
+ it 'falls back to the store default locale for blank or unavailable values' do
+ admin_user.update!(selected_locale: 'xx')
+
+ message = described_class.password_reset_email(admin_user, token, store)
+
+ expect(message.subject).to eq("#{store.name} #{Spree.t('admin_user_mailer.password_reset_email.subject')}")
+ end
+ end
end
describe '#confirmation_email' do