8000 bug #48833 [Translation] Handle the translation of empty strings (jav… · symfony/symfony@cfda9b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfda9b4

Browse files
bug #48833 [Translation] Handle the translation of empty strings (javiereguiluz)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Translation] Handle the translation of empty strings | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - In some apps, you might use an expression as the value of some `TranslatableMessage` object. The result of this expression can result in using an empty string as the value of the that object: ```php new TranslatableMessage($someCondition ? $someObject->someMethod() : '') ``` The issue is that Symfony will report that empty string in the list of "missing translations" (like in the first row of this image): ![](https://user-images.githubusercontent.com/73419/209708276-2c321204-4e7b-40c6-9386-0779a7ac4bf7.png) I think this is a bug and an empty string should just be output "as is" without reporting it as missing. What do you think? Is this truly a bug? Would it be a new feature for 6.3? The current behavior is correct and we should close without merging? Thanks. Commits ------- f195579 [Translation] Handle the translation of empty strings
2 parents 2a633cf + f195579 commit cfda9b4

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/Symfony/Bridge/Twig/Extension/TranslationExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ public function trans($message, $arguments = [], string $domain = null, string $
116116
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a locale passed as a string when the message is a "%s", "%s" given.', __METHOD__, TranslatableInterface::class, get_debug_type($arguments)));
117117
}
118118

119+
if ($message instanceof TranslatableMessage && '' === $message->getMessage()) {
120+
return '';
121+
}
122+
119123
return $message->trans($this->getTranslator(), $locale ?? (\is_string($arguments) ? $arguments : null));
120124
}
121125

src/Symfony/Bridge/Twig/Tests/Extension/TranslationExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public static function getTransTests()
124124
['{{ foo|trans }}', '', ['foo' => null]],
125125

126126
// trans object
127+
['{{ t("")|trans }}', ''],
127128
['{{ t("Hello")|trans }}', 'Hello'],
128129
['{{ t(name)|trans }}', 'Symfony', ['name' => 'Symfony']],
129130
['{{ t(hello, { \'%name%\': \'Symfony\' })|trans }}', 'Hello Symfony', ['hello' => 'Hello %name%']],

0 commit comments

Comments
 (0)
0