diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 38dd73cd38ce..34ede80bd2a9 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -208,11 +208,13 @@ public function later($delay, Queue $queue) */ public function render() { - Container::getInstance()->call([$this, 'build']); + return $this->withLocale($this->locale, function () { + Container::getInstance()->call([$this, 'build']); - return Container::getInstance()->make('mailer')->render( - $this->buildView(), $this->buildViewData() - ); + return Container::getInstance()->make('mailer')->render( + $this->buildView(), $this->buildViewData() + ); + }); } /** diff --git a/tests/Integration/Mail/RenderingMailWithLocaleTest.php b/tests/Integration/Mail/RenderingMailWithLocaleTest.php new file mode 100644 index 000000000000..742be8820a9c --- /dev/null +++ b/tests/Integration/Mail/RenderingMailWithLocaleTest.php @@ -0,0 +1,51 @@ +set('app.locale', 'en'); + + View::addLocation(__DIR__.'/Fixtures'); + + app('translator')->setLoaded([ + '*' => [ + '*' => [ + 'en' => ['nom' => 'name'], + 'es' => ['nom' => 'nombre'], + ], + ], + ]); + } + + public function testMailableRendersInDefaultLocale() + { + $mail = new RenderedTestMail; + + $this->assertEquals("name\n", $mail->render()); + } + + public function testMailableRendersInSelectedLocale() + { + $mail = (new RenderedTestMail)->locale('es'); + + $this->assertEquals("nombre\n", $mail->render()); + } +} + +class RenderedTestMail extends Mailable +{ + public function build() + { + return $this->view('view'); + } +}