Zend Framework 2 - Translate Standard Form Validation and Error messages

Ron picture Ron · Dec 19, 2012 · Viewed 9.1k times · Source

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?

Answer

Whisher picture Whisher · Feb 12, 2013

for me it works with

public function onBootstrap(MvcEvent $e)
{
    $translator=$e->getApplication()->getServiceManager()->get('translator');
    $translator->addTranslationFile(
        'phpArray',
        './vendor/zendframework/zendframework/resources/languages/it/Zend_Validate.php'

    );
    AbstractValidator::setDefaultTranslator($translator);
   // \Zend\Debug\Debug::dump($application);
}

'./vendor/zendframework/zendframework/resources/languages/langfolderyouwant/Zend_Validate.php'