Symfony2 translating validator messages

Featured Image

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:

            - NotBlank: { message: "not.blank.first_name" }
            - NotBlank: { message: "not.blank.last_name" }
            - NotBlank:  { message: "" }
            - Email: { message: "Please provide valid E-mail address" }
            - NotBlank:  { message: "not.blank.username" }
            - MinLength: {limit: 6, message: "min.length.username"}
NotBlank:  { message: "not.blank.password" }

Next procedure is same as for regular messages:

Inside messages.{_locale}.yml

    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();
        $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

Enjoy with your Symfony2! 🙂


  1. 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!

  2. Yeah, I guess researching, doing things in different ways and again researching is the only option on a way to tame Symfony2 =)

  3. I agree with you, but I used this article just to show other possibilities . Thank you for your comment. 🙂

  4. Of course if you want to use xliff. But, I personally, don’t. Thank you for your comment, @Wizz. 🙂

  5. You can also create a validators.en.xliff for example in your translation directory and you will not need to use the translator service.

  6. @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 🙂

  7. 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!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.