Skip to content

Commit

Permalink
bug #48833 [Translation] Handle the translation of empty strings (jav…
Browse files Browse the repository at this point in the history
…iereguiluz)

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
-------

f19557953a [Translation] Handle the translation of empty strings
  • Loading branch information
nicolas-grekas committed Feb 23, 2023
2 parents 019f98c + 4b4b345 commit 8e2eaad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Extension/TranslationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public function trans($message, $arguments = [], string $domain = null, string $
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)));
}

if ($message instanceof TranslatableMessage && '' === $message->getMessage()) {
return '';
}

return $message->trans($this->getTranslator(), $locale ?? (\is_string($arguments) ? $arguments : null));
}

Expand Down
1 change: 1 addition & 0 deletions Tests/Extension/TranslationExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public static function getTransTests()
['{{ foo|trans }}', '', ['foo' => null]],

// trans object
['{{ t("")|trans }}', ''],
['{{ t("Hello")|trans }}', 'Hello'],
['{{ t(name)|trans }}', 'Symfony', ['name' => 'Symfony']],
['{{ t(hello, { \'%name%\': \'Symfony\' })|trans }}', 'Hello Symfony', ['hello' => 'Hello %name%']],
Expand Down

0 comments on commit 8e2eaad

Please sign in to comment.