Skip to content

Commit

Permalink
Notifications can choose locale
Browse files Browse the repository at this point in the history
Queued notifications can run in a
locale other than the application
default.
  • Loading branch information
derekmd committed Jul 21, 2018
1 parent 499485e commit 40615ab
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 10 deletions.
24 changes: 22 additions & 2 deletions src/Illuminate/Notifications/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ class ChannelManager extends Manager implements DispatcherContract, FactoryContr
*/
protected $defaultChannel = 'mail';

/**
* Locale used when sending notifications.
*
* @var string|null
*/
protected $locale;

/**
* Send the given notification to the given notifiable entities.
*
Expand All @@ -31,7 +38,7 @@ class ChannelManager extends Manager implements DispatcherContract, FactoryContr
public function send($notifiables, $notification)
{
return (new NotificationSender(
$this, $this->app->make(Bus::class), $this->app->make(Dispatcher::class))
$this, $this->app->make(Bus::class), $this->app->make(Dispatcher::class), $this->locale)
)->send($notifiables, $notification);
}

Expand All @@ -46,7 +53,7 @@ public function send($notifiables, $notification)
public function sendNow($notifiables, $notification, array $channels = null)
{
return (new NotificationSender(
$this, $this->app->make(Bus::class), $this->app->make(Dispatcher::class))
$this, $this->app->make(Bus::class), $this->app->make(Dispatcher::class), $this->locale)
)->sendNow($notifiables, $notification, $channels);
}

Expand Down Expand Up @@ -168,4 +175,17 @@ public function deliverVia($channel)
{
$this->defaultChannel = $channel;
}

/**
* Set the locale of notifications.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;

return $this;
}
}
20 changes: 20 additions & 0 deletions src/Illuminate/Notifications/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class Notification
*/
public $id;

/**
* Locale used when sending the notification.
*
* @var string|null
*/
public $locale;

/**
* Get the channels the event should broadcast on.
*
Expand All @@ -24,4 +31,17 @@ public function broadcastOn()
{
return [];
}

/**
* Set the locale to send this notification in.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;

return $this;
}
}
28 changes: 23 additions & 5 deletions src/Illuminate/Notifications/NotificationSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Localizable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection as ModelCollection;

class NotificationSender
{
use Localizable;

/**
* The notification manager instance.
*
Expand All @@ -31,19 +34,28 @@ class NotificationSender
*/
protected $events;

/**
* Locale used when sending notifications.
*
* @var string|null
*/
protected $locale;

/**
* Create a new notification sender instance.
*
* @param \Illuminate\Notifications\ChannelManager $manager
* @param \Illuminate\Contracts\Bus\Dispatcher $bus
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @param string|null $locale
* @return void
*/
public function __construct($manager, $bus, $events)
public function __construct($manager, $bus, $events, $locale = null)
{
$this->bus = $bus;
$this->events = $events;
$this->manager = $manager;
$this->locale = $locale;
}

/**
Expand Down Expand Up @@ -83,11 +95,13 @@ public function sendNow($notifiables, $notification, array $channels = null)
continue;
}

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

foreach ((array) $viaChannels as $channel) {
$this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
}
foreach ((array) $viaChannels as $channel) {
$this->sendToNotifiable($notifiable, $notificationId, clone $original, $channel);
}
});
}
}

Expand Down Expand Up @@ -153,6 +167,10 @@ protected function queueNotification($notifiables, $notification)

$notification->id = $notificationId;

if (! is_null($this->locale)) {
$notification->locale = $this->locale;
}

$this->bus->dispatch(
(new SendQueuedNotifications($notifiable, $notification, [$channel]))
->onConnection($notification->connection)
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @method static void send(\Illuminate\Support\Collection|array|mixed $notifiables, $notification)
* @method static void sendNow(\Illuminate\Support\Collection|array|mixed $notifiables, $notification)
* @method static mixed channel(string|null $name = null)
* @method static \Illuminate\Notifications\ChannelManager locale(string|null $locale)
*
* @see \Illuminate\Notifications\ChannelManager
*/
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class NotificationFake implements NotificationFactory, NotificationDispatcher
*/
protected $notifications = [];

/**
* Locale used when sending notifications.
*
* @var string|null
*/
public $locale;

/**
* Assert if a notification was sent based on a truth-test callback.
*
Expand Down Expand Up @@ -203,6 +210,7 @@ public function sendNow($notifiables, $notification)
'notification' => $notification,
'channels' => $notification->via($notifiable),
'notifiable' => $notifiable,
'locale' => $notification->locale ?? $this->locale,
];
}
}
Expand All @@ -217,4 +225,17 @@ public function channel($name = null)
{
//
}

/**
* Set the locale of notifications.
*
* @param string $locale
* @return $this
*/
public function locale($locale)
{
$this->locale = $locale;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ __('hi') }}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ public function test_faking()
->route('testchannel', 'enzo')
->route('anothertestchannel', 'enzo@deepblue.com');

NotificationFacade::send(
NotificationFacade::locale('it')->send(
$notifiable,
new TestMailNotificationForAnonymousNotifiable()
);

NotificationFacade::assertSentTo(new AnonymousNotifiable(), TestMailNotificationForAnonymousNotifiable::class,
function ($notification, $channels, $notifiable) {
return $notifiable->routes['testchannel'] == 'enzo' && $notifiable->routes['anothertestchannel'] == 'enzo@deepblue.com';
function ($notification, $channels, $notifiable, $locale) {
return $notifiable->routes['testchannel'] === 'enzo' &&
$notifiable->routes['anothertestchannel'] === 'enzo@deepblue.com' &&
$locale === 'it';
}
);
}
Expand Down
Loading

0 comments on commit 40615ab

Please sign in to comment.