I have a common problem with translating validator messages in Symfony, and all suggested solutions do not help me. This is my constraint:
// src/AppBundle/Entity/Friend.php
/**
* @var string
*
* @Assert\NotBlank(message = "test")
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
And file with translations:
// src/AppBundle/Resources/translations/validators.en.yml
test: my message
The same file with translations i've also added in app directory. Actually, it doesn't work. What am i missing?
If you follow this steps, it should work:
First, turn on translation system:
# app/config/config.yml
framework:
translator: { fallback: en }
Create the constraint as you did:
// src/AppBundle/Entity/Friend.php
use Symfony\Component\Validator\Constraints as Assert;// Don't forget this part.
class Friend
{
/**
* @var string
* @Assert\NotBlank(message = "test")
* @ORM\Column(name="name", type="string", length=255)
*/
public $name;
}
Create a translation file under the validators catalog for the constraint messages, typically in the Resources/translations/ directory of the bundle as you did.
# validators.en.yml
test: my message
IMPORTANT Last, clear your cache as you've added new translations (do it even if you're in dev environment).
$ php app/console cache:clear
For me this solution works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With