How to translate form labels in Zend Framework 2?

I don’t get it ! .. May someone like to explain how to translate the forms? A simple example will 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. --> 

Configurable module/Application/config/module.config.php :

 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 used the FormLabel view FormLabel :

 <dt><label><?php echo $this->formLabel($form->get('city')); ?></label></dt> 

In addition, I debugged FormLabel in the place where the transactor is used (lines 116-120 ) - everything seems to be in order.

But it still does not work.


EDIT

Elements (test) for labels, which I added to the de_DE.po file manually, are translated. The essence of the problem with ZF2 was that I used $form->get('city')->getLabel() instead of $this->formlabel($form->get('city')) from a script perspective.

The problem is that tags are not added to the de_DE.po file. But this is no longer ZF2's problem, so I accept Ruben's answer and open a new question about Poedit.

+7
source share
4 answers

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 pasted it into your ServiceManager. Most likely, you will get it in your application 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 use the formlabel view helper:

 echo $this->formLabel($form->get('city')); 

And of course, make sure your translations are in your .po file.

+8
source

I think your problem is that you did not specify poedit (or a similar tool), so you must add them manually to your poedit directories (.po)

for your labels to be detected by tools like poedit, your lines must be used inside the translate () or _ () function (you can add another function in the directory / properties / sources).

since the _ () function is not a user in ZF2 (today), so a tiny hack is to add such a function to your index.php (no need to change anything in the poedit parameters):

 // in index.php function _($str) { return $str; } 

and in your code just use it when your lines are outside the translation 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 if you want

 $myLabel = _('any label string'); // <--- added to poedit catalog //... 'options' => array( 'label' => $myLabel , 'value_options' => $this->cities, 'id' => 'searchFormCity', ), 
+4
source

@ Ruben speaks correctly!

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

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

Of course, Poedit must be configured to search for my keywords. check on how to configure it

+1
source

All solutions do not use ZF2 power. You must configure poedit correctly:

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

0
source

All Articles