From c0bf8758900271cf2933c85bcd12b11d054acab0 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 19 Oct 2015 11:23:39 +1030 Subject: [PATCH] Make emails translatable closes #267 --- .../Command/RequestPasswordResetHandler.php | 22 ++++++++---- .../Core/Listener/EmailConfirmationMailer.php | 36 +++++++++++-------- .../Controller/ResetPasswordController.php | 2 +- .../views/emails/activateAccount.blade.php | 8 ----- .../core/views/emails/confirmEmail.blade.php | 8 ----- .../core/views/emails/resetPassword.blade.php | 8 ----- 6 files changed, 39 insertions(+), 45 deletions(-) delete mode 100644 framework/core/views/emails/activateAccount.blade.php delete mode 100644 framework/core/views/emails/confirmEmail.blade.php delete mode 100644 framework/core/views/emails/resetPassword.blade.php diff --git a/framework/core/src/Core/Command/RequestPasswordResetHandler.php b/framework/core/src/Core/Command/RequestPasswordResetHandler.php index e71f2e073..93598a135 100644 --- a/framework/core/src/Core/Command/RequestPasswordResetHandler.php +++ b/framework/core/src/Core/Command/RequestPasswordResetHandler.php @@ -18,6 +18,7 @@ use Illuminate\Mail\Message; use Illuminate\Database\Eloquent\ModelNotFoundException; use Flarum\Core; use Flarum\Forum\UrlGenerator; +use Symfony\Component\Translation\TranslatorInterface; class RequestPasswordResetHandler { @@ -41,18 +42,25 @@ class RequestPasswordResetHandler */ protected $url; + /** + * @var TranslatorInterface + */ + protected $translator; + /** * @param UserRepository $users * @param SettingsRepository $settings * @param Mailer $mailer * @param UrlGenerator $url + * @param TranslatorInterface $translator */ - public function __construct(UserRepository $users, SettingsRepository $settings, Mailer $mailer, UrlGenerator $url) + public function __construct(UserRepository $users, SettingsRepository $settings, Mailer $mailer, UrlGenerator $url, TranslatorInterface $translator) { $this->users = $users; $this->settings = $settings; $this->mailer = $mailer; $this->url = $url; + $this->translator = $translator; } /** @@ -72,14 +80,16 @@ class RequestPasswordResetHandler $token->save(); $data = [ - 'username' => $user->username, - 'url' => $this->url->toRoute('resetPassword', ['token' => $token->id]), - 'forumTitle' => $this->settings->get('forum_title'), + '{username}' => $user->username, + '{url}' => $this->url->toRoute('resetPassword', ['token' => $token->id]), + '{forum}' => $this->settings->get('forum_title'), ]; - $this->mailer->send(['text' => 'flarum::emails.resetPassword'], $data, function (Message $message) use ($user) { + $body = $this->translator->trans('core.email.reset_password.body', $data); + + $this->mailer->raw($body, function (Message $message) use ($user, $data) { $message->to($user->email); - $message->subject('Reset Your Password'); + $message->subject('['.$data['{forum}'].'] '.$this->translator->trans('core.email.reset_password.subject')); }); return $user; diff --git a/framework/core/src/Core/Listener/EmailConfirmationMailer.php b/framework/core/src/Core/Listener/EmailConfirmationMailer.php index a79b84d5c..d34f282ec 100755 --- a/framework/core/src/Core/Listener/EmailConfirmationMailer.php +++ b/framework/core/src/Core/Listener/EmailConfirmationMailer.php @@ -20,6 +20,7 @@ use Flarum\Settings\SettingsRepository; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Mail\Mailer; use Illuminate\Mail\Message; +use Symfony\Component\Translation\TranslatorInterface; class EmailConfirmationMailer { @@ -32,21 +33,29 @@ class EmailConfirmationMailer * @var Mailer */ protected $mailer; + /** * @var UrlGenerator */ - private $url; + protected $url; + + /** + * @var TranslatorInterface + */ + protected $translator; /** * @param \Flarum\Settings\SettingsRepository $settings * @param Mailer $mailer * @param UrlGenerator $url + * @param TranslatorInterface $translator */ - public function __construct(SettingsRepository $settings, Mailer $mailer, UrlGenerator $url) + public function __construct(SettingsRepository $settings, Mailer $mailer, UrlGenerator $url, TranslatorInterface $translator) { $this->settings = $settings; $this->mailer = $mailer; $this->url = $url; + $this->translator = $translator; } /** @@ -71,9 +80,11 @@ class EmailConfirmationMailer $data = $this->getEmailData($user, $user->email); - $this->mailer->send(['text' => 'flarum::emails.activateAccount'], $data, function (Message $message) use ($user) { + $body = $this->translator->trans('core.email.activate_account.body', $data); + + $this->mailer->raw($body, function (Message $message) use ($user, $data) { $message->to($user->email); - $message->subject('Activate Your New Account'); + $message->subject('['.$data['{forum}'].'] '.$this->translator->trans('core.email.activate_account.subject')); }); } @@ -85,16 +96,17 @@ class EmailConfirmationMailer $email = $event->email; $data = $this->getEmailData($event->user, $email); - $this->mailer->send(['text' => 'flarum::emails.confirmEmail'], $data, function (Message $message) use ($email) { + $body = $this->translator->trans('core.email.confirm_email.body', $data); + + $this->mailer->raw($body, function (Message $message) use ($email, $data) { $message->to($email); - $message->subject('Confirm Your New Email Address'); + $message->subject('['.$data['{forum}'].'] '.$this->translator->trans('core.email.confirm_email.subject')); }); } /** * @param User $user * @param string $email - * * @return EmailToken */ protected function generateToken(User $user, $email) @@ -110,20 +122,16 @@ class EmailConfirmationMailer * * @param User $user * @param string $email - * * @return array */ protected function getEmailData(User $user, $email) { $token = $this->generateToken($user, $email); - // TODO: Need to use AbstractUrlGenerator, but since this is part of core we - // don't know that the forum routes will be loaded. Should the confirm - // email route be part of core?? return [ - 'username' => $user->username, - 'url' => $this->url->toRoute('confirmEmail', ['token' => $token->id]), - 'forumTitle' => $this->settings->get('forum_title') + '{username}' => $user->username, + '{url}' => $this->url->toRoute('confirmEmail', ['token' => $token->id]), + '{forum}' => $this->settings->get('forum_title') ]; } } diff --git a/framework/core/src/Forum/Controller/ResetPasswordController.php b/framework/core/src/Forum/Controller/ResetPasswordController.php index 88345e9d0..07106624b 100644 --- a/framework/core/src/Forum/Controller/ResetPasswordController.php +++ b/framework/core/src/Forum/Controller/ResetPasswordController.php @@ -34,8 +34,8 @@ class ResetPasswordController extends AbstractHtmlController /** * @param Request $request - * @param array $routeParams * @return \Illuminate\Contracts\View\View + * @throws InvalidConfirmationTokenException */ public function render(Request $request) { diff --git a/framework/core/views/emails/activateAccount.blade.php b/framework/core/views/emails/activateAccount.blade.php deleted file mode 100644 index 10a235f77..000000000 --- a/framework/core/views/emails/activateAccount.blade.php +++ /dev/null @@ -1,8 +0,0 @@ -Hey {{ $username }}! - -Someone (hopefully you!) has signed up to {{ $forumTitle }} with this email address. - -If this was you, simply click the following link and your account will be activated: -{{ $url }} - -If you did not sign up, please ignore this email. diff --git a/framework/core/views/emails/confirmEmail.blade.php b/framework/core/views/emails/confirmEmail.blade.php deleted file mode 100644 index 667366ad3..000000000 --- a/framework/core/views/emails/confirmEmail.blade.php +++ /dev/null @@ -1,8 +0,0 @@ -Hey {{ $username }}! - -Someone (hopefully you!) has changed their email address on {{ $forumTitle }} to this one. - -If this was you, simply click the following link and your email will be confirmed: -{{ $url }} - -If this was not you, please ignore this email. diff --git a/framework/core/views/emails/resetPassword.blade.php b/framework/core/views/emails/resetPassword.blade.php deleted file mode 100644 index 1f808d99e..000000000 --- a/framework/core/views/emails/resetPassword.blade.php +++ /dev/null @@ -1,8 +0,0 @@ -Hey {{ $username }}! - -Someone (hopefully you!) has submitted a forgotten password request for your account on the {{ $forumTitle }}. - -If this was you, click the following link to reset your password: -{{ $url }} - -If you do not wish to change your password, just ignore this email and nothing will happen.