Localized Database Notifications. #19334
Replies: 1 comment
|
Database notifications are snapshots. When Filament/Laravel persists a notification, That’s why there’s no first-class “live translation” for already-stored DB notifications: the DB row would either need to store keys + params, or you’d re-translate on every read. Practical patterns1) Translate at send-time (simplest) $locale = $user->preferred_locale ?? app()->getLocale();
app()->setLocale($locale);
Notification::make()
->title(__('notifications.order_shipped.title'))
->body(__('notifications.order_shipped.body', ['id' => $order->id]))
->sendToDatabase($user);The stored text matches the user at the moment it was created. 2) Store translation keys yourself (true “translatable later”) Notification::make()
->title('order_shipped') // treat as key
->body(json_encode(['id' => $order->id]))
->sendToDatabase($user);
// when displaying:
__("notifications.{$notification->data['title']}.title");or a custom database notification class that writes structured 3) A RecommendationFor most apps: translate when sending into the user’s locale. Only store keys if users regularly switch language and need old notifications to flip language on the fly. So the current “evaluate to string” behavior is intentional consistency for persisted mail/history — not missing i18n, just deferred i18n lives in your persistence format. |
Uh oh!
There was an error while loading. Please reload this page.
Package
Notifications
Package Version
v5.0
How can we help you?
Hi there,
I was wondering why the ability to have localized database notifications isn't there, I know that the
NotificationclasstoArray()function evaluates the title or the body to its value, but why is that, can't it just serialize them if they are closures which will add more consistency? (this is the current behavior of mine).Has anyone else tackled this? Would it make sense to add a
->translatableTitle()method or something similar? Or is there already a recommended pattern for localizing persisted notifications?All reactions