Symfony2 translating validator messages

After some digging and searching, I found the way that suit me best for translating messages from validation config file. I like to use yml format so let me show how I did it:
#Surgeworks\CoreBundle\Resources\config\validation.yml
#...
Surgeworks\CoreBundle\Entity\User:
properties:
firstName:
- NotBlank: { message: "not.blank.first_name" }
lastName:
- NotBlank: { message: "not.blank.last_name" }
email:
- NotBlank: { message: "not.blank.email" }
- Email: { message: "Please provide valid E-mail address" }
username:
- NotBlank: { message: "not.blank.username" }
- MinLength: {limit: 6, message: "min.length.username"}
password:
NotBlank: { message: "not.blank.password" }
#...
Next procedure is same as for regular messages:
Inside messages.{_locale}.yml
#Surgeworks\CoreBundle\Resources\translations\messages.en.yml
#...
not:
blank:
first_name: First name can not be blank!
last_name: last name can not be blank!
email: E-mail can not be blank!
username: Username field can not be blank!
password: Password field can not be blank!
#...
And use it inside controller:
//...
$user = new User();
$user->setFirstName($first_name);
$user->setLastName($last_name);
$user->setEmail($email);
$user->setUsername($username);
//...
$errorList = $this->get('validator')->validate($user);
$msg = "";
if (count($errorList) > 0) {
foreach ($errorList as $err) {
$msg.= $this->get('translator')->trans($err->getMessage()) . "\n";
}
return $msg; //translated error message using your translations
}
$this->getEm()->persist($user);
$this->getEm()->flush();
//...
Enjoy with your Symfony2! 🙂
11 comments
Your article is outdeted, see
http://symfony.com/doc/current/book/translation.html#translating-constraint-messages
perfect 🙂 just what I needed!
Hi, can you tell me why does only working in app_dev.php, (Translating. Sorry my english is weak)
I think it’s great that you took the time to write this post, but the approach you illustrate here is more difficult than just doing it the right way!
Please update your post to show that you don’t need steps one or three — all you need is a validators.{locale}.{yml|php|xliff} with translated strings.
You can still use step one if you want to create keyword-based translations, but you certainly don’t need to run all the messages through the translator again — they’ve already been through once!
Yeah, I guess researching, doing things in different ways and again researching is the only option on a way to tame Symfony2 =)
I agree with you, but I used this article just to show other possibilities . Thank you for your comment. 🙂
You can use any format you wish – xliff, php or yaml. Using validators domain is a proper way to translate messages. Check Stof answers here
http://groups.google.com/group/symfony2/browse_thread/thread/d94eba65ed8feb8e
Of course if you want to use xliff. But, I personally, don’t. Thank you for your comment, @Wizz. 🙂
You can also create a validators.en.xliff for example in your translation directory and you will not need to use the translator service.
@Fernando,sorry, my mistake. I accidentally copied the code from my ‘Admin’ service where I implemented private methods: getValidator() and getTranslator() for this. I changed the source code in the post. Thanks for comment 🙂
Hi!
Inside the Controller we have no “getValidator” or “getTranslator” functions.
So you have to change to $this->get(‘validator’) and $this->get(‘translator’).
But very nice tip!
Regards,
Fernando