.. index:: single: Validation; Translation How to Translate Validation Constraint Messages =============================================== If you're using validation constraints with the Form component, you can translate the error messages by creating a translation resource for the ``validators`` :ref:`domain `. To start, suppose you've created a plain-old-PHP object that you need to use somewhere in your application:: // src/Entity/Author.php namespace App\Entity; class Author { public $name; } Add constraints through any of the supported methods. Set the message option to the translation source text. For example, to guarantee that the ``$name`` property is not empty, add the following: .. configuration-block:: .. code-block:: php-annotations // src/Entity/Author.php use Symfony\Component\Validator\Constraints as Assert; class Author { /** * @Assert\NotBlank(message="author.name.not_blank") */ public $name; } .. code-block:: yaml # config/validator/validation.yaml App\Entity\Author: properties: name: - NotBlank: { message: 'author.name.not_blank' } .. code-block:: xml .. code-block:: php // src/Entity/Author.php // ... use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Mapping\ClassMetadata; class Author { public $name; public static function loadValidatorMetadata(ClassMetadata $metadata) { $metadata->addPropertyConstraint('name', new NotBlank([ 'message' => 'author.name.not_blank', ])); } } Now, create a ``validators`` catalog file in the ``translations/`` directory: .. configuration-block:: .. code-block:: xml author.name.not_blank Please enter an author name. .. code-block:: yaml # translations/validators.en.yaml author.name.not_blank: Please enter an author name. .. code-block:: php // translations/validators.en.php return [ 'author.name.not_blank' => 'Please enter an author name.', ]; You may need to clear your cache (even in the dev environment) after creating this file for the first time.