From e84fe520c4a0db3d755f4f43d09e9f571976407b Mon Sep 17 00:00:00 2001 From: Ralf Eggert Date: Fri, 8 Jul 2016 17:47:30 +0200 Subject: [PATCH] Added translation to FormElementErrors view helper --- src/View/Helper/FormElementErrors.php | 4 ++++ test/View/Helper/FormElementErrorsTest.php | 26 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/View/Helper/FormElementErrors.php b/src/View/Helper/FormElementErrors.php index fa6f0a8d..82bfd347 100644 --- a/src/View/Helper/FormElementErrors.php +++ b/src/View/Helper/FormElementErrors.php @@ -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); }); diff --git a/test/View/Helper/FormElementErrorsTest.php b/test/View/Helper/FormElementErrorsTest.php index 89fcdaa5..cbf5dcb8 100644 --- a/test/View/Helper/FormElementErrorsTest.php +++ b/test/View/Helper/FormElementErrorsTest.php @@ -46,6 +46,32 @@ public function testRendersErrorMessagesUsingUnorderedListByDefault() $this->assertRegexp('##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('##s', $markup); + } + public function testCanSpecifyAttributesForOpeningTag() { $messages = $this->getMessageList();