Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 translation of validation messages issue

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?

like image 781
Jay Avatar asked Jan 11 '15 08:01

Jay


1 Answers

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.

like image 125
acontell Avatar answered Oct 22 '22 15:10

acontell