How to translate form labels in Zend Framework 2?
Asked Answered
U

4

8

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.

Underthrust answered 12/4, 2013 at 1:28 Comment(2)
Please add some code of what you currently have. It will make it easier to help you out and see what the problem is.Olathe
Thank you for a fast response! I added the relevant code now.Underthrust
O
9

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.

Olathe answered 12/4, 2013 at 2:37 Comment(4)
The module.config.php is configured and now I also edited my view and use the FormLabel view helper. I've debugged the FormLabel in the place, where the tranlator is used (lines 116-120) -- seems to be OK. But it's still not working.Underthrust
And you are sure that the translations are in your .po file? Did you check if PoEdit parses the values for the labels and did you translate them in the specific locale you are using?Olathe
Exctly, it was the problem. See updated answerd above and my other answer here.Underthrust
Sorry, I missed the EDIT part. ;) Great that you got it solved!Olathe
H
5

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',
        ),
Hutchens answered 27/4, 2013 at 13:2 Comment(1)
HI @aromatix i am using language translations in zend 2 learning project when i am using $this->add(array( ‘name’ => “username”, ‘type’ => ‘Zend\Form\Element\Text’, ‘options’ => array( ‘label’ => _(‘Your Username’), // i found fatal error at this position ), )); fatal error : Call to undefined function Incuser\Form_() _() for translate a form label give me fatal error i am using php 5.3.1 right now . Please help me , i am unable to figure out this issue.Falkner
S
1

@Ruben says right!

Me I use PoEdit to generate my *.mo files and to be sure to get all translations in the file, I create somewhere (in view for example) a file named _lan.phtml with all text to be translated :

<?php echo $this->translate("My label"); 
... ?>

Of course, Poedit has to be configured to find my keywords. check this to how to configure it

Snashall answered 12/4, 2013 at 7:5 Comment(2)
Nice simple workaround. Thank you! Good idea for the case, if I won't get it working.Underthrust
I do something similar using Poedit, but there's no need for an extra file: coderwall.com/p/atvtbwDoloresdolorimetry
S
0

All solutions don't use the power of ZF2. You must configure your poedit correctly :

All things are here : http://circlical.com/blog/2013/11/5/localizing-your-twig-using-zend-framework-2-applications

Sheeb answered 10/3, 2014 at 10:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.