0

I´m developing an application using Zend Framework 2 and I need to translate the text of the radio buttons ("Show", "Hide") that I´ve created in my form:

    //within the Form

    public function addRadioButtons ()
        {
            $isPublicRadioButtons = new Element\Radio('isPublic');
            $isPublicRadioButtons->setAttribute('id', 'isPublic')
                    ->setAttribute('value', '0')
                    ->setValueOptions(array(
                        '0' => 'Show',
                        '1' => 'Hide',
                    ));

            $this->add($isPublicRadioButtons);
        }

What do I have to do in the view side to be able to translate them?

I know that to render translations to the views I need to use $this→translate() view helper. So within the view I´ll have to somehow call the text of the radio buttons..

//Whithin the view

echo $this->translate($someHowCallTheTextOfRadioButton('isPublic') , $textDomain, $locale);
devanerd
  • 365
  • 4
  • 12

3 Answers3

0

Look at FormLabel section to read about translating labels in zend framework 2. I think that most important thing to remember is:

If you have a translator in the Service Manager under the key, ‘translator’, the view helper plugin manager will automatically attach the translator to the FormLabel view helper. See Zend\View\HelperPluginManager::injectTranslator() for more information.

How to properly setup translator you have in ZendSkeletonApplication

kierzniak
  • 606
  • 5
  • 15
0

In your view you can do something like this:

$this->formRadio()->setTranslatorTextDomain('textdomainhere');

Diemuzi
  • 3,507
  • 7
  • 36
  • 61
0

You can have your form implement the TranslatorAwareInterface and, if you are using PHP 5.4+, have it use the TranslatorAwareTrait (otherwise you simply have to implement the interface yourself). You can now inject a translator instance into your form, e.g. in the form's factory. Then you can translate the labels as follows:

//within the Form

public function addRadioButtons ()
    {
        $isPublicRadioButtons = new Element\Radio('isPublic');
        $isPublicRadioButtons->setAttribute('id', 'isPublic')
                ->setAttribute('value', '0')
                ->setValueOptions(array(
                    '0' => $this->getTranslator()->translate('Show'),
                    '1' => $this->getTranslator()->translate('Hide'),
                ));

        $this->add($isPublicRadioButtons);
    }
Manu
  • 75
  • 3