Search code examples
phpzend-frameworkzend-formzend-translate

Zend_Form setAttrib title not translating with Zend_Translate


When setting the title attribute in Zend_Form, this is not being translated. (Label & errors are translated fine)

// form file

$email = new Zend_Form_Element_Text('username');
$email->setLabel('auth.form.login.username')
      ->setRequired(true)
      ->addFilter('StripTags')
      ->addFilter('StringTrim')
      ->addFilter('StringToLower')
      ->addValidator('NotEmpty')
      ->setAttrib('title', 'auth.form.login.username');

// resource file

...
<tu tuid='auth.form.login.username'>
   <tuv xml:lang="en"><seg>Username</seg></tuv>
</tu>
...

Current result: auth.form.login.username

Expected result: Username


Solution

  • This is a normal behavior, nothing wrong with it.

    The setAttrib() method doesn't translate its value parameter, so you need to use a Zend_Translate adapter as follows:

    $email->setAttrib('title', $this->getTranslator()->translate('auth.form.login.username'))

    This should fix your problem.

    See Standard I18n Targets for more information.

    Now that you've attached a translation object to, what exactly can you translate by default?

    • Validation error messages.
    • Labels.
    • Fieldset Legends.
    • Form and Element Descriptions.
    • Multi-option Values.
    • Submit and Button Labels.