Use Rails to translate email subject

And translate the subject with the user's locale.
If the subject doesn't need parameters, you don't need to supply it.
Rails finds the translation automatically.
This commit is contained in:
Maikel Linke
2026-02-06 10:23:17 +11:00
parent c66b9611b6
commit 69108df206
4 changed files with 14 additions and 18 deletions

View File

@@ -6,11 +6,9 @@ class PaymentMailer < ApplicationMailer
def authorize_payment(payment)
@payment = payment
@order = @payment.order
subject = I18n.t("payment_mailer.authorize_payment.subject",
distributor: @order.distributor.name)
I18n.with_locale valid_locale(@order.user) do
mail(to: @order.email,
subject:,
subject: default_i18n_subject(distributor: @order.distributor.name),
reply_to: @order.distributor.contact.email)
end
end
@@ -18,24 +16,19 @@ class PaymentMailer < ApplicationMailer
def authorization_required(payment)
@order = payment.order
shop_owner = @order.distributor.owner
subject = I18n.t("payment_mailer.authorization_required.subject",
order: @order)
I18n.with_locale valid_locale(shop_owner) do
mail(to: shop_owner.email,
subject:,
reply_to: @order.email)
mail(to: shop_owner.email, reply_to: @order.email)
end
end
def refund_available(payment, taler_order_status_url)
@order = payment.order
@shop = @order.distributor.name
@taler_order_status_url = taler_order_status_url
subject = I18n.t("payment_mailer.refund_available.subject",
order: @order)
I18n.with_locale valid_locale(@order.user) do
mail(to: @order.email,
subject:,
subject: default_i18n_subject(shop: @shop),
reply_to: @order.email)
end
end

View File

@@ -1,2 +1,2 @@
%p= t(".message", order_number: @order.number)
%p= t(".message", shop: @shop)
%p= link_to @taler_order_status_url, @taler_order_status_url

View File

@@ -466,8 +466,10 @@ en:
subject: "A payment requires authorization from the customer"
message: "A payment for order %{order_number} requires additional authorization from the customer. The customer has been notified via email and the payment will appear as pending until it is authorized."
refund_available:
subject: "Refund available"
message: "Your payment for order %{order_number} is being refunded. Claim your refund following the link below."
subject: "Refund from %{shop}"
message: |
Your payment to %{shop} is being refunded.
Accept your refund following the link below.
producer_mailer:
order_cycle:
subject: "Order cycle report for %{producer}"

View File

@@ -2,7 +2,6 @@
RSpec.describe PaymentMailer do
describe '#payment_mailer' do
let(:enterprise) { create(:enterprise) }
let(:payment_method) {
create(:payment_method, distributors: [order.distributor])
}
@@ -15,7 +14,8 @@ RSpec.describe PaymentMailer do
subject(:email) { described_class.authorize_payment(payment) }
it "includes the distributor's name in the subject" do
expect(email.subject).to include("authorize your payment to #{order.distributor.name}")
order.distributor.name = "Fennel Farmer"
expect(email.subject).to include("authorize your payment to Fennel Farmer")
end
it "sets a reply-to of the customer email" do
@@ -44,11 +44,12 @@ RSpec.describe PaymentMailer do
describe "#refund_available" do
it "tells the user to accept a refund" do
payment = create(:payment)
payment.order.distributor = create(:enterprise, name: "Carrot Castle")
link = "https://taler.example.com/order/1"
mail = PaymentMailer.refund_available(payment, link)
expect(mail.subject).to eq "Refund available"
expect(mail.body).to match "Claim your refund following the link below."
expect(mail.subject).to eq "Refund from Carrot Castle"
expect(mail.body).to match "Your payment to Carrot Castle is being refunded."
expect(mail.body).to match link
end
end