DEV Community

Mahmoud Ramadan
Mahmoud Ramadan

Posted on

Using User Locale for Notifications

It is considered best practice to send notifications and emails in the recipient's preferred locale. Laravel supports this out of the box through the HasLocalePreference contract, which enables you to easily define a user's locale preference:

use Illuminate\Contracts\Translation\HasLocalePreference;

class User extends Model implements HasLocalePreference
{
    /**
     * Get the user's preferred locale.
     */
    public function preferredLocale(): string
    {
        return $this->locale;
    }
}
Enter fullscreen mode Exit fullscreen mode

Once you implement the HasLocalePreference contract, Laravel will automatically use the preferred locale returned by the preferredLocale method when sending notifications and emails.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.