Skip to content

Commit

Permalink
Localize DateTime based on intl extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Jan 17, 2025
1 parent 7eb2cab commit 6e33a4e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Application/Handler/SendInviteMailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __invoke(SendInviteMailCommand $command, AuthContext $authContex
],
'footer' => [
'hints' => [
$this->translator->get($locale, 'mail.inviteUser.hints.validity', [$expiresAt->format('d.m.Y H:i')]),
$this->translator->get($locale, 'mail.inviteUser.hints.validity', [$this->translator->getLocalizedDateTime($locale, $expiresAt)]),
$this->translator->get($locale, 'mail.inviteUser.hints.disclosure')
]
]
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Handler/SendPasswordResetMailHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __invoke(SendPasswordResetMailCommand $command): array
],
'footer' => [
'hints' => [
$this->translator->get($locale, 'mail.resetPassword.hints.validity', [$expiresAt->format('d.m.Y H:i')]),
$this->translator->get($locale, 'mail.resetPassword.hints.validity', [$this->translator->getLocalizedDateTime($locale, $expiresAt)]),
$this->translator->get($locale, 'mail.resetPassword.hints.disclosure'),
$this->translator->get($locale, 'mail.resetPassword.hints.flooding')
]
Expand Down
16 changes: 16 additions & 0 deletions src/Application/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

namespace HexagonalPlayground\Application;

use DateTimeInterface;
use IntlDateFormatter;
use RuntimeException;

class Translator
{
/** @var string[][] */
private array $translations;

/** @var IntlDateFormatter[] */
private array $dateFormatters;

public function __construct()
{
$this->translations = [];
$this->dateFormatters = [];
}

public function get(string $locale, string $key, array $params = []): string
Expand All @@ -37,6 +44,15 @@ public function get(string $locale, string $key, array $params = []): string
return $value;
}

public function getLocalizedDateTime(string $locale, DateTimeInterface $dateTime): string
{
if (!isset($this->dateFormatters[$locale])) {
$this->dateFormatters[$locale] = new IntlDateFormatter($locale, IntlDateFormatter::LONG, IntlDateFormatter::LONG);
}

return $this->dateFormatters[$locale]->format($dateTime);
}

private function flattenArray(array $array, string $parentKey = ''): array
{
$flattened = [];
Expand Down

0 comments on commit 6e33a4e

Please sign in to comment.