Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate form labels in Zend Framework 2?

I'm not getting it!.. Can please someone explain, how to translate form labels? A simple example would be great.

Thank you in advance!


class Search\Form\CourseSearchForm

...

class CourseSearchForm extends Form {

    ...

    public function __construct(array $cities) {
        parent::__construct('courseSearch');
        ...
        $this->add(array(
            'name' => 'city',
            'type'  => 'Zend\Form\Element\Select',
            'options' => array(
                'label' => 'Stadt',
                'value_options' => $this->cities,
                'id'  => 'searchFormCity',
            ),
        ));
        ...
    }
}

view script /module/Search/view/search/search/search-form.phtml

<?php echo $this->form()->openTag($form); ?>
<dl>
    ...
    <dt><label><?php echo $form->get('city')->getLabel(); ?></label></dt>
    <dd><?php echo $this->formRow($form->get('city'), null, false, false); ?></dd>
    ...
</dl>
<?php echo $this->form()->closeTag(); ?>
<!-- The formRow(...) is my MyNamespace\Form\View\Helper (extends Zend\Form\View\Helper\FormRow); the fourth argument of it disables the label. -->

The module/Application/config/module.config.php is configured:

return array(
    'router' => ...
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'de_DE',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => ...
    'view_manager' => ...
);

I also edited my view and use the FormLabel view helper:

<dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt>

Furthermore I debugged the FormLabel at the place, where the tranlator is used (lines 116-120) -- seems to be OK.

But it's still not working.


EDIT

The (test) items for labels, I added to the de_DE.po file manually, are tranlated. The ZF2 side problem was actually, that I was using $form->get('city')->getLabel() instead of $this->formlabel($form->get('city')) in th view script.

The problem is now, that the labels are not added to the de_DE.po file. But it's not a ZF2 issue anymore, so I've accept Ruben's answer and open a new Poedit question.

like image 583
automatix Avatar asked Apr 12 '13 01:04

automatix


2 Answers

Instead of using:

<?php echo $form->get('city')->getLabel(); ?>

You should use the formlabel view helper. This helper automatically uses your translator during rendering if you have inserted it in your ServiceManager. Most likely you will have it in your Application's module module.config.php:

'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
        ),
    ),

    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),

Once you do use the formlabel view helper:

echo $this->formLabel($form->get('city'));

And of course make sure your translations are in your .po file.

like image 127
Ruben Avatar answered Nov 03 '22 00:11

Ruben


i think your problem is that you label are not detected by poedit (or similar tool), so you have to add them manually to your poedit catalogs (.po)

to make your label strings detected by tools like poedit, your strings need to be used inside a translate() function or _() (other function can be added in Catalog/properties/sources keyword)

as the _() function is not user in ZF2 (today) so a tiny hack is to add a function like this in your index.php (no need to modify anything, this way, in poedit params):

// in index.php
function _($str) 
{ 
    return $str; 
}

and in your code, just use it when your strings are outside of a translate function

//...
    $this->add(array(
        'name' => 'city',
        'type'  => 'Zend\Form\Element\Select',
        'options' => array(
            'label' => _('myLabel') ,    // <------ will be detected by poedit
            'value_options' => $this->cities,
            'id'  => 'searchFormCity',
        ),
    ));
//...

or like this if you prefer

$myLabel = _('any label string');  // <--- added to poedit catalog
//...
        'options' => array(
            'label' => $myLabel ,
            'value_options' => $this->cities,
            'id'  => 'searchFormCity',
        ),
like image 5
Rachid BERKANE Avatar answered Nov 03 '22 00:11

Rachid BERKANE