15.8. Internationalization of Zend_Form

Increasingly, developers need to tailor their content for multiple languages and regions. Zend_Form aims to make such a task trivial, and leverages functionality in both Zend_Translate and Zend_Validate to do so.

By default, no internationalisation (i18n) is performed. To turn on i18n features in Zend_Form, you will need to instantiate a Zend_Translate object with an appropriate adapter, and attach it to Zend_Form and/or Zend_Validate. See the Zend_Translate documentation for more information on creating the translate object and translation files

[Замечание] Translation Can Be Turned Off Per Item

You can disable translation for any form, element, display group, or sub form by calling its setDisableTranslator($flag) method or passing a disableTranslator option to the object. This can be useful when you want to selectively disable translation for individual elements or sets of elements.

15.8.1. 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);
    ?>

15.8.2. Standard I18N Targets

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

  • Validation error messages. Validation error messages may be translated. To do so, use the various error code constants from the Zend_Validate validation classes as the message IDs. For more information on these codes, see the Zend_Validate documentation.

  • Labels. Element labels will be translated, if a translation exists.

  • Fieldset Legends. Display groups and sub forms render in fieldsets by default. The Fieldset decorator attempts to translate the legend before rendering the fieldset.

  • Form and Element Descriptions. All form types (element, form, display group, sub form) allow specifying an optional item description. The Description decorator can be used to render this, and by default will take the value and attempt to translate it.

  • Multi-option Values. For the various items inheriting from Zend_Form_Element_Multi (including the MultiCheckbox, Multiselect, and Radio elements), the option values (not keys) will be translated if a translation is available; this means that the option labels presented to the user will be translated.

  • Submit and Button Labels. The various Submit and Button elements (Button, Submit, and Reset) will translate the label displayed to the user.

    Поддержать сайт на родительском проекте КГБ