Issues

ZF-10050: Translation is executed twice on form labels

Issue Type: Bug Created: 2010-06-24T23:51:50.000+0000 Last Updated: 2010-10-26T00:48:56.000+0000 Status: Resolved Fix version(s): Reporter: Christian Riesen (christianriesen) Assignee: Christian Albrecht (alab) Tags: - Zend_Form

Related issues: - ZF-8694

Attachments:

Description

I classified it as unknown component since I'm not sure exactly where it's at, just see the resulting 'mayhem'.

Initializing translate this way:

<pre class="highlight">
    public function _initTranslate()
    {
        $this->bootstrap('logging');
        $log = Zend_Registry::get('Zend_Log');
        
        $translate = new Zend_Translate('array', APPLICATION_PATH . '/../language/en.php', 'en');
        $translate->setOptions(array(
            'log' => $log,
            'logUntranslated' => true
        ));

        Zend_Registry::set('Zend_Translate', $translate);
        return $translate;
    }

Then initializing and setting the form in controller, and echoing it in view. Here is the form class:

<pre class="highlight">
<?php

class User_Form_Login extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $this->addElement('text', 'username', array(
            'label'      => 'Username',
            'required'   => true,
            'filters'    => array(
                array('StringTrim'),
            ),
            'validators' => array(
                array(
                    'StringLength',
                    false,
                    array(
                        3,
                        60
                    )
                ),
            ),
        ));

        $this->addElement('password', 'userpassword', array(
            'label'      => 'Password',
            'required'   => true,
            'filters'    => array('StringTrim'),
            'validators' => array(
                array(
                    'StringLength',
                    false,
                    array(
                        6,
                        100
                    )
                ),
            )
        ));
        
        $this->addElement('submit', 'submit', array(
            'ignore'   => true,
            'label'    => 'Login',
        ));
    }
}

As you can see nothing really interesting or overly dramatic. Now using the firebug logger, it displayed to me that Username was not translated, twice. I added it and the message went away. Then I added a translation in german, "Benutzername". Now I received a message that "Benutzername" was not translated, although I did not change the form. So it tried to retranslate the already translated string IMHO.

I cloned the array adapter for translate and overwrote the following function below:

<pre class="highlight">
    protected function _log($message, $locale) {
        if ($this->_options['logUntranslated']) {
            $message = str_replace('%message%', $message, $this->_options['logMessage']);
            $message = str_replace('%locale%', $locale, $message);

            $message .= PHP_EOL;

            $trace = debug_backtrace();
            foreach ($trace as $line) {
                $message .= $line['file'] . ':' . $line['line'];
                $message .= "\n";
            }

            if ($this->_options['log']) {
                $this->_options['log']->notice($message);
            } else {
                trigger_error($message, E_USER_NOTICE);
            }
        }
    }

This gave me the following output on just the English translation showing, with no Username translation added.

Untranslated message within 'en': Username /www/ZendFramework-1.10.6/Zend/Translate/Adapter.php:748 /www/ZendFramework-1.10.6/Zend/Form/Element.php:625 /www/ZendFramework-1.10.6/Zend/Form/Decorator/Label.php:253 /www/ZendFramework-1.10.6/Zend/Form/Decorator/Label.php:296 /www/ZendFramework-1.10.6/Zend/Form/Element.php:2020 /www/ZendFramework-1.10.6/Zend/Form/Decorator/FormElements.php:101 /www/ZendFramework-1.10.6/Zend/Form.php:2887 /www/ZendFramework-1.10.6/Zend/Form.php:2903 /www/base/application/modules/user/views/scripts/login/index.phtml:9 /www/ZendFramework-1.10.6/Zend/View.php:108 /www/ZendFramework-1.10.6/Zend/View/Abstract.php:880 /www/ZendFramework-1.10.6/Zend/Controller/Action/Helper/ViewRenderer.php:897 /www/ZendFramework-1.10.6/Zend/Controller/Action/Helper/ViewRenderer.php:918 /www/ZendFramework-1.10.6/Zend/Controller/Action/Helper/ViewRenderer.php:957 /www/ZendFramework-1.10.6/Zend/Controller/Action/HelperBroker.php:277 /www/ZendFramework-1.10.6/Zend/Controller/Action.php:523 /www/ZendFramework-1.10.6/Zend/Controller/Dispatcher/Standard.php:295 /www/ZendFramework-1.10.6/Zend/Controller/Front.php:954 /www/ZendFramework-1.10.6/Zend/Application/Bootstrap/Bootstrap.php:97 /www/ZendFramework-1.10.6/Zend/Application.php:366 /www/base/public/index.php:27

Untranslated message within 'en': Username /www/ZendFramework-1.10.6/Zend/Translate/Adapter.php:748 /www/ZendFramework-1.10.6/Zend/Form/Decorator/Label.php:261 /www/ZendFramework-1.10.6/Zend/Form/Decorator/Label.php:296 /www/ZendFramework-1.10.6/Zend/Form/Element.php:2020 /www/ZendFramework-1.10.6/Zend/Form/Decorator/FormElements.php:101 /www/ZendFramework-1.10.6/Zend/Form.php:2887 /www/ZendFramework-1.10.6/Zend/Form.php:2903 /www/base/application/modules/user/views/scripts/login/index.phtml:9 /www/ZendFramework-1.10.6/Zend/View.php:108 /www/ZendFramework-1.10.6/Zend/View/Abstract.php:880 /www/ZendFramework-1.10.6/Zend/Controller/Action/Helper/ViewRenderer.php:897 /www/ZendFramework-1.10.6/Zend/Controller/Action/Helper/ViewRenderer.php:918 /www/ZendFramework-1.10.6/Zend/Controller/Action/Helper/ViewRenderer.php:957 /www/ZendFramework-1.10.6/Zend/Controller/Action/HelperBroker.php:277 /www/ZendFramework-1.10.6/Zend/Controller/Action.php:523 /www/ZendFramework-1.10.6/Zend/Controller/Dispatcher/Standard.php:295 /www/ZendFramework-1.10.6/Zend/Controller/Front.php:954 /www/ZendFramework-1.10.6/Zend/Application/Bootstrap/Bootstrap.php:97 /www/ZendFramework-1.10.6/Zend/Application.php:366 /www/base/public/index.php:27

It looks like in the first round, it goes from Decorator Label to Form Element and then translates there. In the second round it just seems to call the translator from Decorator Label.

This output is from a single call to a page, not two or more.

It's not exactly a show stopper, but annoying as it fills up the log over time with unnecessary messages.

I know about filtering and so on for logs, but think this still is a bug that should not happen.

Comments

Posted by Christian Riesen (christianriesen) on 2010-06-25T04:51:20.000+0000

1.9.6 Issue still active in 1.10.6

Posted by Marc Hodgins (mjh_ca) on 2010-10-26T00:48:50.000+0000

Closing as this is a duplicate of ZF-8694. Better to direct all efforts to a single ticket as this same issue is open many times in the tracker.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts