-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mailables can hook into LocaleUpdated
Allow mailables to translate timestamps, money currency, etc. in Mailable body content by adding LocaleUpdated event listeners.
- Loading branch information
Showing
4 changed files
with
47 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{__('nom')}} {{ Illuminate\Support\Carbon::tomorrow()->diffForHumans() }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -35,6 +38,7 @@ protected function getEnvironmentSetUp($app) | |
'*' => [ | ||
'en' => ['nom' => 'name'], | ||
'ar' => ['nom' => 'esm'], | ||
'es' => ['nom' => 'nombre'] | ||
], | ||
], | ||
]); | ||
|
@@ -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()); | ||
|
@@ -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'); | ||
} | ||
} |