avatarSet Kyar Wa Lar

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1274

Abstract

ype">Queueable</span>;</pre></div><div id="496d"><pre> <span class="hljs-keyword">protected</span> <span class="hljs-variable">title</span>;</pre></div><div id="409f"><pre> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">body</span>;</pre></div><div id="0811"><pre> <span class="hljs-comment">/** * Create a new notification instance. * * <span class="hljs-doctag">@return</span> void /</span> <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params"></span>) </span>{ <span class="hljs-variable language_">this</span>-&gt;title = <span class="hljs-title function_ invoke__">trans</span>(<span class="hljs-string">'notification.title'</span>); <span class="hljs-variable language_">this</span>->body = <span class="hljs-title function_ invoke__">trans</span>(<span class="hljs-string">'notification.body'</span>); }</pre></div><p id="da7f">That’s causing the issue. I have to use the following. Inside the notification channel. Otherwise, it will keep using the application’s default language.</p><div id="909e"><pre>/*

  • Get the notification's delivery cha

Options

nnels. *

  • @param mixed $notifiable
  • @return<span class="hljs-built_in"> array </span> /<span class="hljs-keyword"> public</span> function via($notifiable) { <span class="hljs-built_in"> return </span>['mail']; }</pre></div><div id="39fa"><pre><span class="hljs-comment">/*
  • Get the mail representation of the notification.
  • <span class="hljs-doctag">@param</span> mixed $notifiable
  • <span class="hljs-doctag">@return</span> \Illuminate\Notifications\Messages\MailMessage */</span> <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">toMail</span>(<span class="hljs-params"><span class="hljs-variable">notifiable</span></span>) </span>{ <span class="hljs-variable">title</span> = <span class="hljs-title function_ invoke__">trans</span>(<span class="hljs-string">'notification.title'</span>); <span class="hljs-variable">$body</span> = <span class="hljs-title function_ invoke__">trans</span>(<span class="hljs-string">'notification.body'</span>);</pre></div><p id="d655">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.</p></article></body>

Fixing Laravel’s Notification locale issue

Originally posted — https://setkyar.com/laravels-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 keeps going with our application's 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 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.

Laravel
Localization
Recommended from ReadMedium
avatarAI Rabbit
Goodbye Obsidian

7 min read