【问题标题】:How can I translate the password reset email in Laravel 5.7?如何在 Laravel 5.7 中翻译密码重置邮件?
【发布时间】:2021-07-06 10:52:02
【问题描述】:

我正在尝试在 Laravel 5.7 中翻译密码重置电子邮件,默认为英文。

通常 - 对于登录、注册和密码重置视图 - 您会翻译 /resources/lang/ 下的文件,但我在电子邮件中找不到正文的相应行。

如何翻译密码重置邮件?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    Illuminate\Auth\Notifications\ResetPassword::toMail() 方法中,您可以看到Lang::getFromJson() 方法用于填充电子邮件:

    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.'));
    

    因此您应该能够将这些翻译添加到resources/lang/xx.json 文件as described in the documentation(向下滚动到“使用翻译字符串作为键”。)

    这也适用于Illuminate\Auth\Notifications\VerifyEmail 中的电子邮件验证消息。

    例如,这可能是resources/lang/fr.json 的内容(原谅我25年前的高中法语)

    {
        "If you did not request a password reset, no further action is required.": "Si vous ne demandez pas le réinitialisation de votre mot de passe, vous ne pouvez rien faire"
    }
    

    对于这两个类,模板文件 Illuminate/Notifications/resources/views/email.blade.php 包含标准 Blade @lang 标记中的附加文本,可以使用位于 resources/lang/xx/messages.php 的消息文件进行翻译

    例如,这可能是resources/lang/fr/messages.php的内容:

    <?php
    return [
        "Regards" => "Félicitations",
    ];
    

    【讨论】:

      【解决方案2】:

      刚刚发现您还可以翻译 json 文件中的 @lang 标签:

      {
        "Regards": "Met vriendelijke groet",
        "If you’re having trouble clicking the \":actionText\" button, copy and paste the URL below\ninto your web browser: [:actionURL](:actionURL)": "Als u problemen ondervindt bij het klikken op de knop \":actionText\" kopieert en plakt u de onderstaande URL in uw webbrowser\n[:actionURL](:actionURL)",
        "All rights reserved.": "Alle rechten voorbehouden."
      }
      

      查看所有翻译文件的存储库:

      https://github.com/caouecs/Laravel-lang

      【讨论】:

      • 这应该是公认的答案。在 Laravel 8 下测试。只需将您的 .json 文件复制到 /resources/lang 并确保与 /config/app.php 中的语言环境匹配
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多