我正在尋找基于用戶區域設定值(英語和西班牙語)在 Laravel 中構建通知的最佳方法。
SomeController.php(發送通知):
Notification::send(User::find($some_id), new CustomNotification($some_parameter));
自定義通知.php :
class CustomNotification extends Notification
{
use Queueable;
protected $title, $body, $foot;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($some_parameter)
{
$this->title = __('Some Title');
$this->response = __('Some Response');
$this->foot = 'My WebPage Title';
}
...
}
使用 CustomNotification 構造,$title 和 $response 的值將根據發送通知的當前用戶進行翻譯,但在這種情況下,管理員是發送通知的人,因此該變數的值將在管理員語言環境中而不是用戶。
uj5u.com熱心網友回復:
那么在這種情況下,您應該將語言環境存盤在用戶表中,然后您可以將它(或至少是他的語言環境)發送到通知建構式:
$user=User::find($some_id);
Notification::send($user, new CustomNotification($some_parameter,$user));
并在您的通知類中:
class CustomNotification extends Notification
{
use Queueable;
protected $title, $body, $foot;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($some_parameter,User $user)
{
App::setLocale($user->locale??$defaultLocale);
$this->title = __('Some Title');
$this->response = __('Some Response');
$this->foot = 'My WebPage Title';
}
...
}
并且像@ManojKiran Appathurai 一樣,您使用Laravel 通知本地化:
Notification::send($user, new CustomNotification($some_parameter)->locale($user->locale);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/364632.html
上一篇:按鍵拆分關聯陣列
