From 6565ac6b4194e9d37ee5a74259e24265c94cf561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Tue, 19 Dec 2023 12:51:17 -0500 Subject: [PATCH] [Mailer] twig trans support same parameter as built-in trans --- packages/mailer/Twig/TranslationExtension.php | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/packages/mailer/Twig/TranslationExtension.php b/packages/mailer/Twig/TranslationExtension.php index 0d208b18a..092e5e34e 100644 --- a/packages/mailer/Twig/TranslationExtension.php +++ b/packages/mailer/Twig/TranslationExtension.php @@ -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; @@ -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]; @@ -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;