ZF-8764: Zend_Form_Element_Submit translates the label twice.

Description

Using ZendFramework 1.10.0alpha1

A change in r18965 to Zend_Form_Element->getLabel() has added translation of the label at this stage.

However, Zend_Form_Element_Submit->getLabel() translates this value again:


    /**
     * Return label
     *
     * If no label is present, returns the currently set name.
     *
     * If a translator is present, returns the translated label.
     *
     * @return string
     */
    public function getLabel()
    {
        $value = parent::getLabel();

        if (null === $value) {
            $value = $this->getName();
        }

        if (null !== ($translator = $this->getTranslator())) {
            return $translator->translate($value);
        }

        return $value;
    }

This could lead to some very subtle mistranslation artefacts.

A possible fix would be to only translate if the label in Submit is retrieved from getName() as below:


    /**
     * Return label
     *
     * If no label is present, returns the currently set name.
     *
     * If a translator is present, returns the translated label.
     *
     * @return string
     */
    public function getLabel()
    {
        $value = parent::getLabel();

        if (null === $value) {
            $value = $this->getName();
            if (null !== ($translator = $this->getTranslator())) {
                return $translator->translate($value);
            }
        }

        return $value;
    }

Comments

See attachment for a proposed patch and two unit tests. One to test for duplicate translation, and one to insure that an element name that is used as label is properly translated still.

Changed the affected version to 'Next Minor Release'. I had incorrectly used 'Next Mini Release' originally, which became 1.9.7 .

Zend_Form_Decorator_Label is affected by the same issue as of 1.10.0. In fact, r18965 breaks every piece of code that translates the result of Zend_Form_Element::getLabel().

Here ZF-8694 is the description for Zend_Form_Decorator_Label, i mark that as duplicate, as well as ZF-9371

The issue exists still in the current trunk. I agree with the suggested patch. The translation is done in the parent::getLabel() and should only be repeated if the value is used as label.

Patch reviewed and applied to both trunk and the 1.11 release branch.