Skip to content

Commit

Permalink
Notifiables can set preferred locale
Browse files Browse the repository at this point in the history
  • Loading branch information
derekmd committed Sep 24, 2018
1 parent f4abbdc commit aeef1b0
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Illuminate/Notifications/NotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Localizable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\Collection as ModelCollection;

class NotificationSender
Expand Down Expand Up @@ -95,7 +96,7 @@ public function sendNow($notifiables, $notification, array $channels = null)
continue;
}

$this->withLocale($notification->locale ?? $this->locale, function () use ($viaChannels, $notifiable, $original) {
$this->withLocale($this->preferredLocale($notifiable, $notification), function () use ($viaChannels, $notifiable, $original) {
$notificationId = Str::uuid()->toString();

foreach ((array) $viaChannels as $channel) {
Expand All @@ -105,6 +106,23 @@ public function sendNow($notifiables, $notification, array $channels = null)
}
}

/**
* Get the locale for the notification preferred by this notifiable.
*
* @param mixed $notifiable
* @param mixed $notification
*
* @return string|null
*/
protected function preferredLocale($notifiable, $notification)
{
return $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
if ($notifiable instanceof HasLocalePreference) {
return $notifiable->preferredLocale();
}
});
}

/**
* Send the given notification to the given notifiable via a channel.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Foundation\Events\LocaleUpdated;
use Illuminate\Notifications\Channels\MailChannel;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Support\Facades\Notification as NotificationFacade;

/**
Expand Down Expand Up @@ -45,6 +46,7 @@ protected function getEnvironmentSetUp($app)
'*' => [
'*' => [
'en' => ['hi' => 'hello'],
'es' => ['hi' => 'hola'],
'fr' => ['hi' => 'bonjour'],
],
],
Expand Down Expand Up @@ -155,6 +157,83 @@ public function test_mail_is_sent_with_locale_updated_listeners_called()

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

public function test_locale_is_sent_with_notifiable_preferred_locale()
{
$recipient = new NotifiableEmailLocalePreferredUser([
'email' => '[email protected]',
'email_locale' => 'fr',
]);

$recipient->notify(new GreetingMailNotification());

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_sent_with_notifiable_preferred_locale_for_multiple_recipients()
{
$recipients = [
new NotifiableEmailLocalePreferredUser([
'email' => '[email protected]',
'email_locale' => 'fr',
]),
new NotifiableEmailLocalePreferredUser([
'email' => '[email protected]',
'email_locale' => 'es',
]),
NotifiableLocalizedUser::forceCreate([
'email' => '[email protected]',
]),
];

NotificationFacade::send(
$recipients, new GreetingMailNotification()
);

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
$this->assertContains('hola',
app('swift.transport')->messages()[1]->getBody()
);
$this->assertContains('hi',
app('swift.transport')->messages()[2]->getBody()
);
}

public function test_locale_is_sent_with_notification_selected_locale_overriding_notifiable_preferred_locale()
{
$recipient = new NotifiableEmailLocalePreferredUser([
'email' => '[email protected]',
'email_locale' => 'es',
]);

$recipient->notify(
(new GreetingMailNotification())->locale('fr')
);

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
}

public function test_locale_is_sent_with_facade_selected_locale_overriding_notifiable_preferred_locale()
{
$recipient = new NotifiableEmailLocalePreferredUser([
'email' => '[email protected]',
'email_locale' => 'es',
]);

NotificationFacade::locale('fr')->send(
$recipient, new GreetingMailNotification()
);

$this->assertContains('bonjour',
app('swift.transport')->messages()[0]->getBody()
);
}
}

class NotifiableLocalizedUser extends Model
Expand All @@ -165,6 +244,21 @@ class NotifiableLocalizedUser extends Model
public $timestamps = false;
}

class NotifiableEmailLocalePreferredUser extends Model implements HasLocalePreference
{
use Notifiable;

protected $fillable = [
'email',
'email_locale',
];

public function preferredLocale()
{
return $this->email_locale;
}
}

class GreetingMailNotification extends Notification
{
public function via($notifiable)
Expand Down

0 comments on commit aeef1b0

Please sign in to comment.