0

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

Community
  • 1
  • 1
Milad.Nozari
  • 2,243
  • 3
  • 26
  • 47

2 Answers2

1

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'); ?>
Remi Thomas
  • 1,528
  • 1
  • 15
  • 25
  • Thanks Remi for your answer. One thing though, this approach seems a tiny teeny little bit more complicated. Is this _lan.phtml a partial??? – Milad.Nozari Jul 11 '13 at 13:16
  • **_lan.phtml** is not a partial, it's a just a view script whose you use it to get a trace of a translation (with poedit for example) as any kind of other view script – Remi Thomas Jul 22 '13 at 01:41
0

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'),
    ));
Milad.Nozari
  • 2,243
  • 3
  • 26
  • 47