php - 如何翻译 ResetPasswordNotification

标签 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-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/

相关文章:

php - 在 html 表单中键入输入字符需要匹配 - jQuery?

php - 我不知道如何编写 sql 查询来显示我想要的信息

php - 将请求参数传递给 View - Laravel

php - Laravel 黄昏不工作.env.dusk.local

php - 使用 Elixir 功能的 Laravel css 在 xampp 上无法正常工作

php - 如何在 Laravel 中为 Stripe 发票 pdf 添加 Logo

php - 在 If 语句内运行 INSERT 查询

php - Laravel 5.5 - Horizo​​n 不自动运行第二个队列

mysql - 是否可以在 Laravel 5.8 中使用 Eloquent 或查询生成器批量插入忽略/插入或更新?不循环每条记录?

php - 使用 group by 获取其他(自定义)列并使用 laravel 进行计数