Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] Mailables can hook into LocaleUpdated #24451

Merged
merged 1 commit into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Illuminate/Mail/Mailable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Illuminate\Support\Traits\Localizable;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Contracts\Queue\Factory as Queue;
use Illuminate\Contracts\Translation\Translator;
use Illuminate\Contracts\Mail\Mailer as MailerContract;
use Illuminate\Contracts\Mail\Mailable as MailableContract;
use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory;
Expand Down Expand Up @@ -134,9 +133,7 @@ class Mailable implements MailableContract, Renderable
*/
public function send(MailerContract $mailer)
{
$translator = Container::getInstance()->make(Translator::class);

$this->withLocale($this->locale, $translator, function () use ($mailer) {
$this->withLocale($this->locale, function () use ($mailer) {
Container::getInstance()->call([$this, 'build']);

$mailer->send($this->buildView(), $this->buildViewData(), function ($message) {
Expand Down
19 changes: 11 additions & 8 deletions src/Illuminate/Support/Traits/Localizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,33 @@

namespace Illuminate\Support\Traits;

use Illuminate\Container\Container;

trait Localizable
{
/**
* Run the callback with the given locale.
*
* @param string $locale
* @param \Illuminate\Contracts\Translation\Translator $translator
* @param \Closure $callback
* @param string $locale
* @param \Closure $callback
* @return bool
*/
public function withLocale($locale, $translator, $callback)
public function withLocale($locale, $callback)
{
if (! $locale || ! $translator) {
if (! $locale) {
return $callback();
}

$original = $translator->getLocale();
$app = Container::getInstance();

$original = $app->getLocale();

try {
$translator->setLocale($locale);
$app->setLocale($locale);

return $callback();
} finally {
$translator->setLocale($original);
$app->setLocale($original);
}
}
}
1 change: 1 addition & 0 deletions tests/Integration/Mail/Fixtures/timestamp.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{__('nom')}} {{ Illuminate\Support\Carbon::tomorrow()->diffForHumans() }}
34 changes: 34 additions & 0 deletions tests/Integration/Mail/SendingMailWithLocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Mockery;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Carbon;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Events\LocaleUpdated;

/**
* @group integration
Expand Down Expand Up @@ -35,6 +38,7 @@ protected function getEnvironmentSetUp($app)
'*' => [
'en' => ['nom' => 'name'],
'ar' => ['nom' => 'esm'],
'es' => ['nom' => 'nombre'],
],
],
]);
Expand Down Expand Up @@ -63,6 +67,23 @@ public function test_mail_is_sent_with_selected_locale()
);
}

public function test_mail_is_sent_with_locale_updated_listeners_called()
{
Carbon::setTestNow(Carbon::parse('2018-04-01'));

Event::listen(LocaleUpdated::class, function ($event) {
Carbon::setLocale($event->locale);
});

Mail::to('[email protected]')->locale('es')->send(new TimestampTestMail);

$this->assertContains('nombre dentro de 1 día',
app('swift.transport')->messages()[0]->getBody()
);

$this->assertEquals('en', Carbon::getLocale());
}

public function test_locale_is_set_back_to_default_after_mail_sent()
{
Mail::to('[email protected]')->locale('ar')->send(new TestMail());
Expand Down Expand Up @@ -92,3 +113,16 @@ public function build()
return $this->view('view');
}
}

class TimestampTestMail extends Mailable
{
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('timestamp');
}
}