PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Initializing I18n in Forms

In order to initialize I18n in forms, you will need either a Zend_Translate object or a Zend_Translate_Adapter object, as detailed in the Zend_Translate documentation. Once you have a translation object, you have several options:

  • Easiest: add it to the registry. All I18n aware components of Zend Framework will autodiscover a translate object that is in the registry under the 'Zend_Translate' key and use it to perform translation and/or localization:

    <?php
    // use the 'Zend_Translate' key; $translate is a Zend_Translate object:
    Zend_Registry::set('Zend_Translate'$translate);

    This will be picked up by Zend_Form, Zend_Validate, and Zend_View_Helper_Translate.

  • If all you are worried about is translating validation error messages, you can register the translation object with Zend_Validate_Abstract:

    <?php
    // Tell all validation classes to use a specific translate adapter:
    Zend_Validate_Abstract::setDefaultTranslator($translate);
  • Alternatively, you can attach to the Zend_Form object as a global translator. This has the side effect of also translating validation error messages:

    <?php
    // Tell all form classes to use a specific translate adapter, as well
    // as use this adapter to translate validation error messages:
    Zend_Form::setDefaultTranslator($translate);
  • Finally, you can attach a translator to a specific form instance or to specific elements using their setTranslator() methods:

    <?php
    // Tell *this* form instance to use a specific translate adapter; it
    // will also be used to translate validation error messages for all
    // elements:
    $form->setTranslator($translate);

    // Tell *this* element to use a specific translate adapter; it will
    // also be used to translate validation error messages for this
    // particular element:
    $element->setTranslator($translate);

Zend Framework