Question

How to translate placeholder in Zend Framework 2?

I have some form item, like this:

 $this->add(array(
         'name' => 'Email',
         'attributes' => array(
             'type'  => 'email',
             'class' => 'form-control',
             'placeholder' => 'Email address',
             'required' => 'required'
         ),
     ));

In view I call this:

<?php echo $this->formRow($form->get('Email'));?>
Was it helpful?

Solution

If your are using the ZendSkeletonApplication or similar you can do the following on your view:

    $translator = $this->plugin('translate')->getTranslator();
    $this->plugin('form_row')->setTranslator($translator);

then

    echo $this->formRow($form->get('Email'));

OTHER TIPS

Maybe there's a better way, but you could always abandon the formRow helper and manually display that input. E.g.:

<?php $emailField = $form->get('Email'); ?>
<input type="<?php $emailField->getType(); ?>" class="<?php $emailField->getClass(); ?>" placeholder="<?php $this->translate($emailField->getPlaceholder()); ?>" value="<?php $emailField->getValue(); ?>" <?php $emailField->getRequired(); ?> >

That gives you more control over how that input is displayed, but it's also laborious and error prone. Also I'm assuming you have a translation prepared in your .po files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top