From 8adcb742ba8e6df61a28532b17b99081ec03d356 Mon Sep 17 00:00:00 2001 From: Christophe Bliard Date: Fri, 28 Jan 2022 15:10:37 +0100 Subject: [PATCH] display membership emails in the language of the recipient --- app/mailers/member_mailer.rb | 4 ++-- spec/mailers/member_mailer_spec.rb | 33 ++++++++++++++++++------------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/app/mailers/member_mailer.rb b/app/mailers/member_mailer.rb index 47b3a23575..8e77c02dcd 100644 --- a/app/mailers/member_mailer.rb +++ b/app/mailers/member_mailer.rb @@ -78,8 +78,8 @@ class MemberMailer < ApplicationMailer end def send_mail(current_user, member, subject, message) - in_member_locale(member) do - User.execute_as(current_user) do + User.execute_as(current_user) do + in_member_locale(member) do message_id member, current_user @roles = member.roles diff --git a/spec/mailers/member_mailer_spec.rb b/spec/mailers/member_mailer_spec.rb index 154262b087..7e0d763f3c 100644 --- a/spec/mailers/member_mailer_spec.rb +++ b/spec/mailers/member_mailer_spec.rb @@ -101,7 +101,12 @@ describe MemberMailer, type: :mailer do shared_examples_for 'has the expected body' do let(:body) { subject.body.parts.detect { |part| part['Content-Type'].value == 'text/html' }.body.to_s } - + let(:i18n_params) do + { + project: project ? link_to_project(project, only_path: false) : nil, + user: link_to_user(current_user, only_path: false) + }.compact + end it 'highlights the roles received' do expected = <<~MSG @@ -116,17 +121,24 @@ describe MemberMailer, type: :mailer do .at_path('body/table/tr/td/ul') end + context 'when current user and principal have different locales' do + let(:principal) { FactoryBot.build_stubbed(:user, language: 'fr') } + let(:current_user) { FactoryBot.build_stubbed(:user, language: 'de') } + + it 'is in the locale of the recipient' do + OpenProject::LocaleHelper.with_locale_for(principal) do + i18n_params + end + expect(body).to include(I18n.t(:"#{expected_header}.without_message", locale: :fr, **i18n_params)) + end + end + context 'with a custom message' do let(:message) { "Some **styled** message" } it 'has the expected header' do - params = { - project: project ? link_to_project(project, only_path: false) : nil, - user: link_to_user(current_user, only_path: false) - }.compact - expect(body) - .to include(I18n.t(:"#{expected_header}.with_message", **params)) + .to include(I18n.t(:"#{expected_header}.with_message", **i18n_params)) end it 'includes the custom message' do @@ -138,13 +150,8 @@ describe MemberMailer, type: :mailer do context 'without a custom message' do it 'has the expected header' do - params = { - project: project ? link_to_project(project, only_path: false) : nil, - user: link_to_user(current_user, only_path: false) - }.compact - expect(body) - .to include(I18n.t(:"#{expected_header}.without_message", **params)) + .to include(I18n.t(:"#{expected_header}.without_message", **i18n_params)) end end end