3

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 !

David Noreña
  • 3,951
  • 1
  • 28
  • 43

2 Answers2

3

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'
            )
        )
    )
);
Diemuzi
  • 3,507
  • 7
  • 36
  • 61
0

You cannot set the translator by this way.

Use instead this static method :

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

Hope this helps.

BADAOUI Mohamed
  • 2,146
  • 1
  • 17
  • 14
  • yeah i see something like that but sorry i'm just learning zend i'm noob could you say me please where to put that code you give me ....... thanks ... – David Noreña Dec 30 '12 at 19:59
  • For your test, you can put it just after having instantiate your translator. But it should be better in the Module.php of application module in order to load the validation's translations for all modules. – BADAOUI Mohamed Dec 30 '12 at 20:14