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.

Fix: restoring a shared email template revision kept translation keys

Damian Legawiece0bc58bdcde3
diff --git a/spree/core/app/workflows/spree/email_templates/restore_revision.rb b/spree/core/app/workflows/spree/email_templates/restore_revision.rb
index 3464efbf43..1524cdda80 100644
--- a/spree/core/app/workflows/spree/email_templates/restore_revision.rb
+++ b/spree/core/app/workflows/spree/email_templates/restore_revision.rb
@@ -8,12 +8,26 @@ module Spree
def perform(revision:, actor: nil, locale: nil, draft_version: {})
super
template = revision.email_template
+ target_locale = locale.presence || template.locale
Spree.email_template_save_draft_workflow.call(
- store: template.store, key: template.key, locale: locale.presence || template.locale, actor: actor,
- attributes: { subject: revision.subject, body: revision.body, **draft_version.slice(:lock_version) }
+ store: template.store, key: template.key, locale: target_locale, actor: actor,
+ attributes: {
+ subject: readable(revision.subject, target_locale, escape: false),
+ body: readable(revision.body, target_locale),
+ **draft_version.slice(:lock_version)
+ }
)
end
+
+ private
+
+ # A shared revision restored into one language reads in that language's words.
+ def readable(text, target_locale, escape: true)
+ return text if target_locale == Spree::EmailTemplate::ANY_LOCALE
+
+ Spree::Emails::TranslationInliner.call(text, locale: target_locale, escape: escape)
+ end
end
end
end
diff --git a/spree/core/spec/workflows/spree/email_templates/restore_revision_spec.rb b/spree/core/spec/workflows/spree/email_templates/restore_revision_spec.rb
index 1644f6dd01..fb1233ae03 100644
--- a/spree/core/spec/workflows/spree/email_templates/restore_revision_spec.rb
+++ b/spree/core/spec/workflows/spree/email_templates/restore_revision_spec.rb
@@ -13,4 +13,15 @@ describe Spree::EmailTemplates::RestoreRevision do
expect(draft).to have_attributes(key: editable_key, subject: 'Old subject', body: 'Old body')
expect(template.reload.body).not_to eq('Old body')
end
+
+ it "writes a shared revision's translation keys out in the language it is restored into" do
+ keyed = create(:email_template_revision, email_template: template,
+ subject: "{{ 'admin_user_mailer.password_reset_email.subject' | t }}",
+ body: "<mj-text>{{ 'admin_user_mailer.password_reset_email.action' | t }}</mj-text>")
+
+ draft = described_class.new.call(revision: keyed, locale: 'en').value
+
+ expect(draft.subject).to eq(Spree.t('admin_user_mailer.password_reset_email.subject'))
+ expect(draft.body).to include(Spree.t('admin_user_mailer.password_reset_email.action'))
+ end
end