From 16f59f514bc4409f916d6b2dd351d40b3b3fa344 Mon Sep 17 00:00:00 2001 From: Ian Morland <16573496+imorland@users.noreply.github.com> Date: Thu, 14 Jul 2022 12:02:55 +0100 Subject: [PATCH] fix: set translator locale to user preference for email notifications (#3525) --- .../core/src/Notification/NotificationMailer.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/framework/core/src/Notification/NotificationMailer.php b/framework/core/src/Notification/NotificationMailer.php index d5bd5dff1..5bdcbca6c 100644 --- a/framework/core/src/Notification/NotificationMailer.php +++ b/framework/core/src/Notification/NotificationMailer.php @@ -9,6 +9,7 @@ namespace Flarum\Notification; +use Flarum\Settings\SettingsRepositoryInterface; use Flarum\User\User; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Mail\Message; @@ -26,14 +27,21 @@ class NotificationMailer */ protected $translator; + /** + * @var SettingsRepositoryInterface + */ + protected $settings; + /** * @param Mailer $mailer * @param TranslatorInterface $translator + * @param SettingsRepositoryInterface $settings */ - public function __construct(Mailer $mailer, TranslatorInterface $translator) + public function __construct(Mailer $mailer, TranslatorInterface $translator, SettingsRepositoryInterface $settings) { $this->mailer = $mailer; $this->translator = $translator; + $this->settings = $settings; } /** @@ -42,6 +50,10 @@ class NotificationMailer */ public function send(MailableInterface $blueprint, User $user) { + // Ensure that notifications are delivered to the user in their default language, if they've selected one. + // If the selected locale is no longer available, the forum default will be used instead. + $this->translator->setLocale($user->getPreference('locale') ?? $this->settings->get('default_locale')); + $this->mailer->send( $blueprint->getEmailView(), compact('blueprint', 'user'),