zend-formzend-framework2zend-translate

Zend Framework 2 translate form errors doesnt work


I spent all night trying to figure out why this code doesn't work

$inputFilter = new InputFilter();
$factory = new InputFactory();

$translator = new \Zend\I18n\Translator\Translator();
//Copy of file found in resources/languages/es/Zend_Validate.php
$translator->addTranslationFile('phparray', './module/Product/language/zend_validate.php');

$inputFilter->add(
    $factory->createInput(
        array(
            'name' => 'idpro',
            'required' => true,
            'filters' => array(
                array('name' => 'Int'),
            ),
        )
    )
);

$inputFilter->add(
    $factory->createInput(
        array(
            'name' => 'nompro',
            'validators' => array(
                array(
                    'name' => 'EmailAddress',
                    'options' => array(
                        'translator' => $translator
                    )
                ),
            ),
        )
    )
);

I've installed php5-intl and enabled it in the php.ini

but it doesnt work i get the same message Value is required and can't be empty but it should be the translated one ...

Thank you very much !


Solution

  • Here is what I did to make Form Errors Translate properly:

    module.config.php

    <?php
    return array(
        'service_manager' => array(
            'factories' => array(
                'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory'
            )
        ),
        'translator' => array(
            'locale' => 'en',
            'translation_file_patterns' => array(
                array(
                    'type'        => 'gettext',
                    'base_dir'    => __DIR__ . '/../language',
                    'pattern'     => '%s.mo',
                    'text_domain' => 'admin'
                ),
                array(
                    'type'        => 'phparray',
                    'base_dir'    => __DIR__ . '/../resources/languages',
                    'pattern'     => '/%s/Zend_Validate.php',
                    'text_domain' => 'default'
                )
            )
        )
    );