Skip to content

Commit

Permalink
Extract common code from email notifications to HasTranslations trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaszkadziela committed Jun 18, 2024
1 parent 3b57c09 commit 77734f4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 41 deletions.
37 changes: 14 additions & 23 deletions app/Notifications/AccountCreatedViaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@

namespace App\Notifications;

use App\Notifications\Traits\HasTranslations;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;

class AccountCreatedViaProvider extends Notification
{
public const LANG = [
use HasTranslations;

protected array $lang = [
'en' => [
'action' => 'Log in your account',
'line-1' => 'Welcome to EverestServer! An account has been created for you via **{{ $provider }}**.',
'line-2' => 'We automatically generated the **{{ $password }}** password for you. For safety reasons, please change it as soon as possible.',
'line-3' => 'You can access your account via **{{ $provider }}** or with **{{ $email }}** e-mail address and the password mentioned above.',
'line-1' => 'Welcome to EverestServer! An account has been created for you via **:provider**.',
'line-2' => 'We automatically generated the **:password** password for you. For safety reasons, please change it as soon as possible.',
'line-3' => 'You can access your account via **:provider** or with **:email** e-mail address and the password mentioned above.',
'line-4' => 'If you did not create an account, you can ignore this message.',
'subject' => 'EverestServer - Account created',
],
'pl' => [
'action' => 'Zaloguj się na swoje konto',
'line-1' => 'Witamy w EverestServer! Utworzono dla Ciebie konto za pośrednictwem **{{ $provider }}**.',
'line-2' => 'Automatycznie wygenerowaliśmy dla Ciebie hasło **{{ $password }}**. Ze względów bezpieczeństwa prosimy o jak najszybszą zmianę.',
'line-3' => 'Możesz uzyskać dostęp do swojego konta poprzez **{{ $provider }}** lub za pomocą adresu e-mail **{{ $email }}** i hasła wymienionego powyżej.',
'line-1' => 'Witamy w EverestServer! Utworzono dla Ciebie konto za pośrednictwem **:provider**.',
'line-2' => 'Automatycznie wygenerowaliśmy dla Ciebie hasło **:password**. Ze względów bezpieczeństwa prosimy o jak najszybszą zmianę.',
'line-3' => 'Możesz uzyskać dostęp do swojego konta poprzez **:provider** lub za pomocą adresu e-mail **:email** i hasła wymienionego powyżej.',
'line-4' => 'Jeśli nie utworzyłeś konta, zignoruj tę wiadomość.',
'subject' => 'EverestServer - Utworzono konto',
],
];

private array $placeholdersMap = [];

/**
* Create a notification instance.
*/
public function __construct(public string $provider, public string $email, public string $password)
public function __construct(private string $provider, private string $email, private string $password)
{
$this->placeholdersMap = [
'{{ $provider }}' => $provider,
'{{ $email }}' => $email,
'{{ $password }}' => $password,
':provider' => $provider,
':email' => $email,
':password' => $password,
];
}

Expand All @@ -63,12 +62,4 @@ public function toMail(): MailMessage
->line($this->lang('line-4'))
->action($this->lang('action'), route('login'));
}

/**
* Get translated text and replace placeholders with actual content.
*/
private function lang(string $key): string
{
return Str::swap($this->placeholdersMap, self::LANG[Lang::getLocale()][$key]);
}
}
34 changes: 23 additions & 11 deletions app/Notifications/ResetPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace App\Notifications;

use App\Notifications\Traits\HasTranslations;
use Illuminate\Auth\Notifications\ResetPassword as BaseResetPassword;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;

class ResetPassword extends BaseResetPassword
{
public const LANG = [
use HasTranslations;

protected array $lang = [
'en' => [
'action' => 'Reset password',
'line-1' => 'You are receiving this email because we received a password reset request for your account.',
Expand All @@ -27,18 +28,29 @@ class ResetPassword extends BaseResetPassword
];

/**
* Get the verify email notification mail message for the given URL.
* Create a notification instance.
*/
public function __construct(string $token)
{
parent::__construct($token);

$this->placeholdersMap = [
':count' => config('auth.passwords.' . config('auth.defaults.passwords') . '.expire'),
];
}

/**
* Get the reset password notification mail message for the given URL.
*
* @param string $url
* @return MailMessage
*/
protected function buildMailMessage($url)
protected function buildMailMessage($url): MailMessage
{
return (new MailMessage())
->subject(self::LANG[Lang::getLocale()]['subject'])
->line(self::LANG[Lang::getLocale()]['line-1'])
->action(self::LANG[Lang::getLocale()]['action'], $url)
->line(Str::replace(':count', config('auth.passwords.' . config('auth.defaults.passwords') . '.expire'), self::LANG[Lang::getLocale()]['line-2']))
->line(self::LANG[Lang::getLocale()]['line-3']);
->subject($this->lang('subject'))
->line($this->lang('line-1'))
->action($this->lang('action'), $url)
->line($this->lang('line-2'))
->line($this->lang('line-3'));
}
}
23 changes: 23 additions & 0 deletions app/Notifications/Traits/HasTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Notifications\Traits;

use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Str;

/**
* @property array $lang
* @property array $placeholdersMap
*/
trait HasTranslations
{
protected array $placeholdersMap = [];

/**
* Get translated text and replace placeholders with actual content.
*/
public function lang(string $key): string
{
return Str::swap($this->placeholdersMap, $this->lang[Lang::getLocale()][$key]);
}
}
15 changes: 8 additions & 7 deletions app/Notifications/VerifyEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace App\Notifications;

use App\Notifications\Traits\HasTranslations;
use Illuminate\Auth\Notifications\VerifyEmail as BaseVerifyEmail;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;

class VerifyEmail extends BaseVerifyEmail
{
public const LANG = [
use HasTranslations;

protected array $lang = [
'en' => [
'action' => 'Verify email address',
'line-1' => 'Please click the button below to verify your email address.',
Expand All @@ -27,13 +29,12 @@ class VerifyEmail extends BaseVerifyEmail
* Get the verify email notification mail message for the given URL.
*
* @param string $url
* @return MailMessage
*/
protected function buildMailMessage($url)
protected function buildMailMessage($url): MailMessage
{
return (new MailMessage())
->subject(self::LANG[Lang::getLocale()]['subject'])
->line(self::LANG[Lang::getLocale()]['line-1'] . ' ' . self::LANG[Lang::getLocale()]['line-2'])
->action(self::LANG[Lang::getLocale()]['action'], $url);
->subject($this->lang('subject'))
->line($this->lang('line-1') . ' ' . $this->lang('line-2'))
->action($this->lang('action'), $url);
}
}

0 comments on commit 77734f4

Please sign in to comment.