From 559958f8c540a28c9f41da040fa23a228fadad0b Mon Sep 17 00:00:00 2001
From: Claire <claire.github-309c@sitedethib.com>
Date: Wed, 4 Sep 2024 19:35:40 +0200
Subject: [PATCH] Fix email language when recipient has no selected locale
 (#31747)

---
 app/mailers/application_mailer.rb |  2 +-
 app/mailers/user_mailer.rb        | 24 ++++++++++++------------
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb
index 3312183d47..35f0b5fee1 100644
--- a/app/mailers/application_mailer.rb
+++ b/app/mailers/application_mailer.rb
@@ -12,7 +12,7 @@ class ApplicationMailer < ActionMailer::Base
   protected
 
   def locale_for_account(account, &block)
-    I18n.with_locale(account.user_locale || I18n.locale || I18n.default_locale, &block)
+    I18n.with_locale(account.user_locale || I18n.default_locale, &block)
   end
 
   def set_autoreply_headers!
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 81a2c0c6d0..5c9e5c96d9 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -33,7 +33,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -43,7 +43,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -53,7 +53,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -63,7 +63,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -73,7 +73,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -83,7 +83,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -93,7 +93,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -103,7 +103,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: default_devise_subject
     end
   end
@@ -114,7 +114,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: I18n.t('devise.mailer.webauthn_credential.added.subject')
     end
   end
@@ -125,7 +125,7 @@ class UserMailer < Devise::Mailer
 
     return unless @resource.active_for_authentication?
 
-    I18n.with_locale(locale) do
+    I18n.with_locale(locale(use_current_locale: true)) do
       mail subject: I18n.t('devise.mailer.webauthn_credential.deleted.subject')
     end
   end
@@ -219,7 +219,7 @@ class UserMailer < Devise::Mailer
     @instance = Rails.configuration.x.local_domain
   end
 
-  def locale
-    @resource.locale.presence || I18n.locale || I18n.default_locale
+  def locale(use_current_locale: false)
+    @resource.locale.presence || (use_current_locale && I18n.locale) || I18n.default_locale
   end
 end