Skip to content

Commit

Permalink
Mailable render() respects locale
Browse files Browse the repository at this point in the history
In production, this allows:

(new Mailable)->locale('es')->render()
  • Loading branch information
Derek MacDonald authored and derekmd committed Oct 7, 2018
1 parent ce0e5df commit 76dc8a1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
});
}

/**
Expand Down
51 changes: 51 additions & 0 deletions tests/Integration/Mail/RenderingMailWithLocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Illuminate\Tests\Integration\Mail;

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

/**
* @group integration
*/
class RenderingMailWithLocaleTest extends TestCase
{
protected function getEnvironmentSetUp($app)
{
$app['config']->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');
}
}

0 comments on commit 76dc8a1

Please sign in to comment.