Milad.N
Milad.N

Reputation: 2329

Translate select form element options in zend framework 2

I want to translate the options of a select element in a form. Now I know how to translate form element labels from here: How to translate form labels in Zend Framework 2? (although I'm not using this method to do it) And I also know how to translate form messages from here: http://framework.zend.com/manual/2.0/en/modules/zend.validator.messages.html

But so far I don't know how to translate the options in them comboboxes. I wanted to get the translator from the service manager, but apparently you can't access the serviceLocator from a Form object. I also think that I could alter the options in the select element right before I print it in the template, but I don't know how.

So, I appreciate any help. Thanks

Upvotes: 0

Views: 857

Answers (2)

Remi Thomas
Remi Thomas

Reputation: 1528

The translator is by default for Form\Elements

In my projects I just create one .phtml file named _lan.phtml to contain my select options to translate. Like this:

<?php echo $this->translate('Item01'); ?>
<?php echo $this->translate('Item02'); ?>

Upvotes: 1

Milad.N
Milad.N

Reputation: 2329

I'm not sure this is the right method, but I solved it myself. In the view template, before echoing the element, I did this:

    $this->form->get('user_type')->setValueOptions(array(
        'item01'        => $this->translate('Item01'),
        'item02'        => $this->translate('Item02'),
    ));

Upvotes: 0

Related Questions