Skip to content

Commit

Permalink
user trait
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Feb 19, 2018
1 parent 6b21a75 commit 351c543
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
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;
use Illuminate\Contracts\Mail\Mailable as MailableContract;

class Mailable implements MailableContract, Renderable
{
use Localizable;

/**
* The person the message is from.
*
Expand Down Expand Up @@ -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);
}
}
});
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Illuminate/Support/Traits/Localizable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Illuminate\Support\Traits;

trait Localizable
{
/**
* Run the callback with the given locale.
*
* @param string $locale
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Closure $callback
* @return bool
*/
public function withLocale($locale, $translator, $callback)
{
if (! $locale || ! $translator) {
return $callback();
}

$original = $translator->getLocale();

try {
$translator->setLocale($locale);

return $callback();
} finally {
$translator->setLocale($original);
}
}
}

0 comments on commit 351c543

Please sign in to comment.