Skip to content

Commit

Permalink
fix translating file validation error message
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Apr 7, 2019
1 parent 62e5a91 commit e532709
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -93,6 +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">
<argument type="service" id="translator" on-invalid="ignore" />
<deprecated>The "%service_id%" service is deprecated since Symfony 3.1 and will be removed in 4.0.</deprecated>
</service>
<service id="form.type.hidden" class="Symfony\Component\Form\Extension\Core\Type\HiddenType" public="true">
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/FileType.php
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;

class FileType extends AbstractType
{
Expand All @@ -32,6 +33,13 @@ class FileType extends AbstractType
self::MIB_BYTES => 'MiB',
];

private $translator;

public function __construct(TranslatorInterface $translator = null)
{
$this->translator = $translator;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -150,7 +158,13 @@ private function getFileUploadError($errorCode)
$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);
}

/**
Expand Down

0 comments on commit e532709

Please sign in to comment.