如何翻译ResetPasswordNotification

时间:2017-03-16 07:25:52

标签: php laravel laravel-5

我想知道从PasswortResetNotification翻译邮件的最佳方式。

public function toMail($notifiable)
{
    return (new MailMessage)
        ->line('You are receiving this email because we received a password reset request for your account.')
        ->action('Reset Password', route('password.reset', $this->token))
        ->line('If you did not request a password reset, no further action is required.');
}

更改ResetPassword(上面)中的代码似乎不适用,因为这是供应商文件夹的一部分 - 同样适用于CanResetPassword - 特性。

我的猜测是,我必须创建自己的MyResetPasswordNotification(可以继承自ResetPasswordNotification)并覆盖我自己的用户模型中的sendPasswordResetNotification - 方法。

还是有更好的方法,我目前看不到?

2 个答案:

答案 0 :(得分:1)

你可以使用php artisan vendor:publish这会给你 \resources\views\vendor\notifications可配置

供应商:发布选项:

php artisan help vendor:publish
Usage:
 vendor:publish [--force] [--provider[="..."]] [--tag[="..."]]

Options:
 --force               Overwrite any existing files.
 --provider            The service provider that has assets you want to publish.
 --tag                 The tag that has assets you want to publish.
 --help (-h)           Display this help message
 --quiet (-q)          Do not output any message
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
 --version (-V)        Display this application version
 --ansi                Force ANSI output
 --no-ansi             Disable ANSI output
 --no-interaction (-n) Do not ask any interactive question
 --env                 The environment the command should run under.

答案 1 :(得分:1)

这几天在Laravel 5.6+中,您可以在顶层创建区域设置json文件,并在ResetPassword中转换键。

例如

fr.json
{
  "Reset Password Notification" => "Changement de mot de passe"
}

请参见https://laravel.com/docs/5.6/localization#using-translation-strings-as-keys

要翻译的相关行在Illuminate\Auth\Notifications\ResetPassword密码文件中。

    return (new MailMessage)
        ->subject(Lang::getFromJson('Reset Password Notification'))
        ->line(Lang::getFromJson('You are receiving this email because we received a password reset request for your account.'))
        ->action(Lang::getFromJson('Reset Password'), url(config('app.url').route('password.reset', $this->token, false)))
        ->line(Lang::getFromJson('If you did not request a password reset, no further action is required.'));

编辑: 并非所有翻译都在这里,您还需要查看 Illuminate/Notifications/resources/views/email.blade.php并翻译这些键。

总体而言,遵循文档中所提示的内容可能会更容易:

$ php artisan make:mail ResetPassword

// pass the token to the view in the created file, e.g. 
public $link;
public function __construct($token, $notifiable)
{
    $this->link = url('password/reset', $token).'?email='.urlencode($notifiable->getEmailForPasswordReset());
}

$ php artisan make:notification ResetPassword

// change toMail method in the created file
use App\Mail\ResetPassword as ResetPasswordEmail;

...
public function toMail($notifiable)
{
    return (new ResetPasswordEmail($this->token, $notifiable))->to($notifiable);
}

// User Model

use App\Notifications\ResetPassword as ResetPasswordNotification;

...
public function sendPasswordResetNotification($token)
{
    $this->notify(new ResetPasswordNotification($token));
}

然后在ResetPassword电子邮件上实现build方法,并为此编写刀片模板。这使您能够以正常方式完全控制刀片和平移。