0

我想在用户注册时翻译通知电子邮件,但它始终是发送的默认语言。我有CustomEmailVerificationNotification一个覆盖默认供应商 / 的类SendEmailVerificationNotification

该项目使用 laravel-localization 包。

protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];
<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\URL;

class CustomEmailVerificationNotification extends Notification
{
    /**
     * The callback that should be used to create the verify email URL.
     *
     * @var \Closure|null
     */
    public static $createUrlCallback;

    /**
     * The callback that should be used to build the mail message.
     *
     * @var \Closure|null
     */
    public static $toMailCallback;

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

    /**
     * Build the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        if (static::$toMailCallback) {
            return call_user_func(static::$toMailCallback, $notifiable, $verificationUrl);
        }

        return $this->buildMailMessage($verificationUrl);
    }

    /**
     * Get the verify email notification mail message for the given URL.
     *
     * @param  string  $url
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    protected function buildMailMessage($url)
    {
        return (new MailMessage)
            ->greeting(Lang::get('custom_email_verification_notification_greeting'))
            ->subject(Lang::get('custom_email_verification_notification_subject'))
            ->line(Lang::get('custom_email_verification_notification_line_one'))
            ->action(Lang::get('custom_email_verification_notification_action'), $url)
            ->line(Lang::get('custom_email_verification_notification_line_two'));
    }

    /**
     * Get the verification URL for the given notifiable.
     *
     * @param  mixed  $notifiable
     * @return string
     */
    protected function verificationUrl($notifiable)
    {
        if (static::$createUrlCallback) {
            return call_user_func(static::$createUrlCallback, $notifiable);
        }

        return URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );
    }

    /**
     * Set a callback that should be used when creating the email verification URL.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function createUrlUsing($callback)
    {
        static::$createUrlCallback = $callback;
    }

    /**
     * Set a callback that should be used when building the notification mail message.
     *
     * @param  \Closure  $callback
     * @return void
     */
    public static function toMailUsing($callback)
    {
        static::$toMailCallback = $callback;
    }
}

我尝试像在文档中那样添加preferredLocale:

use Illuminate\Contracts\Translation\HasLocalePreference;

class User extends Authenticatable implements MustVerifyEmail, HasLocalePreference
{
    public function preferredLocale()
    {
        return $this->locale;
    }

    public function sendEmailVerificationNotification()
    {
        $this->notify(new CustomEmailVerificationNotification);
    }
}

然后在通知中制作 $notificable->locale 但它为空。

4

1 回答 1

1

试试这个选项:

use Illuminate\Contracts\Translation\HasLocalePreference;

class User extends Model implements HasLocalePreference{
    /**
     * Get the user's preferred locale.
     *
     * @return string
     */
    public function preferredLocale() {
        return $this->locale;
    }
}
于 2021-09-28T09:35:43.770 回答