How to Translate Validation Constraint Messages
The validation constraints used in forms can translate their error messages by
creating a translation resource for the validators
translation domain.
First of all, install the Symfony translation component (if it’s not already installed in your application) running the following command:
$ composer require symfony/translation
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:
Now, create a validators catalog file in the translations/ directory:
You may need to clear your cache (even in the dev environment) after creating this file for the first time.