Skip to content

Commit 76dc8a1

Browse files
Derek MacDonaldderekmd
authored andcommitted
Mailable render() respects locale
In production, this allows: (new Mailable)->locale('es')->render()
1 parent ce0e5df commit 76dc8a1

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/Illuminate/Mail/Mailable.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ public function later($delay, Queue $queue)
208208
*/
209209
public function render()
210210
{
211-
Container::getInstance()->call([$this, 'build']);
211+
return $this->withLocale($this->locale, function () {
212+
Container::getInstance()->call([$this, 'build']);
212213

213-
return Container::getInstance()->make('mailer')->render(
214-
$this->buildView(), $this->buildViewData()
215-
);
214+
return Container::getInstance()->make('mailer')->render(
215+
$this->buildView(), $this->buildViewData()
216+
);
217+
});
216218
}
217219

218220
/**
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Mail;
4+
5+
use Illuminate\Mail\Mailable;
6+
use Orchestra\Testbench\TestCase;
7+
use Illuminate\Support\Facades\View;
8+
9+
/**
10+
* @group integration
11+
*/
12+
class RenderingMailWithLocaleTest extends TestCase
13+
{
14+
protected function getEnvironmentSetUp($app)
15+
{
16+
$app['config']->set('app.locale', 'en');
17+
18+
View::addLocation(__DIR__.'/Fixtures');
19+
20+
app('translator')->setLoaded([
21+
'*' => [
22+
'*' => [
23+
'en' => ['nom' => 'name'],
24+
'es' => ['nom' => 'nombre'],
25+
],
26+
],
27+
]);
28+
}
29+
30+
public function testMailableRendersInDefaultLocale()
31+
{
32+
$mail = new RenderedTestMail;
33+
34+
$this->assertEquals("name\n", $mail->render());
35+
}
36+
37+
public function testMailableRendersInSelectedLocale()
38+
{
39+
$mail = (new RenderedTestMail)->locale('es');
40+
41+
$this->assertEquals("nombre\n", $mail->render());
42+
}
43+
}
44+
45+
class RenderedTestMail extends Mailable
46+
{
47+
public function build()
48+
{
49+
return $this->view('view');
50+
}
51+
}

0 commit comments

Comments
 (0)