Skip to content

Commit

Permalink
[Mailer] twig trans support same parameter as built-in trans
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Dec 19, 2023
1 parent 4a9800a commit 6565ac6
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/mailer/Twig/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Draw\Component\Mailer\Twig;

use Symfony\Component\Translation\TranslatableMessage;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
Expand All @@ -19,10 +21,7 @@ public function getFilters(): array
];
}

/**
* @param string|string[] $messages
*/
public function trans(string|array $messages, array $arguments = [], ?string $domain = null, ?string $locale = null, ?int $count = null): ?string
public function trans(null|string|\Stringable|array|TranslatableInterface $messages, string|array $arguments = [], ?string $domain = null, ?string $locale = null, ?int $count = null): ?string
{
if (!\is_array($messages)) {
$messages = [$messages];
Expand All @@ -34,6 +33,20 @@ public function trans(string|array $messages, array $arguments = [], ?string $do

$result = reset($messages);
foreach ($messages as $message) {
if ($message instanceof TranslatableInterface) {
if ($message instanceof TranslatableMessage && '' === $message->getMessage()) {
return '';
}

$result = $message->trans($this->translator, $locale ?? (\is_string($arguments) ? $arguments : null));

if ($result != $message) {
return $result;
}

continue;
}

$result = $this->translator->trans($message, $arguments, $domain, $locale);
if ($result != $message) {
return $result;
Expand Down

0 comments on commit 6565ac6

Please sign in to comment.