Search code examples
phpvalidationzend-formzend-framework2zend-translate

Zend Framework 2 - Translate Standard Form Validation and Error messages


I'm writing a complete German application and therefore need to set basically everything to German.

My question: What is the best and easiest way to set for example the form validation to German?

I found this page but couldn't figure out how to get this code working:

Zend_Validate_Abstract::setDefaultTranslator($translate);

Could anyone give me some advice how to use this?

Edit:

Thanks to @Gordon I put the following into my Application/Module.php:

use Zend\I18n\Translator\Translator;
use Zend\Validator\AbstractValidator;

class Module
{
public function onBootstrap(MvcEvent $e)
{
    ...

     $translator = new Translator();
     $translator->addTranslationFile(
      'phpArray',
      'resources/languages/de.php',
      'default',
      'de_DE'
     );
         AbstractValidator::setDefaultTranslator($translator);
    ...
}

Edit 2: Alright, this is odd. When I set de_DE I get the message that the de.php file couldn't be opened - which is true because "de" is a folder containing two other PHP files.

Could not open file resources/languages/de.php for reading

Altering the path to the folder or to any existing file within it doesnt help...

When I change the "de_DE" to "de" or "de_de" then nothing happens. No error and English validation errors. Any clues?


Solution

  • Finally I found with help of @Gordon the answer!

    I put the following into my Application/Module.php:

    use Zend\I18n\Translator\Translator;
    use Zend\Validator\AbstractValidator;
    
    class Module
    {
    public function onBootstrap(MvcEvent $e)
    {
        ...
    
         $translator = new Translator();
         $translator->addTranslationFile(
          'phpArray',
          'vendor/zendframework/zendframework/resources/languages/Zend_Validate.php',
          'default',
          'de_DE'
         );
         AbstractValidator::setDefaultTranslator($translator);
        ...
    }
    

    Then you need to enable php5-intl. Go to php.ini and enable extension=php_intl.dll.

    Finally I needed to add the full path (starting with vendor) in the funciton provided by Gordon and the docs.