From f66340f845671c636b6d49a195f3694069a4bfe4 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 15 Feb 2018 19:32:14 +0200 Subject: [PATCH 1/3] ability to set locale on sent Mailable --- src/Illuminate/Mail/MailServiceProvider.php | 4 + src/Illuminate/Mail/Mailable.php | 48 ++++++++-- src/Illuminate/Mail/Mailer.php | 32 +++++++ src/Illuminate/Mail/PendingMail.php | 23 ++++- src/Illuminate/Translation/Translator.php | 11 +++ .../Integration/Mail/Fixtures/view.blade.php | 1 + .../Mail/SendingMailWithLocaleTest.php | 95 +++++++++++++++++++ 7 files changed, 205 insertions(+), 9 deletions(-) create mode 100644 tests/Integration/Mail/Fixtures/view.blade.php create mode 100644 tests/Integration/Mail/SendingMailWithLocaleTest.php diff --git a/src/Illuminate/Mail/MailServiceProvider.php b/src/Illuminate/Mail/MailServiceProvider.php index e185c3796cff..a81426e29a6b 100755 --- a/src/Illuminate/Mail/MailServiceProvider.php +++ b/src/Illuminate/Mail/MailServiceProvider.php @@ -51,6 +51,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. diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 5a412c90a900..df7ba9b8c7a7 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -51,6 +51,13 @@ class Mailable implements MailableContract, Renderable */ public $replyTo = []; + /** + * The locale of the message. + * + * @var string + */ + public $locale; + /** * The subject of the message. * @@ -122,15 +129,27 @@ class Mailable implements MailableContract, Renderable */ public function send(MailerContract $mailer) { - Container::getInstance()->call([$this, 'build']); + $currentLocale = $mailer->translator ? $mailer->translator->getLocale() : null; - $mailer->send($this->buildView(), $this->buildViewData(), function ($message) { - $this->buildFrom($message) - ->buildRecipients($message) - ->buildSubject($message) - ->runCallbacks($message) - ->buildAttachments($message); - }); + try { + if ($mailer->translator && $this->locale) { + $mailer->translator->setLocale($this->locale); + } + + Container::getInstance()->call([$this, 'build']); + + $mailer->send($this->buildView(), $this->buildViewData(), function ($message) { + $this->buildFrom($message) + ->buildRecipients($message) + ->buildSubject($message) + ->runCallbacks($message) + ->buildAttachments($message); + }); + } finally { + if ($mailer->translator) { + $mailer->translator->setLocale($currentLocale); + } + } } /** @@ -572,6 +591,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. * diff --git a/src/Illuminate/Mail/Mailer.php b/src/Illuminate/Mail/Mailer.php index a3d1c7ff2ac3..84e4b767de32 100755 --- a/src/Illuminate/Mail/Mailer.php +++ b/src/Illuminate/Mail/Mailer.php @@ -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 { @@ -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. * @@ -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. * @@ -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; + } } diff --git a/src/Illuminate/Mail/PendingMail.php b/src/Illuminate/Mail/PendingMail.php index e3af961a3bdd..eebb93c46203 100644 --- a/src/Illuminate/Mail/PendingMail.php +++ b/src/Illuminate/Mail/PendingMail.php @@ -34,6 +34,13 @@ class PendingMail */ protected $bcc = []; + /** + * The locale of the message. + * + * @var array + */ + protected $locale; + /** * Create a new mailable mailer instance. * @@ -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. * @@ -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); } } diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php index 8633b69a28b2..9444e747b24f 100755 --- a/src/Illuminate/Translation/Translator.php +++ b/src/Illuminate/Translation/Translator.php @@ -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; + } } diff --git a/tests/Integration/Mail/Fixtures/view.blade.php b/tests/Integration/Mail/Fixtures/view.blade.php new file mode 100644 index 000000000000..71c40be4ceaa --- /dev/null +++ b/tests/Integration/Mail/Fixtures/view.blade.php @@ -0,0 +1 @@ +{{__('nom')}} diff --git a/tests/Integration/Mail/SendingMailWithLocaleTest.php b/tests/Integration/Mail/SendingMailWithLocaleTest.php new file mode 100644 index 000000000000..9d235c3dd4f4 --- /dev/null +++ b/tests/Integration/Mail/SendingMailWithLocaleTest.php @@ -0,0 +1,95 @@ +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'); + } +} From 6b21a752c5346d63c1e864d9e676e20f40295044 Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Thu, 15 Feb 2018 19:41:17 +0200 Subject: [PATCH 2/3] fix style --- tests/Integration/Mail/SendingMailWithLocaleTest.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/Integration/Mail/SendingMailWithLocaleTest.php b/tests/Integration/Mail/SendingMailWithLocaleTest.php index 9d235c3dd4f4..b1692be8ae67 100644 --- a/tests/Integration/Mail/SendingMailWithLocaleTest.php +++ b/tests/Integration/Mail/SendingMailWithLocaleTest.php @@ -2,12 +2,11 @@ namespace Illuminate\Tests\Integration\Mail\SendingMailWithLocale; -use Illuminate\Support\Facades\Mail; -use Illuminate\Support\Facades\View; use Mockery; use Illuminate\Mail\Mailable; use Orchestra\Testbench\TestCase; -use Illuminate\Translation\Translator; +use Illuminate\Support\Facades\Mail; +use Illuminate\Support\Facades\View; /** * @group integration @@ -36,8 +35,8 @@ protected function getEnvironmentSetUp($app) '*' => [ 'en' => ['nom' => 'name'], 'ar' => ['nom' => 'esm'], - ] - ] + ], + ], ]); } From 351c543fc76bf869f04b49ddbe5e01b04f4d3e0e Mon Sep 17 00:00:00 2001 From: Mohamed Said Date: Mon, 19 Feb 2018 19:25:40 +0200 Subject: [PATCH 3/3] 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); + } + } +}