Skip to content

Commit

Permalink
fix: Push notifications localization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dogukanoksuz committed Jan 5, 2024
1 parent 7beff02 commit 397e72c
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/Classes/NotificationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class NotificationBuilder
/**
* @param Notification $notification
*/
public function __construct(private readonly Notification $notification)
public function __construct(private $notification, private $locale)
{
}

Expand All @@ -31,7 +31,7 @@ public function convertToBroadcastable(): array
$contents = $this->notification->contents;
$title = $contents['title'];
$content = $contents['content'];
$locale = app()->getLocale();
$locale = $this->locale;
$fallback = env('APP_LOCALE', 'tr');

if (isset($title[$locale])) {
Expand Down
8 changes: 4 additions & 4 deletions app/Http/Controllers/API/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class NotificationController extends Controller
*/
public function index()
{
return auth()->user()
return auth('api')->user()
->notifications()
->withPivot('read_at', 'seen_at')
->orderBy('send_at', 'desc')
->take(100)
->get()
->map(function ($notification) {
$builder = new NotificationBuilder($notification);
$builder = new NotificationBuilder($notification, auth('api')->user()->locale);

return $builder->convertToBroadcastable();
});
Expand All @@ -39,15 +39,15 @@ public function index()
*/
public function unread()
{
return auth()->user()
return auth('api')->user()
->notifications()
->withPivot('read_at', 'seen_at')
->where('read_at', null)
->orderBy('send_at', 'desc')
->take(8)
->get()
->map(function ($notification) {
$builder = new NotificationBuilder($notification);
$builder = new NotificationBuilder($notification, auth('api')->user()->locale);

return $builder->convertToBroadcastable();
});
Expand Down
11 changes: 5 additions & 6 deletions app/Mail/BasicNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,17 @@ class BasicNotification extends Mailable
{
use Queueable, SerializesModels;

public $user;

public $subject;

public $notification;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($notification)
public function __construct(public $notification, public $user)
{
$this->subject = __('Liman MYS Bilgilendirme');
$builder = new NotificationBuilder($notification);
$builder = new NotificationBuilder($this->notification, $this->user->locale ?? env('APP_LOCALE', 'tr'));
$this->notification = $builder->convertToBroadcastable();
}

Expand All @@ -41,6 +37,9 @@ public function __construct($notification)
*/
public function build()
{
// Set session locale
app()->setLocale($this->user->locale ?? env('APP_LOCALE', 'tr'));

return $this->from([
'address' => env('APP_NOTIFICATION_EMAIL'),
'name' => __('Liman Bildiri Sistemi'),
Expand Down
4 changes: 2 additions & 2 deletions app/Notifications/NotificationSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class NotificationSent extends Notification
*
* @return void
*/
public function __construct(private $notification)
public function __construct(private $notification, private $user)
{
}

Expand Down Expand Up @@ -52,7 +52,7 @@ public function toBroadcast(mixed $notifiable): BroadcastMessage
*/
public function toArray(): array
{
$builder = new NotificationBuilder($this->notification);
$builder = new NotificationBuilder($this->notification, $this->user->locale);
return $builder->convertToBroadcastable();
}
}
2 changes: 1 addition & 1 deletion app/Observers/NotificationObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private function sendBroadcast(Notification $notification)
$user->notify(new NotificationSent($notification, $user));
if (env('MAIL_ENABLED') && $notification && $notification->mail) {
try {
Mail::to($user)->send(new BasicNotification($notification));
Mail::to($user)->send(new BasicNotification($notification, $user));
} catch (TransportException $e) {
// Don't throw anything on when mail server is not active
}
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"Merhaba": "Hello",
"Bilginize": "For your information",
"Email Adresi": "Email Address",
"Beni Hatırla": "Remember Me",
"Giriş Yap ": "Login ",
Expand Down
6 changes: 3 additions & 3 deletions resources/views/email/external_notification.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
@endcomponent
@endslot

Merhaba,<br /><br />
{{__("Merhaba")}},<br /><br />
{{$notification['content']}}<br /><br />
Bilginize.
{{__("Bilginize")}}.

@slot('footer')
@component('mail::footer')
Bu email <a href="https://liman.works">Liman MYS</a> dış bildirim sisteminde <b>{{ isset(explode("->", $notification['title'])[1]) ? explode("->", $notification['title'])[1] : 'Liman' }}</b> tarafından oluşturulmuştur.
Bu email <a href="https://liman.havelsan.com.tr">Liman MYS</a> dış bildirim sisteminde <b>{{ isset(explode("->", $notification['title'])[1]) ? explode("->", $notification['title'])[1] : 'Liman' }}</b> tarafından oluşturulmuştur.
@endcomponent
@endslot

Expand Down

0 comments on commit 397e72c

Please sign in to comment.