Skip to content

Commit

Permalink
Nullable message id?
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Sep 3, 2019
1 parent 959eb56 commit 55eac63
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
Expand Up @@ -124,7 +124,7 @@
{{- block('form_widget_simple') -}}
{%- set label_attr = label_attr|merge({ class: (label_attr.class|default('') ~ ' custom-file-label')|trim }) -%}
<label for="{{ form.vars.id }}" {% with { attr: label_attr } %}{{ block('attributes') }}{% endwith %}>
{%- if attr.placeholder is defined -%}
{%- if attr.placeholder is defined and attr.placeholder is not none -%}
{{- translation_domain is same as(false) ? attr.placeholder : attr.placeholder|trans({}, translation_domain) -}}
{%- endif -%}
</label>
Expand Down
Expand Up @@ -141,7 +141,7 @@ public function getCollectedMessages()
return $this->messages;
}

private function collectMessage(?string $locale, ?string $domain, string $id, string $translation, ?array $parameters = [])
private function collectMessage(?string $locale, ?string $domain, ?string $id, string $translation, ?array $parameters = [])
{
if (null === $domain) {
$domain = 'messages';
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Translation/LoggingTranslator.php
Expand Up @@ -131,7 +131,7 @@ public function __call($method, $args)
/**
* Logs for missing translations.
*/
private function log(string $id, ?string $domain, ?string $locale)
private function log(?string $id, ?string $domain, ?string $locale)
{
if (null === $domain) {
$domain = 'messages';
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Translation/Tests/TranslatorTest.php
Expand Up @@ -491,6 +491,19 @@ public function testTransChoiceNullLocale()
$this->addToAssertionCount(1);
}

public function testTransNullId()
{
$translator = new Translator('en');
$translator->addLoader('array', new ArrayLoader());
$translator->addResource('array', ['foo' => 'foofoo'], 'en');

$this->assertSame('', $translator->trans(null));

(\Closure::bind(function () use ($translator) {
$this->assertSame([], $translator->catalogues);
}, $this, Translator::class))();
}

public function getTransFileTests()
{
return [
Expand All @@ -512,6 +525,7 @@ public function getTransTests()
['Symfony est super !', 'Symfony is great!', 'Symfony est super !', [], 'fr', ''],
['Symfony est awesome !', 'Symfony is %what%!', 'Symfony est %what% !', ['%what%' => 'awesome'], 'fr', ''],
['Symfony est super !', new StringClass('Symfony is great!'), 'Symfony est super !', [], 'fr', ''],
['', null, '', [], 'fr', ''],
];
}

Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/Translation/Translator.php
Expand Up @@ -210,11 +210,14 @@ public function getFallbackLocales()
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
if ('' === $id = (string) $id) {
return '';
}

if (null === $domain) {
$domain = 'messages';
}

$id = (string) $id;
$catalogue = $this->getCatalogue($locale);
$locale = $catalogue->getLocale();
while (!$catalogue->defines($id, $domain)) {
Expand Down Expand Up @@ -242,6 +245,10 @@ public function transChoice($id, $number, array $parameters = [], $domain = null
{
@trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the trans() one instead with a "%%count%%" parameter.', __METHOD__), E_USER_DEPRECATED);

if ('' === $id = (string) $id) {
return '';
}

if (!$this->formatter instanceof ChoiceMessageFormatterInterface) {
throw new LogicException(sprintf('The formatter "%s" does not support plural translations.', \get_class($this->formatter)));
}
Expand All @@ -250,7 +257,6 @@ public function transChoice($id, $number, array $parameters = [], $domain = null
$domain = 'messages';
}

$id = (string) $id;
$catalogue = $this->getCatalogue($locale);
$locale = $catalogue->getLocale();
while (!$catalogue->defines($id, $domain)) {
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Contracts/Translation/TranslatorTrait.php
Expand Up @@ -43,7 +43,9 @@ public function getLocale()
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
$id = (string) $id;
if ('' === $id = (string) $id) {
return '';
}

if (!isset($parameters['%count%']) || !is_numeric($parameters['%count%'])) {
return strtr($id, $parameters);
Expand Down

0 comments on commit 55eac63

Please sign in to comment.