Laravel重置密码通知中Lang::get('Reset Password Notification')如何翻译?
翻译步骤
1. 翻译文件存放&内容编写
你代码中直接以英文原字符串作为Lang::get()的键,推荐直接使用JSON格式的翻译文件,路径为resources/lang/zh_CN.json,文件不存在直接新建即可。
文件内以英文原串作为键,对应中文翻译作为值,示例内容如下:
{ "Reset Password Notification": "重置密码通知", "You are receiving this email because we received a password reset request for your account.": "你收到此邮件是因为我们接收到针对你账号的重置密码申请。", "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.": "如果你未申请重置密码,无需进行任何操作。" }
翻译内容里的:count占位符会被Laravel自动替换为对应数值,不需要修改。
2. 语言设置(locale配置)
- 全局默认生效:打开
config/app.php配置文件,找到locale配置项,修改为对应语言标识,和你新建的翻译文件前缀保持一致即可,示例:
'locale' => 'zh_CN',
- 临时动态切换:如果需要根据用户场景自定义语言,可在业务逻辑或中间件中调用方法动态设置:
\App::setLocale('zh_CN');
3. 自定义通知生效配置
你已经复制了默认重置密码通知到app/Notifications/PasswordReset.php,需要在App\Models\User.php中重写密码重置通知发送方法,否则系统仍会调用vendor目录下的默认通知:
public function sendPasswordResetNotification($token) { $resetUrl = url(route('password.reset', [ 'token' => $token, 'email' => $this->email ])); $this->notify(new \App\Notifications\PasswordReset($resetUrl)); }
内容的提问来源于stack exchange,提问作者Aipo
相关产品推荐
相关产品推荐

