Make emails translatable

closes #267
This commit is contained in:
Toby Zerner 2015-10-19 11:23:39 +10:30
parent f47be34df0
commit c0bf875890
6 changed files with 39 additions and 45 deletions

View File

@ -18,6 +18,7 @@ use Illuminate\Mail\Message;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Flarum\Core; use Flarum\Core;
use Flarum\Forum\UrlGenerator; use Flarum\Forum\UrlGenerator;
use Symfony\Component\Translation\TranslatorInterface;
class RequestPasswordResetHandler class RequestPasswordResetHandler
{ {
@ -41,18 +42,25 @@ class RequestPasswordResetHandler
*/ */
protected $url; protected $url;
/**
* @var TranslatorInterface
*/
protected $translator;
/** /**
* @param UserRepository $users * @param UserRepository $users
* @param SettingsRepository $settings * @param SettingsRepository $settings
* @param Mailer $mailer * @param Mailer $mailer
* @param UrlGenerator $url * @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->users = $users;
$this->settings = $settings; $this->settings = $settings;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->url = $url; $this->url = $url;
$this->translator = $translator;
} }
/** /**
@ -72,14 +80,16 @@ class RequestPasswordResetHandler
$token->save(); $token->save();
$data = [ $data = [
'username' => $user->username, '{username}' => $user->username,
'url' => $this->url->toRoute('resetPassword', ['token' => $token->id]), '{url}' => $this->url->toRoute('resetPassword', ['token' => $token->id]),
'forumTitle' => $this->settings->get('forum_title'), '{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->to($user->email);
$message->subject('Reset Your Password'); $message->subject('['.$data['{forum}'].'] '.$this->translator->trans('core.email.reset_password.subject'));
}); });
return $user; return $user;

View File

@ -20,6 +20,7 @@ use Flarum\Settings\SettingsRepository;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Mail\Mailer; use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Message; use Illuminate\Mail\Message;
use Symfony\Component\Translation\TranslatorInterface;
class EmailConfirmationMailer class EmailConfirmationMailer
{ {
@ -32,21 +33,29 @@ class EmailConfirmationMailer
* @var Mailer * @var Mailer
*/ */
protected $mailer; protected $mailer;
/** /**
* @var UrlGenerator * @var UrlGenerator
*/ */
private $url; protected $url;
/**
* @var TranslatorInterface
*/
protected $translator;
/** /**
* @param \Flarum\Settings\SettingsRepository $settings * @param \Flarum\Settings\SettingsRepository $settings
* @param Mailer $mailer * @param Mailer $mailer
* @param UrlGenerator $url * @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->settings = $settings;
$this->mailer = $mailer; $this->mailer = $mailer;
$this->url = $url; $this->url = $url;
$this->translator = $translator;
} }
/** /**
@ -71,9 +80,11 @@ class EmailConfirmationMailer
$data = $this->getEmailData($user, $user->email); $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->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; $email = $event->email;
$data = $this->getEmailData($event->user, $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->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 User $user
* @param string $email * @param string $email
*
* @return EmailToken * @return EmailToken
*/ */
protected function generateToken(User $user, $email) protected function generateToken(User $user, $email)
@ -110,20 +122,16 @@ class EmailConfirmationMailer
* *
* @param User $user * @param User $user
* @param string $email * @param string $email
*
* @return array * @return array
*/ */
protected function getEmailData(User $user, $email) protected function getEmailData(User $user, $email)
{ {
$token = $this->generateToken($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 [ return [
'username' => $user->username, '{username}' => $user->username,
'url' => $this->url->toRoute('confirmEmail', ['token' => $token->id]), '{url}' => $this->url->toRoute('confirmEmail', ['token' => $token->id]),
'forumTitle' => $this->settings->get('forum_title') '{forum}' => $this->settings->get('forum_title')
]; ];
} }
} }

View File

@ -34,8 +34,8 @@ class ResetPasswordController extends AbstractHtmlController
/** /**
* @param Request $request * @param Request $request
* @param array $routeParams
* @return \Illuminate\Contracts\View\View * @return \Illuminate\Contracts\View\View
* @throws InvalidConfirmationTokenException
*/ */
public function render(Request $request) public function render(Request $request)
{ {

View File

@ -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.

View File

@ -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.

View File

@ -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.