Search code examples
validationsymfonytranslation

Symfony translation inside validation won't work


If I try to use translations inside validation like described here http://inchoo.net/tools-frameworks/symfony2-translating-validator-messages/

I get always

not.blank.username

as output on Error. It seems like symfony didn't find the message-translation. I placed all my translationfiles inside app/ressources/translations and they are named as

messages.de.yml
....

What am I doing wrong? Do I have to place the translationfiles insde each Bundle?

Kind regards

EDIT: Problem ist clear and fixed, but when I try to use "MinLength" I get an strange error:

Attempted to load class "MinLength" from namespace 
"Symfony\Component\Validator\Constraints" in /var/www/symfony/webprojekt/vendor/symfony/symfony/src/Symfony/Component/Validator/Mapping/Loader/AbstractLoader.php line 64. 
Do you need to "use" it from another namespace? 

I already included

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\MaxLength;

AM I missing something?


Solution

  • To translate validation messages you need to create files with the following name structure inside translations folder:

    validators.LANG.yml
    

    Also, you can check in your config.yml, inside framework, if exists the translator:

    framework:
        translator: { fallback: %locale% }
    

    This is required.