Laravel’s Notification locale issue

I was checking the locale issue on my own application and found out this interesting issue. I am using Laravel’s user preferred locales for the notification translation. But, it keep going with our application default language. It does not use the user’s preferred locales.

https://laravel.com/docs/8.x/notifications#localizing-notifications
https://laravel.com/docs/8.x/notifications#user-preferred-locales

I keep trying out and testing out. And found out the issue. It was because I am using the translation on the construct. Not inside the channels. Like the following…

class UserAlert extends Notification
{
    use Queueable;

    protected $title;

    protected $body;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->title = trans('notification.title');
        $this->body = trans('notification.body');
    }

That’s causing the issue. I have to use like the following. Inside the notification channel. Otherwise it will keep using the application’s default language.

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $title = trans('notification.title');
        $body = trans('notification.body');
        

That’s the issue with the Notification Locale. I hope this was helpful. I am not sure about others, I use to come back to my blog and read hahaha. Happy debugging.

no responses for Laravel’s Notification locale issue

    Leave a Reply