Q: ZF 2 Form Validation Translations I use ZF 2.4 and I would like to change form validation messages to German, so I used code compatible with doc - https://framework.zend.com/manual/2.4/en/modules/zend.validator.messages.html
$translator = new \Zend\Mvc\I18n\Translator();
$translator->addTranslationFile(
    'phpArray',
    './vendor/zendframework/zendframework/resources/languages/en/Zend_Validate.php', //or Zend_Captcha
    'default',
    'de_DE'
);
\Zend\Validator\AbstractValidator::setDefaultTranslator($translator);

Unfortunatelly EN is used still.. For example message "Value is required and can't be empty" is displayed from vendor/zendframework/zendframework/library/Zend/Validator/NotEmpty.php and isn't translated from vendor/zendframework/zendframework/resources/languages/de/Zend_Validate.php
No errors are there...
Could somebody help me, please? This is very important for me...

A: The translator is still pointing to the Locale of EN. What you want to do is set the translator Locale based on the user settings or maybe even a route parameter. To do that use the setLocale($locale) on your Translator.
So you could do this within your Application\Module.php:
$language = $event->getRouteMatch()->getParam('language', 'en_US');
$serviceManager = $event->getApplication()->getServiceManager();
$translator = $serviceManager->get('translator');
$translator
    ->setLocale($language)
    ->setFallbackLocale('en_US')
    ->addTranslationFilePattern(
        'phpArray',
        \Zend\I18n\Translator\Resources::getBasePath(),
        \Zend\I18n\Translator\Resources::getPatternForValidator()
    );

AbstractValidator::setDefaultTranslator($translator);

It is up to you to where you get the language locale from. If you don't have a route param defined for the language but want to use the user settings for example:
$language = 'en_US';
$authService = $auth = $serviceManager->get('zfcuser_auth_service');
if ($authService->hasIdentity()) {
    $language = $authService->getIdentity()->getUserSettings()->getLanguage();
}


A: Haa, it works when I used below code:
    public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);
$serviceManager = $e->getApplication()->getServiceManager();
    $translator = $serviceManager->get('translator');

    //$locale = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    $locale = 'de_DE';
    //$locale = 'en_US';

    $translator->setLocale(\Locale::acceptFromHttp($locale));
    $translator->addTranslationFile(
        'phpArray',
        './vendor/zendframework/zendframework/resources/languages/de/Zend_Validate.php',
        'default',
        'de_DE'
    );
    \Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
}

I don't understand why it wasn't working before..
