Skip to content

Instantly share code, notes, and snippets.

@drbyte
Forked from sebastiaanluca/CombinedNotification.php
Created February 9, 2018 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbyte/e93c1dcfbc47a29648e54ce988e6f8bb to your computer and use it in GitHub Desktop.
Save drbyte/e93c1dcfbc47a29648e54ce988e6f8bb to your computer and use it in GitHub Desktop.
Queued jobs in different locales. See https://github.com/laravel/internals/issues/394 for more info.
<?php
namespace App\Notifications;
use App\Helpers\RemembersLocale;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Notifications\Channels\FirebaseChannel;
use Notifications\Messages\FirebaseMessage;
use Notifications\Notifications\FirebaseNotification;
class CombinedNotification extends Notification implements FirebaseNotification, ShouldQueue
{
use Queueable;
use RemembersLocale;
public function __construct()
{
// When the job gets created and dispatched, we store
// the current application's locale in this job. This
// way, we can reload it later when it's being executed
// in a queue for instance.
$this->rememberLocale();
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return array
*/
public function via($notifiable) : array
{
return [
'mail',
FirebaseChannel::class,
];
}
/**
* @param $notifiable
*
* @return \Notifications\Messages\FirebaseMessage
*/
public function toFcm($notifiable) : FirebaseMessage
{
// A Firebase (push) notification is a primary example of how to use
// the RemembersLocale trait. We wrap our notification in a closure
// to have all our copy translated in the locale we saved earlier.
return $this->executeInLocale(function () use ($notifiable) {
return app(FirebaseMessage::class)
->setTitle(__('combined_notification.title'))
->setBody(__('combined_notification.body'));
});
}
/**
* Get the mail representation of the notification.
*
* @param $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
// An e-mail notification or mailable is different in the way its built.
// We need to set the locale for the entire duration of the process so
// the framework parses the returned mail message in that locale. If we
// just wrap it in the closure like any other notification, it won't
// get translated because we'd have reset the locale before the message
// is built (which is some time after it gets returned here).
$this->restoreLocale();
return (new MailMessage)
->subject(__('combined_notification.subject'))
->markdown('emails.combined_notification');
}
}
<?php
namespace App\Helpers;
trait RemembersLocale
{
/**
* The queued job's locale (language).
*
* @var string
*/
public $locale;
/**
* Manually set the locale.
*
* @param string $locale
*/
public function setLocale(string $locale)
{
$this->locale = $locale;
}
/**
* Remember the current application's locale.
*/
protected function rememberLocale()
{
$this->locale = app()->getLocale();
}
/**
* Restore the saved locale.
*/
protected function restoreLocale()
{
app()->setLocale($this->locale);
}
/**
* @param callable $callback
*
* @return mixed
*/
protected function executeInLocale(callable $callback)
{
$locale = app()->getLocale();
$this->restoreLocale();
$result = $callback();
app()->setLocale($locale);
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment