Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Translate Assertion message from entity annotation

Tags:

symfony

I have an entity with some validation and assertion messages. I need to translate that messages into arabic i have a messages.ar.xlf file The validation message from the Entity is not translated!

    <trans-unit id="1">
        <source>Enter.Car.Details</source>
        <target>بيانات السيارة</target>
    </trans-unit> 

and here's my Entity

/**
 * @var string
 * @Assert\Length(max = "4", maxMessage = "Enter.Car.Details")
 * @Assert\NotBlank(message="Enter.Car.Details")
 * @ORM\Column(name="year_made", type="integer", length=4, nullable=true)
 */
private $yearMade;

and here's my config.yml

framework:
    translator:      { fallback: ar }

Note :- I translated some words in my twig file to check if the local and messages.ar.xlf file are working or not, and it's all working except the validation messages coming from the Entity.

like image 682
Essam Khaled Avatar asked Sep 08 '13 08:09

Essam Khaled


1 Answers

The validation messages are get not from messages domain, but from validators one.

So create validators.ar.xlf file and put your validation messages there and should be working (see the official Symfony2 documentation for reference).

One more thing. Depending on what Symfony version do you use: maxLength assertion id deprecated from Symfony 2.1 and is removed in 2.3. So, if you use Symfony >2.0, then you should use Length assertion instead.

like image 74
Cyprian Avatar answered Sep 21 '22 23:09

Cyprian