我想知道翻译 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
-trait 相同。
我的猜测是,我必须创建自己的 MyResetPasswordNotification
(可以从 ResetPasswordNotification
继承)并覆盖我的 sendPasswordResetNotification
方法自己的用户模型。
或者有更好的方法,我目前还没有看到吗?
最佳答案
现在,在 Laravel 5.6+ 中,您可以在顶层创建区域设置 json 文件并翻译 ResetPassword 中的 key 。
例如
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 电子邮件上实现构建方法,并为此编写 Blade 模板。这使您能够以正常方式完全控制 Blade 和平移。
关于php - 如何翻译 ResetPasswordNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42827713/