David Noreña
David Noreña

Reputation: 4230

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 !

Upvotes: 3

Views: 2408

Answers (2)

Diemuzi
Diemuzi

Reputation: 3527

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'
            )
        )
    )
);

Upvotes: 3

BADAOUI Mohamed
BADAOUI Mohamed

Reputation: 2204

You cannot set the translator by this way.

Use instead this static method :

\Zend\Validator\AbstractValidator::setDefaultTranslator($translator, 'default');

Hope this helps.

Upvotes: 0

Related Questions