Arvind
Arvind

Reputation: 938

ZF2 - How To Translate Zend Form Validation Error Messages?

I have the .po and compiled .mo language files for diffrent languages. But language translation is not working on zend form validation error messages. I dont want to use extra .php file like fr.php for it.

I got this code from click here

$translator = new Zend\I18n\Translator\Translator();
$translator->addTranslationFile(
    'phpArray'
    'resources/languages/en.php',
    'default',
    'en_US'
);

Zend\Validator\AbstractValidator::setDefaultTranslator($translator);

Any solution how to implement it in ZF2.

Upvotes: 3

Views: 7703

Answers (2)

Havelock
Havelock

Reputation: 6976

Simply add your .po or .mo files instead of the .php ones, i.e. like this:

$translator = new Zend\I18n\Translator\Translator();
$translator->addTranslationFile(
    'gettext'
    'resources/languages/fr.mo',
    'default',
    'fr_FR'
);
Zend\Validator\AbstractValidator::setDefaultTranslator($translator);

And then to translate it, use

echo $this->formElementErrors($form->get("username"),
                              array('message' => $this->translate("errormessage"))
);

I don't know how it is with performance of the translation in ZF2, but in ZF1 using arrays in .php files was way faster than any other method.

Upvotes: 1

Sam
Sam

Reputation: 16445

I do not approve of the selected answer :P

When adding a new translator to your configuration, do NOT use the default translator-text-domain. The Syntax is as follows:

$translator->addTranslationFile(
    $type,
    $resource,
    $textDomain, //<-- this is the important one
    $lang
);

In your example you've added a file to the default-textdomain. This, sadly, brings lots of troubles with it, at it will not always work as expected. Whenever you're adding translation files, add them to your own text-domain!

After that, all you need to do is to assign the Zend\Form\View\Helper your text-domain. This is done by the following:

// For Labels
$this->formLabel()->setTranslatorTextDomain('your-textdomain');

// For Buttons (input type submit)
$this->formButton()->setTranslatorTextDomain('your-textdomain');

// For Error-Messages
$this->formElementErrors()->setTranslatorTextDomain('your-textdomain');

How to get the ServiceManager into the Form?

This is pretty easy, too. The simples one is to use constructor-injection and inject the ServiceManager or ServiceLocator into the __construct() of your Form. On a Controller-Level this would look something like this:

$serviceLocator = $this->getServiceLocator();
$form           = new My\Form($serviceLocator);

A more in-depth introduction to Form-Dependencies can be found on my Blog, where i illustrate the population of a Zend\Form\Element\Select-Element on dependent Database-Connections.

About the Translator itself

Another thing to note is: as long as there is a Translator-Service attached to your configuration with the name translator, it will automatically be attached to the form as default translator. This is a sample configuration i use within my modules regularly:

'translator' => array(
    'locale' => 'de_DE',
    'translation_file_patterns' => array(
        array(
            'type'     => 'phparray',
            'base_dir' => __DIR__ . '/lang',
            'pattern'  => '%s.php',
            'text_domain' => __NAMESPACE__,
        ),
    ),
),

Upvotes: 7

Related Questions