From 039fc80d6c6615b611d5716eab241434763113a1 Mon Sep 17 00:00:00 2001 From: Flinsch Date: Tue, 25 Aug 2020 14:44:21 +0200 Subject: [PATCH] [TwigBridge] Fix #37931: BC break where filter method `trans` did not allow null values for `$message` parameter anymore --- src/Symfony/Bridge/Twig/Extension/TranslationExtension.php | 6 +++++- .../Twig/Tests/Extension/TranslationExtensionTest.php | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php b/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php index ad16041356..d65578608d 100644 --- a/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php +++ b/src/Symfony/Bridge/Twig/Extension/TranslationExtension.php @@ -92,8 +92,12 @@ final class TranslationExtension extends AbstractExtension return $this->translationNodeVisitor ?: $this->translationNodeVisitor = new TranslationNodeVisitor(); } - public function trans(string $message, array $arguments = [], string $domain = null, string $locale = null, int $count = null): string + public function trans(?string $message, array $arguments = [], string $domain = null, string $locale = null, int $count = null): string { + if (null === $message || '' === $message) { + return ''; + } + if (null !== $count) { $arguments['%count%'] = $count; } diff --git a/src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php b/src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php index f384fa59c5..28149e1315 100644 --- a/src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php @@ -118,6 +118,10 @@ class TranslationExtensionTest extends TestCase ['{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples"|trans(count=count) }}', 'There is 5 apples', ['count' => 5]], ['{{ text|trans(count=5, arguments={\'%name%\': \'Symfony\'}) }}', 'There is 5 apples (Symfony)', ['text' => '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)']], ['{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples"|trans({}, "messages", "fr", count) }}', 'There is 5 apples', ['count' => 5]], + + // trans filter with null message + ['{{ null|trans }}', ''], + ['{{ foo|trans }}', '', ['foo' => null]], ]; }