Translate Laravel’s reset password email
It takes a few steps to completely translate Laravel’s reset password email and I’m listing them here as a reminder for myself and others.
If you have a blank Laravel installation you’ll have to follow steps from the documentation to set up authentication:
https://laravel.com/docs/5.3/authentication#introduction
This will leave you with a general authentication functionality which includes forgot password emails in English. Here are the steps for fully translating the content:
Step 1: Edit the general email layout
php artisan vendor:publish
This will copy the email templates to your application and you’ll now find them in /resources/views/vendor/notifications directory. This particular layout contains some generic strings like “Hello”, “Regards”, … and you should translate those.
Step 2: Create ResetPassword notification class
php artisan make:notification ResetPassword
Make sure the class extends Illuminate\Auth\Notifications\ResetPassword:
use Illuminate\Auth\Notifications\ResetPassword as ResetPasswordNotification;
class ResetPassword extends ResetPasswordNotification
{
And update toMail function with the translation of the content of the notification:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Promjena lozinke - ' . config('app.name'))
->line('Ovaj e-mail Vam je poslat jer ste zahtijevali promjenu lozinke za vaš nalog.')
->action('Promjena lozinke', url('password/reset', $this->token))
->line('Ukoliko nijeste zahtijevali promjenu lozinke, ignorišite ovaj e-mail.');
}
(You can open toMail function of the parent class to see the actual content in English)
Step 3: Update User class to use the new notification
You need to override sendPasswordResetNotification method in User.php class and make it look like this:
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPassword($token));
}
Make sure you import your new ResetPassword class in User.php.
Done
That’s it, the email for resetting password is now translated. In this example I was setting fixed language strings, but you could use Laravel’s localisation to make sure multiple languages are supported