How to translate form labels in Zend Framework 2?

前端 未结 4 853
星月不相逢
星月不相逢 2020-12-10 06:17

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 S

4条回答
  •  粉色の甜心
    2020-12-10 06:38

    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',
            ),
    

提交回复
热议问题