ZF-10300: Label gets translated twice

Description

For Example the message 'my.namespace.country.germany' will translate to 'Deutschland' and then it tries to translate 'Deutschland' again.

As far as I debugged the problem is relativ easy to solve:

Update: Also Zend_Form_Element_Submit uses the already translated Label


/**
     * Retrieve element label
     *
     * @return string
     */
    public function getLabel()
    {
        $translator = $this->getTranslator();
        if (null !== $translator) {
            return $translator->translate($this->_label);
        }

        return $this->_label;
    }

/**
     * Get label to render
     *
     * @return void
     */
    public function getLabel()
    {
        if (null === ($element = $this->getElement())) {
            return '';
        }

        $label = $element->getLabel();
        $label = trim($label);

        if (empty($label)) {
            return '';
        }

        if (null !== ($translator = $element->getTranslator())) {
            $label = $translator->translate($label);
        }

        $optPrefix = $this->getOptPrefix();
        $optSuffix = $this->getOptSuffix();
        $reqPrefix = $this->getReqPrefix();
        $reqSuffix = $this->getReqSuffix();
        $separator = $this->getSeparator();

        if (!empty($label)) {
            if ($element->isRequired()) {
                $label = $reqPrefix . $label . $reqSuffix;
            } else {
                $label = $optPrefix . $label . $optSuffix;
            }
        }

        return $label;
    }

    /**
     * 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;
    }

As you can see getLabel() from Zend_Form_Element will translate the string and getLabel() from Zend_Form_Element_Decorator translates the already translated string again

Comments

A simple fix would be to remove the translation of getLabel() from the decorator, which is also in my opinion a sideeffect.

A fecorator should decorate...

Detaching Zend_Translate from the affected components.

The problem is not related to Zend_Translate as it translates whatever Zend_Form gives to be translated. There is no way for Zend_Translate to know if that string, which is given, is already translated, or if it's just an untranslated source string.

agree with markus - its simply the decorator that is overtaking irrelevant functions. He should decorate, not change content.

as i cannot edit my comment:

1 remove translating from decorator

2 move the conditional translation inside submit-element (ll 80-82 inside the $value === null condition before (ll76-78).

i run over the same problem, when i wrote a tool to list all untranslated id for me.

Imho the decorator should not change but decorate the content, so removing the translation from it makes sense. The translation is done by the element allready

i stumbled upon this issue while i was writing a custom Zend_Translate_Adapter which is able to organize the translations in my db and uses the logging to collect missing translations. fixed this issue as described above.

Duplicate of ZF-8694. A fix has been proposed there.