Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

FormRow translate label fix (#ZF2-516) #2300

Merged
merged 3 commits into from

3 participants

@cgmartin

Fixed FormRow so it will translate labels, from this bug report:
http://framework.zend.com/issues/browse/ZF2-516

@bakura10

While your at it, could you add the label_position feature please ? :)

@cgmartin

@bakura10 Heh sure, I'll look into that one. It won't be part of this PR though.

@weierophinney weierophinney commented on the diff
library/Zend/Form/View/Helper/FormRow.php
@@ -88,7 +88,14 @@ public function render(ElementInterface $element)
$elementString = $elementHelper->render($element);
- if (!empty($label)) {
+ if (isset($label) && '' !== $label) {
+ // Translate the label
+ if (null !== ($translator = $this->getTranslator())) {
+ $label = $translator->translate(
+ $label, $this->getTranslatorTextDomain()
@weierophinney Owner

too much indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [ZF2-516][#2300] CS fix
- Decrease indentation
7d647c0
@weierophinney weierophinney merged commit 7eb804a into from
@weierophinney

Fixed the CS issue on merge; merged to release and master branches.

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [ZF2-516][#2300] CS fix
- Decrease indentation
7569879
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
16 library/Zend/Form/View/Helper/FormRow.php
@@ -88,7 +88,14 @@ public function render(ElementInterface $element)
$elementString = $elementHelper->render($element);
- if (!empty($label)) {
+ if (isset($label) && '' !== $label) {
+ // Translate the label
+ if (null !== ($translator = $this->getTranslator())) {
+ $label = $translator->translate(
+ $label, $this->getTranslatorTextDomain()
@weierophinney Owner

too much indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ );
+ }
+
$label = $escapeHtmlHelper($label);
$labelAttributes = $element->getLabelAttributes();
@@ -281,6 +288,13 @@ protected function getLabelHelper()
$this->labelHelper = new FormLabel();
}
+ if ($this->hasTranslator()) {
+ $this->labelHelper->setTranslator(
+ $this->getTranslator(),
+ $this->getTranslatorTextDomain()
+ );
+ }
+
return $this->labelHelper;
}
View
42 tests/ZendTest/Form/View/Helper/FormRowTest.php
@@ -159,4 +159,46 @@ public function testInvokeWithNoElementChainsHelper()
{
$this->assertSame($this->helper, $this->helper->__invoke());
}
+
+ public function testLabelWillBeTranslated()
+ {
+ $element = new Element('foo');
+ $element->setLabel('The value for foo:');
+
+ $mockTranslator = $this->getMock('Zend\I18n\Translator\Translator');
+ $mockTranslator->expects($this->any())
+ ->method('translate')
+ ->will($this->returnValue('translated content'));
+
+ $this->helper->setTranslator($mockTranslator);
+ $this->assertTrue($this->helper->hasTranslator());
+
+ $markup = $this->helper->__invoke($element);
+ $this->assertContains('>translated content<', $markup);
+ $this->assertContains('<label', $markup);
+ $this->assertContains('</label>', $markup);
+
+ // Additional coverage when element's id is set
+ $element->setAttribute('id', 'foo');
+ $markup = $this->helper->__invoke($element);
+ $this->assertContains('>translated content<', $markup);
+ $this->assertContains('<label', $markup);
+ $this->assertContains('</label>', $markup);
+ }
+
+
+
+ public function testTranslatorMethods()
+ {
+ $translatorMock = $this->getMock('Zend\I18n\Translator\Translator');
+ $this->helper->setTranslator($translatorMock, 'foo');
+
+ $this->assertEquals($translatorMock, $this->helper->getTranslator());
+ $this->assertEquals('foo', $this->helper->getTranslatorTextDomain());
+ $this->assertTrue($this->helper->hasTranslator());
+ $this->assertTrue($this->helper->isTranslatorEnabled());
+
+ $this->helper->setTranslatorEnabled(false);
+ $this->assertFalse($this->helper->isTranslatorEnabled());
+ }
}
Something went wrong with that request. Please try again.