ZF-8694: Zend_Form_Decorator_Label Attempts to Translate Labels Twice
Description
There exists an issue with Zend_Form translating labels which becomes apparent when Zend_Translate logging is enabled.
When a form is rendered the form loops through the decorators running their render method. The following code is called as a result of rendering the label.
Zend_Form_Decorator_Label Line #253
$label = $element->getLabel();
$label = trim($label);
if (empty($label)) {
return '';
}
if (null !== ($translator = $element->getTranslator())) {
$label = $translator->translate($label);
}
The above code retrieves the label from the element object and the runs validation methods and then proceeds to translate the message. Below is the code for the $element->getLabel() method.
Zend_Form_Element Line #606
public function getLabel()
{
$translator = $this->getTranslator();
if (null !== $translator) {
return $translator->translate($this->_label);
}
return $this->_label;
}
As you can see this method ALSO translates the text before sending it back to the decorator. This means the Element object is returning translated text back to the decorator which then is attempting to translate already translated text. This causes the Zend_Translate class to log a notice in its log file notifying the user it is missing a translation.
The fix is an obvious one, simply removing the duplicate code from the Zend_Form_Decorator_Label dealing with translation will solve the problem as their corresponding elements should already be translating the content for them.
Please note this also is affecting the Submit button and I have not been able to trace which decorator is causing that problem, although I assume the problem is the same. You will probably want to check through all of the decorators to check if any others are doing the same.
Brad
Comments
Posted by purple ankh (purpleankh) on 2010-01-20T05:06:40.000+0000
With reference to the final note about the Submit button, this has been reported as ZF-8764.
Posted by Ivan Krechetov (ikr) on 2010-02-17T07:08:44.000+0000
For us this issue is very important, as far as we have an interactive translation mode. It allows to change the site texts right where you see them in the graphical user interface. To do this we have to append stuff to translated strings on the runtime. Like this:
Under ZF 1.9.7 the code above outputs:
Bug title[XY] Send[XY]While under ZF 1.10.0 and 1.10.1 it's:
Bug title[XY][XY] Send[XY][XY]Posted by Joakim Nygård (jokke) on 2010-08-09T06:51:21.000+0000
The question is whether the translation should be restricted to the element's or the Label decorator's getLabel() method. I think it makes the most sense to place the translation in the decorator - as close to the user as possible, but I am not sure if this change will break anything depending on the element doing the translation.
Posted by Tomáš Fejfar (tomas.fejfar@gmail.com) on 2010-12-27T04:05:18.000+0000
The only legitimate place to translate the value is where the value is received - i.e. in element's getLabel() method. Decorator's purpose is to receive some value and wrap it around the current content. Not tu modify it in any way.
Posted by Nicolae Dima (nicolaedima) on 2011-02-06T07:12:36.000+0000
This can be fixed fast, by commenting these lines
Posted by Holger Schletz (hschletz) on 2011-05-28T08:27:05.000+0000
OK, it's bug hunt days again. Time to bump this issue. Any chance to see it fixed this time? It's been around for ages, as well as the proposed simple fix, like in the previous comment.
BTW, the issue with Zend_Form_Element_Submit has been silently fixed in the meantime, but it still occurs with Zend_Form_Decorator_Label.
Posted by Kim Blomqvist (kblomqvist) on 2011-05-28T08:50:25.000+0000
Patch attached. Holger - where did you check that the issue with {{Zend_Form_Element_Submit}} has been already fixed. It wasn't in my trunk.
Posted by Holger Schletz (hschletz) on 2011-05-28T09:36:19.000+0000
It has not been fixed in Zend_Form_Element_Submit itself - in fact, removing the translate() call there would result in an untranslated label. Something else in the code path has changed so that now only Zend_Form_Decorator_Label is still affected.
I don't know where this change has been made and whether the new behavior is intentional or just happened to stop the bug from occurring. The facts are: with the current implementation, the label gets translated once for Zend_Form_Element_Submit::getLabel() (correct) and twice for Zend_Form_Decorator_Label::getLabel() (incorrect). This might need more investigation, but removing the translate() call from Zend_Form_Decorator_Label::getLabel() would at least be an interim solution that actually works. It could easily be reverted if it turns out to be the wrong place for the fix.
The fix has been proposed and known to work flawlessly by several users for a long time, so I think it would be safe to provide it as a working solution. There are already discussions about restructuring and where translations should actually be done, but that's more of a long term goal.
Posted by Holger Schletz (hschletz) on 2011-09-12T10:34:19.000+0000
Somewhere between 1.11.7 and 1.11.10, a change to Zend_Form_Element_Submit was introduced. The label no longer gets translated if the patch to Zend_Form_Decorator_Label is applied. If that patch is reverted, the problem with the double-translated labels shows up again.
An updated patch that works for 1.11.10 is available at http://svn.savannah.nongnu.org/viewvc/checkout/…
Posted by Sebastian (basty) on 2011-10-11T19:09:50.000+0000
The updated patch of Holger works also fine for me in v1.11.11. Thanks!
Posted by Michael Boke (michael.boke@gmail.com) on 2011-11-29T15:56:52.000+0000
Got an entire day wasted figuring out why my label is translated into the wrong text.
For example we got a tag english "or" Translated to dutch to "of" Got translated again english "of" to "van"
Please fix this with the remove translate from the decorator label. That seems to be the right solution.
btw the latest svn revision 24561 still has this problem
Posted by Rafael Tavares Amorim (dextervip) on 2012-01-17T02:52:27.000+0000
Removing the translate code block from: Form/Decorator/Label.php Form/Decorator/Description.php Form/Element/Submit.php
Since translate happen in Form/Element.php, It works fine in my application and solve this issue in v1.11.11 i believe
Posted by Rafael Tavares Amorim (dextervip) on 2012-08-07T05:30:19.000+0000
No updates??
Posted by Tobias Petry (ice-breaker) on 2012-09-04T16:06:54.000+0000
Any updates? The bug is still existent in 1.12.0
Posted by Frank Brückner (frosch) on 2012-09-04T16:17:38.000+0000
@[~ice-breaker] The patch needs a review by the cr-team.
Posted by François-Régis (frej) on 2012-11-15T08:24:25.000+0000
Hi,
While it's still under review, I would propose the following fix for the Zend_Form_Element Class.
The below allows to pass an arg (named "translation_params" in the proposed fix) through the "attribs" property of the form element that will be used as the variables within the label. In our case, this was necessary because labels may depend on some vars, so we needed a way to pass this vars to the the translator. As the translator doesn't handle vars per default, I had to use the view helper which already handles that.
}
Regards, Frej.
Posted by Frank Brückner (frosch) on 2013-01-22T12:02:36.000+0000
Posted by Frank Brückner (frosch) on 2013-01-22T12:07:52.000+0000
Fixed on trunk (25242) and release-1.12 (25243)
(Unit test for Description decorator included.)