Skip to content

Commit

Permalink
Merge branch 'pr/21129' of https://github.com/themsaid/framework into…
Browse files Browse the repository at this point in the history
… themsaid-pr/21129
  • Loading branch information
taylorotwell committed Feb 22, 2018
2 parents 5e3fe04 + 351c543 commit cf9945f
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Mail/MailServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ protected function registerIlluminateMailer()
$mailer->setQueue($app['queue']);
}

if ($app->bound('translator')) {
$mailer->setTranslator($app['translator']);
}

// Next we will set all of the global addresses on this mailer, which allows
// for easy unification of all "from" addresses as well as easy debugging
// of sent messages since they get be sent into a single email address.
Expand Down
39 changes: 32 additions & 7 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 @@ -51,6 +54,13 @@ class Mailable implements MailableContract, Renderable
*/
public $replyTo = [];

/**
* The locale of the message.
*
* @var string
*/
public $locale;

/**
* The subject of the message.
*
Expand Down Expand Up @@ -122,14 +132,16 @@ class Mailable implements MailableContract, Renderable
*/
public function send(MailerContract $mailer)
{
Container::getInstance()->call([$this, 'build']);
$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);
$mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
$this->buildFrom($message)
->buildRecipients($message)
->buildSubject($message)
->runCallbacks($message)
->buildAttachments($message);
});
});
}

Expand Down Expand Up @@ -572,6 +584,19 @@ protected function hasRecipient($address, $name = null, $property = 'to')
});
}

/**
* Set the locale of the message.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;

return $this;
}

/**
* Set the subject of the message.
*
Expand Down
32 changes: 32 additions & 0 deletions src/Illuminate/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Contracts\Queue\Factory as QueueContract;
use Illuminate\Contracts\Mail\Mailable as MailableContract;
use Illuminate\Contracts\Mail\MailQueue as MailQueueContract;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;

class Mailer implements MailerContract, MailQueueContract
{
Expand Down Expand Up @@ -68,6 +69,13 @@ class Mailer implements MailerContract, MailQueueContract
*/
protected $queue;

/**
* The translator implementation.
*
* @var \Illuminate\Contracts\Translation\Translator
*/
public $translator;

/**
* Array of failed recipients.
*
Expand Down Expand Up @@ -148,6 +156,17 @@ public function bcc($users)
return (new PendingMail($this))->bcc($users);
}

/**
* Begin the process of mailing a mailable class instance.
*
* @param string $locale
* @return \Illuminate\Mail\PendingMail
*/
public function locale($locale)
{
return (new PendingMail($this))->locale($locale);
}

/**
* Send a new message with only an HTML part.
*
Expand Down Expand Up @@ -566,4 +585,17 @@ public function setQueue(QueueContract $queue)

return $this;
}

/**
* Set the translator instance.
*
* @param \Illuminate\Contracts\Translation\Translator $translator
* @return $this
*/
public function setTranslator(TranslatorContract $translator)
{
$this->translator = $translator;

return $this;
}
}
23 changes: 22 additions & 1 deletion src/Illuminate/Mail/PendingMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class PendingMail
*/
protected $bcc = [];

/**
* The locale of the message.
*
* @var array
*/
protected $locale;

/**
* Create a new mailable mailer instance.
*
Expand Down Expand Up @@ -84,6 +91,19 @@ public function bcc($users)
return $this;
}

/**
* Set the locale of the message.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;

return $this;
}

/**
* Send a new mailable message instance.
*
Expand Down Expand Up @@ -149,6 +169,7 @@ protected function fill(Mailable $mailable)
{
return $mailable->to($this->to)
->cc($this->cc)
->bcc($this->bcc);
->bcc($this->bcc)
->locale($this->locale);
}
}
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);
}
}
}
11 changes: 11 additions & 0 deletions src/Illuminate/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,4 +476,15 @@ public function setFallback($fallback)
{
$this->fallback = $fallback;
}

/**
* Set loaded translation groups.
*
* @param string $fallback
* @return void
*/
public function setLoaded($loaded)
{
$this->loaded = $loaded;
}
}
1 change: 1 addition & 0 deletions tests/Integration/Mail/Fixtures/view.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{__('nom')}}
94 changes: 94 additions & 0 deletions tests/Integration/Mail/SendingMailWithLocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Illuminate\Tests\Integration\Mail\SendingMailWithLocale;

use Mockery;
use Illuminate\Mail\Mailable;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;

/**
* @group integration
*/
class SendingMailWithLocaleTest extends TestCase
{
public function tearDown()
{
parent::tearDown();

Mockery::close();
}

protected function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', 'true');

$app['config']->set('mail.driver', 'array');

$app['config']->set('app.locale', 'en');

View::addLocation(__DIR__.'/Fixtures');

app('translator')->setLoaded([
'*' => [
'*' => [
'en' => ['nom' => 'name'],
'ar' => ['nom' => 'esm'],
],
],
]);
}

public function setUp()
{
parent::setUp();
}

public function test_mail_is_sent_with_default_locale()
{
Mail::to('test@mail.com')->send(new TestMail());

$this->assertContains('name',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_mail_is_sent_with_selected_locale()
{
Mail::to('test@mail.com')->locale('ar')->send(new TestMail());

$this->assertContains('esm',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_set_back_to_default_after_mail_sent()
{
Mail::to('test@mail.com')->locale('ar')->send(new TestMail());
Mail::to('test@mail.com')->send(new TestMail());

$this->assertEquals('en', app('translator')->getLocale());

$this->assertContains('esm',
app('swift.transport')->messages()[0]->getBody()
);

$this->assertContains('name',
app('swift.transport')->messages()[1]->getBody()
);
}
}

class TestMail extends Mailable
{
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('view');
}
}

0 comments on commit cf9945f

Please sign in to comment.