LoginSignup
4
1

More than 3 years have passed since last update.

Laravel 5.8 認証通知メールの多言語化

Last updated at Posted at 2019-07-22

通知処理の実態

make:authで実装されるものは基本的な処理のみで
骨格となる処理はvendor/laravel/framework配下に実装されている

メール内容の多言語化

メール本文は以下のphpで実装されている。
vendor/laravel/framework/src/Illuminate/Auth/Notifications
ResetPassword.php⇒パスワードリセット通知
VerifyEmail.php⇒登録時認証メール通知

Lang::getFromJson()でja.jsonの置換を行っている。

メールのヘッダーフッターは以下のファイルを使用している。
vendor/laravel/framework/src/Illuminate/Notifications/resources/views/配下にある。
email.blade.php

@lang()に囲われた文字列をja.jsonに定義することで変換される

@lang(
    "If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\n".
    'into your web browser: [:actionURL](:actionURL)',
    [
        'actionText' => $actionText,
        'actionURL' => $actionUrl,
    ]
)

上記は複雑ですが.を削除して、文字列を連結してja.jsonに定義すれば翻訳されるようです。

resouces/lang/ja.json
{
(略)
    "If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\ninto your web browser: [:actionURL](:actionURL)": "もし\":actionText\"ボタンをクリックできない場合は、以下のURLをコピー&ペーストして、ブラウザから入力してください。[:actionURL](:actionURL)"
(略)
}

独自のメールにしたい場合は、先人たちの例に沿って既存の通知処理をカスタムする。
参考URL
Laravelパスワードリセットメールの文面・デザインを変更する

認証メール・パスワード再設定メールのカスタム

以下のコマンドでそれぞれの通知クラスを作成する

$ php artisan make:notification CustomResetPasswordNotification
$ php artisan make:notification CustomVerifyEmailNotification

app/Notificationsフォルダが作成され、phpファイルが生成される。

リセット通知のカスタム

通知クラスとメール内容の変更

toMail()を以下のように変える。

app/Notifications/CustomResetPasswordNotification.php

use Illuminate\Auth\Notifications\ResetPassword; // ResetPasswordクラス参照

class CustomResetPasswordNotification extends Notification
{
    public $token;
//    public function __construct()
    public function __construct($token)
    {
        $this->token = $token;
    }
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject(__('Reset Password'))
            ->view('vendor.notifications.email')
            ->action(__('Reset Password'), url('password/reset', $this->token));
    }
}

標準のemail.blade.phpresources/views/vendro/notifications/に用意する。
メールの定型文を大幅に変える場合、email.blade.phpを複製してカスタマイズする。

追記
view()にすると例外が発生するのでmarkdown()にする。
定型文に則ると以下の感じ。

app/Notifications/CustomResetPasswordNotification.php
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->markdown('vendor.notifications.email', [
                'actionText' => __('Reset Password'),
                'introLines' => [
                    __('You are receiving this email because we received a password reset request for your account.'),
                ],
                'outroLines' => [
                    __('If you did not request a password reset, no further action is required.'),
                ],
            ])
            ->action(__('Reset Password'), url('password/reset', $this->token))
            ->subject(__('Reset Password Notification'));
    }

パスワードリセットと登録認証でそれぞれ定型文ファイルを分けるなら
引数で渡す必要はないので以下のように修正。

$ cp ./resources/views/vendor/notifications/email.blade.php ./resources/views/vendor/notifications/reset.blade.php

通知クラスの呼び出しを簡素化

app/Notifications/CustomResetPasswordNotification.php
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->markdown('vendor.notifications.reset')
            ->action(__('Reset Password'), url('password/reset', $this->token))
            ->subject(__('Reset Password Notification'));
    }

bladeファイルに多言語対応する場合は上述の通り、__(...)@lang(...)に置換する。

認証通知メールのカスタム

通知クラスの変更

リセットと同様に変更するが、認証するため必要なURLを取得するためのメソッドverificationUrl
NotificationsにはなくIlluminate\Auth\Notifications\VerifyEmailにあるのでこちらを継承する。

app/Notifications/CustomVerifyEmailNotification.php

use Illuminate\Auth\Notifications\VerifyEmail; // クラス参照

//class CustomVerifyEmailNotification extends Notification
class CustomVerifyEmailNotification extends VerifyEmail
{

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->markdown('vendor.notifications.verify')
            ->action(__('Verify Email Address'), $this->verificationUrl($notifiable)) // verificationUrl()で認証URL取得
            ->subject(__('Verify Email Address'));
    }
Userモデルの変更
app/User.php
use Illuminate\Contracts\Auth\MustVerifyEmail as ContractsMustVerifyEmail; // Illuminate\Auth\MustVerifyEmailと名称が被る為、別名
use Illuminate\Auth\MustVerifyEmail;
use App\Notifications\CustomResetPasswordNotification;
use App\Notifications\CustomVerifyEmailNotification;

class User extends Authenticatable implements ContractsMustVerifyEmail
{
    use Notifiable;
    use MustVerifyEmail;
()
    /**
     * Send the email verification notification.
     *
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new CustomVerifyEmailNotification());
    }
4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1