translate notifications

i send notification to user with this code

Notification::make()
->title(__('repair.status_change'))
->body(__('repair.status_change_body_customer', [
'originalStatus' => str($repair->getOriginal('repair_status')->getLabel()),
'newStatus' => str($repair->repair_status->getLabel()),
'item' => $repair->brand->name.' '.$repair->item->name,
'link' => '<a class="underline" href="/customer/my-repairs/">'.__('repair.open_for_more_details').'</a>'
]))
->sendToDatabase($repair->customer->user);

Notification::make()
->title(__('repair.status_change'))
->body(__('repair.status_change_body_customer', [
'originalStatus' => str($repair->getOriginal('repair_status')->getLabel()),
'newStatus' => str($repair->repair_status->getLabel()),
'item' => $repair->brand->name.' '.$repair->item->name,
'link' => '<a class="underline" href="/customer/my-repairs/">'.__('repair.open_for_more_details').'</a>'
]))
->sendToDatabase($repair->customer->user);
app language is italian user language is english, his ui is all translated in english but notification are sent in italian... if admin UI is set to english, notification are sent in english any suggestion? thanks
7 Replies
Dan Harrin
Dan Harrin2mo ago
you need to send the user the notification in their language, not yours so set the locale to the users locale before sending
Soundmit
Soundmit2mo ago
there is a way to do it here
Notification::make()
->title(__('repair.status_change'))
->body(__('repair.status_change_body_customer', [
'originalStatus' => str($repair->getOriginal('repair_status')->getLabel()),
'newStatus' => str($repair->repair_status->getLabel()),
'item' => $repair->brand->name.' '.$repair->item->name,
'link' => '<a class="underline" href="/customer/my-repairs/">'.__('repair.open_for_more_details').'</a>'
]))
->sendToDatabase($repair->customer->user);
Notification::make()
->title(__('repair.status_change'))
->body(__('repair.status_change_body_customer', [
'originalStatus' => str($repair->getOriginal('repair_status')->getLabel()),
'newStatus' => str($repair->repair_status->getLabel()),
'item' => $repair->brand->name.' '.$repair->item->name,
'link' => '<a class="underline" href="/customer/my-repairs/">'.__('repair.open_for_more_details').'</a>'
]))
->sendToDatabase($repair->customer->user);
Lara Zeus
Lara Zeus2mo ago
you can do: app()->setLocale($userLocale);
Lara Zeus
Lara Zeus2mo ago
use setLocale whereever you want to change the locale for the next code block and set it back to the original locale
if()....

setLocale

Notification::make()

setLocale
if()....

setLocale

Notification::make()

setLocale
Soundmit
Soundmit2mo ago
ok, to do in this way i need to make 2 different (but equal) if one for customer one for tech, but it's ok thansk
Lara Zeus
Lara Zeus2mo ago
I think if you implemented User Preferred Locales https://laravel.com/docs/11.x/notifications#user-preferred-locales the notification will be set depend on the user locale only if you're using broadcast or toDatabase
Want results from more Discord servers?
Add your server
More Posts