Last Updated: February 25, 2016
·
1.825K
· yannux

ZF2 - Validator - Global translation

Based on ZF2 Skeleton Application and french translation from zendframework

Copy vendor/zendframework/ressources/languages/fr/Zend_Captcha.php and Zend_Validate.php to data/languages/fr_FR.php (both contents in one file, better to update message like you want)


// module/Application/module.config.php
'translator' => array(
        'locale' => 'fr_FR',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
            array( // Add data/language dir as phpArray translation file 
                'type'     => 'phpArray',
                'base_dir' => 'data/language',
                'pattern'  => '%s.php',
            ),
        ),
    ),

In module/Application/Module.php , OnBootstrap method, set the translator for default validation messages


public function onBootstrap(MvcEvent $e)
{
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);

        // Set the translator for default validation messages
        $serviceManager = $e->getApplication()->getServiceManager();
        $translator = $serviceManager->get('translator');
        \Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
}