bug #30961 [Form] fix translating file validation error message (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Form] fix translating file validation error message

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

I messed this up in #30895.

#FOSSHackathons #EUFOSSA

Commits
-------

9c41842756 fix translating file validation error message
This commit is contained in:
Fabien Potencier 2019-04-07 11:45:40 +02:00
commit c82e2df9ff
2 changed files with 16 additions and 2 deletions

View File

@ -93,7 +93,7 @@
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
</service>
<service id="form.type.file" class="Symfony\Component\Form\Extension\Core\Type\FileType" public="true">
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
<argument type="service" id="translator" on-invalid="ignore" />
</service>
<service id="form.type.hidden" class="Symfony\Component\Form\Extension\Core\Type\HiddenType" public="true">
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>

View File

@ -20,6 +20,7 @@ use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
class FileType extends AbstractType
{
@ -32,6 +33,13 @@ class FileType extends AbstractType
self::MIB_BYTES => 'MiB',
];
private $translator;
public function __construct(TranslatorInterface $translator = null)
{
$this->translator = $translator;
}
/**
* {@inheritdoc}
*/
@ -150,7 +158,13 @@ class FileType extends AbstractType
$messageTemplate = 'The file could not be uploaded.';
}
return new FormError($messageTemplate, $messageTemplate, $messageParameters);
if (null !== $this->translator) {
$message = $this->translator->trans($messageTemplate, $messageParameters);
} else {
$message = strtr($messageTemplate, $messageParameters);
}
return new FormError($message, $messageTemplate, $messageParameters);
}
/**