diff --git a/src/Illuminate/Mail/MailServiceProvider.php b/src/Illuminate/Mail/MailServiceProvider.php index 4be6bfd52250..b07d8408f454 100755 --- a/src/Illuminate/Mail/MailServiceProvider.php +++ b/src/Illuminate/Mail/MailServiceProvider.php @@ -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. diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 212c8ac45871..9dfe01876321 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. * @@ -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. * @@ -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); + }); }); } @@ -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. * 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/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); + } + } +} 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..b1692be8ae67 --- /dev/null +++ b/tests/Integration/Mail/SendingMailWithLocaleTest.php @@ -0,0 +1,94 @@ +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'); + } +}