FIX: Make all email subject vars available in notification subjects (#11064)

A site owner attempting to use both the email_subject site setting and translation overrides for normal post notification
email subjects would find themselves frusturated at the lack of template argument parity.
Make all the variables available for translation overrides by adding the subject variables to the custom interpolation keys list and applying them.

Reported at https://meta.discourse.org/t/customize-subject-format-for-standard-emails/20801/47?u=riking
This commit is contained in:
Kane York 2020-11-02 20:00:11 -08:00 committed by GitHub
parent b9fb1cebcd
commit 789e3775df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 7 deletions

View File

@ -9,6 +9,13 @@ class TranslationOverride < ActiveRecord::Base
topic_title_url_encoded
site_title_url_encoded
context
site_name
optional_re
optional_pm
optional_cat
optional_tags
topic_title
}
}
include ActiveSupport::Deprecation::DeprecatedConstantAccessor

View File

@ -53,7 +53,15 @@ module Email
def subject
if @opts[:template] &&
TranslationOverride.exists?(locale: I18n.locale, translation_key: "#{@opts[:template]}.subject_template")
subject = I18n.t("#{@opts[:template]}.subject_template", @template_args)
augmented_template_args = @template_args.merge({
site_name: @template_args[:email_prefix],
optional_re: @opts[:add_re_to_subject] ? I18n.t('subject_re') : '',
optional_pm: @opts[:private_reply] ? @template_args[:subject_pm] : '',
optional_cat: @template_args[:show_category_in_subject] ? "[#{@template_args[:show_category_in_subject]}] " : '',
optional_tags: @template_args[:show_tags_in_subject] ? "#{@template_args[:show_tags_in_subject]} " : '',
topic_title: @template_args[:topic_title] ? @template_args[:topic_title] : '',
})
subject = I18n.t("#{@opts[:template]}.subject_template", augmented_template_args)
elsif @opts[:use_site_subject]
subject = String.new(SiteSetting.email_subject)
subject.gsub!("%{site_name}", @template_args[:email_prefix])

View File

@ -258,17 +258,28 @@ describe Email::MessageBuilder do
end
context "when use_site_subject is true" do
let(:templated_builder) { Email::MessageBuilder.new(to_address, template: 'mystery', use_site_subject: true) }
let(:templated_builder) { Email::MessageBuilder.new(to_address, template: 'user_notifications.user_replied', use_site_subject: true, topic_title: "Topic Title") }
it "can use subject override" do
override = TranslationOverride.create(
locale: I18n.locale,
translation_key: "mystery.subject_template",
value: "my customized subject"
override = TranslationOverride.upsert!(
I18n.locale,
"user_notifications.user_replied.subject_template",
"my customized subject"
)
I18n.expects(:t).with("mystery.subject_template", templated_builder.template_args).returns(override.value)
override.save!
expect(templated_builder.subject).to eq(override.value)
end
it "can use interpolation arguments in the override" do
SiteSetting.email_prefix = 'some email prefix'
override = TranslationOverride.upsert!(
I18n.locale,
"user_notifications.user_replied.subject_template",
"[%{site_name}] %{topic_title} my customized subject"
).save!
expect(templated_builder.subject).to match("some email prefix")
expect(templated_builder.subject).to match("customized subject")
end
end
end