From c265ace533c29c34862ce52193dffcddb3bf5aea Mon Sep 17 00:00:00 2001 From: seancheung Date: Fri, 22 Mar 2024 11:47:32 +0800 Subject: [PATCH] feat: add custom history translation support --- README.md | 20 ++++++++++++++++++-- src/Events/ModelChanged.php | 6 +++++- src/Listeners/HistoryEventSubscriber.php | 3 ++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ad597f1..96da12f 100644 --- a/README.md +++ b/README.md @@ -180,7 +180,7 @@ Example meta ### Custom history -Besides the built in `created/updating/deleting/restoring` events, you may store custom history record with `ModelChanged` event. +Besides the built in `created/updating/deleting/restoring` events, you may track custom history record by firing an `ModelChanged` event. ```php use Panoscape\History\Events\ModelChanged; @@ -190,7 +190,7 @@ use Panoscape\History\Events\ModelChanged; event(new ModelChanged($user, 'User roles updated', $user->roles()->pluck('id')->toArray())); ``` -The `ModelChanged` constructor accepts two/three arguments. The first is the associated model instance; the second is the message; the third is optional, which is the meta(array); +The `ModelChanged` constructor accepts two/three/four arguments. The first is the associated model instance; the second is the message; the third is optional, which is the meta(array); the fourth is also optional, being the translation key of the event(see [Localization](#localization-1)). ### Localization @@ -234,6 +234,22 @@ This will translate your model history into 创建项目project_001 ``` +You can also translate custom history messages from `ModelChanged` events + +```php +/* +|-------------------------------------------------------------------------- +| Tracker Language Lines +|-------------------------------------------------------------------------- +*/ +'switched_role' => ':model switched role', +``` + +```php +// if you specified the translation key, the message argument will be ignored, simply just pass `null` +event(new ModelChanged($user, null, $user->roles()->pluck('id')->toArray()), 'switched_role'); +``` + ### Filters You may set whitelist and blacklist in config file. Please follow the description guide in the published config file. diff --git a/src/Events/ModelChanged.php b/src/Events/ModelChanged.php index d660980..5f353c3 100644 --- a/src/Events/ModelChanged.php +++ b/src/Events/ModelChanged.php @@ -15,19 +15,23 @@ class ModelChanged public $meta; + public $trans; + /** * Create a new event instance. * * @param Model $model * @param string $message * @param array $meta + * @param string $trans * * @return void */ - public function __construct($model, $message, $meta = null) + public function __construct($model, $message, $meta = null, $trans = null) { $this->model = $model; $this->message = $message; $this->meta = $meta; + $this->trans = $trans; } } diff --git a/src/Listeners/HistoryEventSubscriber.php b/src/Listeners/HistoryEventSubscriber.php index 9b8109e..90b0021 100644 --- a/src/Listeners/HistoryEventSubscriber.php +++ b/src/Listeners/HistoryEventSubscriber.php @@ -18,8 +18,9 @@ public function onModelChanged($event) { if(!HistoryObserver::filter(null)) return; + $message = $event->trans == null? $event->message : trans('panoscape::history.'.$event->trans, ['model' => static::getModelName($model), 'label' => $model->getModelLabel()]); $event->model->morphMany(History::class, 'model')->create([ - 'message' => $event->message, + 'message' => $message, 'meta' => $event->meta, 'user_id' => HistoryObserver::getUserID(), 'user_type' => HistoryObserver::getUserType(),