From 351c543fc76bf869f04b49ddbe5e01b04f4d3e0e Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 19 Feb 2018 19:25:40 +0200 Subject: [PATCH] user trait --- src/Illuminate/Mail/Mailable.php | 25 ++++++--------- src/Illuminate/Support/Traits/Localizable.php | 31 +++++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 src/Illuminate/Support/Traits/Localizable.php diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index df7ba9b8c7a7..a492ba61c148 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -9,6 +9,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\HtmlString; use Illuminate\Container\Container; +use Illuminate\Support\Traits\Localizable; use Illuminate\Contracts\Support\Renderable; use Illuminate\Contracts\Queue\Factory as Queue; use Illuminate\Contracts\Mail\Mailer as MailerContract; @@ -16,6 +17,8 @@ class Mailable implements MailableContract, Renderable { + use Localizable; + /** * The person the message is from. * @@ -129,27 +132,17 @@ class Mailable implements MailableContract, Renderable */ public function send(MailerContract $mailer) { - $currentLocale = $mailer->translator ? $mailer->translator->getLocale() : null; - - try { - if ($mailer->translator && $this->locale) { - $mailer->translator->setLocale($this->locale); - } - + $this->withLocale($this->locale, $mailer->translator, function () use ($mailer) { Container::getInstance()->call([$this, 'build']); $mailer->send($this->buildView(), $this->buildViewData(), function ($message) { $this->buildFrom($message) - ->buildRecipients($message) - ->buildSubject($message) - ->runCallbacks($message) - ->buildAttachments($message); + ->buildRecipients($message) + ->buildSubject($message) + ->runCallbacks($message) + ->buildAttachments($message); }); - } finally { - if ($mailer->translator) { - $mailer->translator->setLocale($currentLocale); - } - } + }); } /** diff --git a/src/Illuminate/Support/Traits/Localizable.php b/src/Illuminate/Support/Traits/Localizable.php new file mode 100644 index 000000000000..b26996200244 --- /dev/null +++ b/src/Illuminate/Support/Traits/Localizable.php @@ -0,0 +1,31 @@ +getLocale(); + + try { + $translator->setLocale($locale); + + return $callback(); + } finally { + $translator->setLocale($original); + } + } +}