1
0
mirror of https://github.com/flarum/core.git synced 2025-08-03 06:57:54 +02:00

fix: set translator locale to user preference for email notifications (#3525)

This commit is contained in:
Ian Morland
2022-07-14 12:02:55 +01:00
committed by GitHub
parent 759f7ef327
commit 16f59f514b

View File

@@ -9,6 +9,7 @@
namespace Flarum\Notification; namespace Flarum\Notification;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User; use Flarum\User\User;
use Illuminate\Contracts\Mail\Mailer; use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Message; use Illuminate\Mail\Message;
@@ -26,14 +27,21 @@ class NotificationMailer
*/ */
protected $translator; protected $translator;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
/** /**
* @param Mailer $mailer * @param Mailer $mailer
* @param TranslatorInterface $translator * @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->mailer = $mailer;
$this->translator = $translator; $this->translator = $translator;
$this->settings = $settings;
} }
/** /**
@@ -42,6 +50,10 @@ class NotificationMailer
*/ */
public function send(MailableInterface $blueprint, User $user) 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( $this->mailer->send(
$blueprint->getEmailView(), $blueprint->getEmailView(),
compact('blueprint', 'user'), compact('blueprint', 'user'),