Skip to content

Instantly share code, notes, and snippets.

@sebastiaanluca
Created May 16, 2018 10:28
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 sebastiaanluca/6cde6cce0acdf93e7b5c8e0996f30084 to your computer and use it in GitHub Desktop.
Save sebastiaanluca/6cde6cce0acdf93e7b5c8e0996f30084 to your computer and use it in GitHub Desktop.
Example of a localized notification
<?php
if (! function_exists('class_uses_recursive_class')) {
/**
* @param object $class
* @param object|string $uses
*
* @return bool
*/
function class_uses_recursive_class($class, $uses) : bool
{
$classes = class_uses_recursive($class);
if (! $classes) {
return false;
}
$uses = is_object($uses) ? get_class($uses) : $uses;
if (! in_array($uses, array_values($classes), true)) {
return false;
}
return true;
}
}
<?php
declare(strict_types=1);
namespace App\Notifications;
use App\Helpers\RemembersLocale;
use Illuminate\Notifications\Notifiable;
trait LocalizedNotifiable
{
use Notifiable {
notify as __notify;
notifyNow as __notifyNow;
}
/**
* Send the given notification.
*
* @param mixed $instance
*
* @return void
*/
public function notify($instance) : void
{
$this->__notify(
$this->localizeNotification($instance)
);
}
/**
* Send the given notification immediately.
*
* @param mixed $instance
* @param array|null $channels
*
* @return void
*/
public function notifyNow($instance, array $channels = null) : void
{
$this->__notifyNow(
$this->localizeNotification($instance),
$channels
);
}
/**
* Try to localize the notification if it supports multiple translations and the object being
* notified has a preferred locale.
*
* @param mixed $instance
*
* @return mixed
*/
protected function localizeNotification($instance)
{
if (class_uses_recursive_class($instance, RemembersLocale::class)
&& object_get($this, 'locale')
) {
return $instance->setLocale($this->locale);
}
return $instance;
}
}
<?php
declare(strict_types=1);
namespace App\Notifications;
use App\Helpers\RemembersLocale;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
class QueuedNotification extends Notification implements ShouldQueue
{
use Queueable, RemembersLocale;
public function __construct()
{
$this->onQueue('notifications');
$this->rememberLocale();
}
}
<?php
declare(strict_types=1);
namespace App\Notifications;
use Illuminate\Notifications\Messages\MailMessage;
class RandomNotification extends QueuedNotification
{
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
*
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
*
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
// For mails, you need to set the entire app's locale to
// the one that was set for this notification to this user
// because the mail is sent in a later stage, not here.
$this->bootLocale();
return (new MailMessage)
->subject('my subject')
->markdown('your stuff here');
}
/**
* @param $notifiable
*
* @return \Notifications\Messages\FirebaseMessage
*/
public function toFcm($notifiable) : FirebaseMessage
{
// This is an example of a non-mail notification (SMS, Firebase push notification, …).
// Here you need to wrap the sending (!) of the message in the executeInLocale closure
// so it's sent in the language of this notification's user. Afterwards, the app's
// locale is reverted back so any other translations are not interfered with.
return $this->executeInLocale(function () use ($notifiable) {
return app(FirebaseMessage::class)
->setTitle(trans('yourkey'))
->setBody(__('yourkey', [
'name' => $yourData,
]));
});
}
}
<?php
namespace App\Helpers;
trait RemembersLocale
{
/**
* The queued job's locale (language).
*
* @var string
*/
public $locale;
/**
* Manually set the locale.
*
* @param string $locale
*
* @return $this
*/
public function setLocale(string $locale)
{
$this->locale = $locale;
return $this;
}
/**
* @param callable $callback
*
* @return mixed
*/
protected function executeInLocale(callable $callback)
{
$locale = app()->getLocale();
$this->bootLocale();
$result = $callback();
app()->setLocale($locale);
return $result;
}
/**
* Restore the saved locale.
*
* Set the user's locale for the entire duration of the process until reset.
*/
protected function bootLocale()
{
app()->setLocale($this->locale);
}
/**
* Remember the current application's locale.
*/
protected function rememberLocale()
{
$this->locale = app()->getLocale();
}
}
<?php
declare(strict_types=1);
namespace User\Models;
use App\Notifications\LocalizedNotifiable;
use SebastiaanLuca\Flow\Models\Model;
class User extends Model
{
use LocalizedNotifiable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment