Felipe Weckx

Translating ZF2 validation messages

Often we use the InputFilter to validate the elements of a form create with Zend Framework 2. However, when creating a system/website which has to support multiple languages we also need to translate the validation messages. Fortunately ZF2 comes with a series of translations for the default validation classes and we can set up the translation using the \Zend\Validator\AbstractValidator::setDefaultTranslator method:

The code above has to placed inside the Module class in the modules you need to translate the messages, or in your application module to enable in all of them. Note that we added it to the onBootstrap method, which means it will be executed in all actions. That can cause a performance issue, so it is best to separate it and call only on the controllers that actually use validations.

1 comentário

Um comentário para “Translating ZF2 validation messages”

  1. $translator = new \Zend\Mvc\I18n\Translator(new \Zend\I18n\Translator\Translator());

    This just saved my life!

Leave a Reply

Your email address will not be published.