LoginSignup
2
3

More than 3 years have passed since last update.

Laravel パスワードリマインダーメールを日本語化してみる

Last updated at Posted at 2020-02-08

パスワードリマインダーカスタマイズ・日本語化

  • make:authコマンドとメールSMTP設定が済むとパスワードリマインダー機能が使える
    • デフォルトでは英語となっているため日本語化の設定が必要

(追記)

@ShibuyaKosuke さんのコメントでresources/lang/ja.json の編集のみでメールでも日本語化が可能なことを教えて頂きました。こちらの方がいいと思います。

ja.json
{
    "Hello!": "こんにちは",
    "Reset Password Notification": "パスワード・リセット通知",
    "You are receiving this email because we received a password reset request for your account.": "アカウントのパスワードリセットリクエストを受け取ったため、このメールを受信して\u200B\u200Bいます。",
    "Reset Password": "パスワードをリセット",
    "This password reset link will expire in :count minutes.": "このパスワードリセットリンクの有効期限は :count 分です。",
    "If you did not request a password reset, no further action is required.": "パスワードのリセットを要求しなかった場合、それ以上のアクションは不要です。"
}

送信されるメールの日本語化

  • notificationのカスタマイズ
  • App\Userにカスタマイズしたnotificationメソッドを追加し、定義をオーバーライド

notificationのカスタマイズ

$ php aritsan make:notification CustomResetPassword

app/Notifications/CustomResetPassword.phpが生成される
中身を次のように変更する

app/Notifications/CustomResetPassword.php
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class CustomResetPassword extends Notification
{
  use Queueable;

  /**
   * Create a new notification instance.
   *
   * @return void
   */

  public $token;

  public function __construct($token)
  {
    $this->token = $token;
  }

  /**
   * 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)
  {
    return (new MailMessage)
      ->from('admin@example.com', config('app.name'))
      ->subject('パスワード再設定')
      ->line('下のボタンをクリックしてパスワードを再設定してください。')
      ->action('パスワード再設定', url(config('app.url') . route('password.reset', $this->token, false)))
      ->line('もし心当たりがない場合は、本メッセージは破棄してください。');
  }

  /**
   * Get the array representation of the notification.
   *
   * @param  mixed  $notifiable
   * @return array
   */
  public function toArray($notifiable)
  {
    return [
      //
    ];
  }
}

App\Userのオーバーライド

App\Userに次の内容を追加する

App\User
// 追加する内容
use App\Notifications\CustomResetPassword;


class User extends Authenticatable
{
  use Notifiable;

  // 追加する内容
  public function sendPasswordResetNotification($token)
  {
      $this->notify(new CustomResetPassword($token));
  }

(補足)送信された再設定用のリンクが開けない場合

  • .envファイルのAPP_URLがうまく設定できていない場合がある
    • APP_URL=http://192.168.0.1:3000 などのようにつながるドメイン・ポート番号に設定する
2
3
3

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
2
3