Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Added translation to FormElementErrors view helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Eggert committed Jul 8, 2016
1 parent 46cce9a commit e84fe52
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/View/Helper/FormElementErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public function render(ElementInterface $element, array $attributes = [])
$escapeHtml = $this->getEscapeHtmlHelper();
$messagesToPrint = [];
array_walk_recursive($messages, function ($item) use (&$messagesToPrint, $escapeHtml) {
if (null !== ($translator = $this->getTranslator())) {
$item = $translator->translate($item, $this->getTranslatorTextDomain());
}

$messagesToPrint[] = $escapeHtml($item);
});

Expand Down
26 changes: 26 additions & 0 deletions test/View/Helper/FormElementErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,32 @@ public function testRendersErrorMessagesUsingUnorderedListByDefault()
$this->assertRegexp('#<ul>\s*<li>First error message</li>\s*<li>Second error message</li>\s*<li>Third error message</li>\s*</ul>#s', $markup);
}

public function testRendersErrorMessagesUsingUnorderedListTranslated()
{
$mockTranslator = $this->getMock('Zend\I18n\Translator\Translator');
$mockTranslator->expects($this->at(0))
->method('translate')
->will($this->returnValue('Translated first error message'));
$mockTranslator->expects($this->at(1))
->method('translate')
->will($this->returnValue('Translated second error message'));
$mockTranslator->expects($this->at(2))
->method('translate')
->will($this->returnValue('Translated third error message'));

$this->helper->setTranslator($mockTranslator);
$this->assertTrue($this->helper->hasTranslator());

$this->helper->setTranslatorTextDomain('default');

$messages = $this->getMessageList();
$element = new Element('foo');
$element->setMessages($messages);

$markup = $this->helper->render($element);
$this->assertRegexp('#<ul>\s*<li>Translated first error message</li>\s*<li>Translated second error message</li>\s*<li>Translated third error message</li>\s*</ul>#s', $markup);
}

public function testCanSpecifyAttributesForOpeningTag()
{
$messages = $this->getMessageList();
Expand Down

0 comments on commit e84fe52

Please sign in to comment.