Zend_Form-I18n.xml 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.form.i18n">
  4. <title>Internationalization of Zend_Form</title>
  5. <para>
  6. Increasingly, developers need to tailor their content for multiple
  7. languages and regions. <classname>Zend_Form</classname> aims to make such a task trivial,
  8. and leverages functionality in both <link
  9. linkend="zend.translate">Zend_Translate</link> and <link
  10. linkend="zend.validate">Zend_Validate</link> to do so.
  11. </para>
  12. <para>
  13. By default, no internationalisation (I18n) is performed. To turn on I18n
  14. features in <classname>Zend_Form</classname>, you will need to instantiate a
  15. <classname>Zend_Translate</classname> object with an appropriate adapter, and
  16. attach it to <classname>Zend_Form</classname> and/or <classname>Zend_Validate</classname>.
  17. See the <link linkend="zend.translate">Zend_Translate
  18. documentation</link> for more information on creating the translate
  19. object and translation files
  20. </para>
  21. <note>
  22. <title>Translation Can Be Turned Off Per Item</title>
  23. <para>
  24. You can disable translation for any form, element, display group, or
  25. sub form by calling its <methodname>setDisableTranslator($flag)</methodname>
  26. method or passing a <property>disableTranslator</property> option to the
  27. object. This can be useful when you want to selectively disable
  28. translation for individual elements or sets of elements.
  29. </para>
  30. </note>
  31. <sect2 id="zend.form.i18n.initialization">
  32. <title>Initializing I18n in Forms</title>
  33. <para>
  34. In order to initialize I18n in forms, you will need either a
  35. <classname>Zend_Translate</classname> object or a
  36. <classname>Zend_Translate_Adapter</classname> object, as detailed in the
  37. <classname>Zend_Translate</classname> documentation. Once you have a
  38. translation object, you have several options:
  39. </para>
  40. <itemizedlist>
  41. <listitem>
  42. <para>
  43. <emphasis>Easiest:</emphasis> add it to the registry. All I18n
  44. aware components of Zend Framework will autodiscover a translate
  45. object that is in the registry under the 'Zend_Translate' key
  46. and use it to perform translation and/or localization:
  47. </para>
  48. <programlisting language="php"><![CDATA[
  49. // use the 'Zend_Translate' key; $translate is a Zend_Translate object:
  50. Zend_Registry::set('Zend_Translate', $translate);
  51. ]]></programlisting>
  52. <para>
  53. This will be picked up by <classname>Zend_Form</classname>,
  54. <classname>Zend_Validate</classname>, and
  55. <classname>Zend_View_Helper_Translate</classname>.
  56. </para>
  57. </listitem>
  58. <listitem>
  59. <para>
  60. If all you are worried about is translating validation error
  61. messages, you can register the translation object with
  62. <classname>Zend_Validate_Abstract</classname>:
  63. </para>
  64. <programlisting language="php"><![CDATA[
  65. // Tell all validation classes to use a specific translate adapter:
  66. Zend_Validate_Abstract::setDefaultTranslator($translate);
  67. ]]></programlisting>
  68. </listitem>
  69. <listitem>
  70. <para>
  71. Alternatively, you can attach to the <classname>Zend_Form</classname>
  72. object as a global translator. This has the side effect of also
  73. translating validation error messages:
  74. </para>
  75. <programlisting language="php"><![CDATA[
  76. // Tell all form classes to use a specific translate adapter, as well
  77. // as use this adapter to translate validation error messages:
  78. Zend_Form::setDefaultTranslator($translate);
  79. ]]></programlisting>
  80. </listitem>
  81. <listitem>
  82. <para>
  83. Finally, you can attach a translator to a specific form instance
  84. or to specific elements using their <methodname>setTranslator()</methodname>
  85. methods:
  86. </para>
  87. <programlisting language="php"><![CDATA[
  88. // Tell *this* form instance to use a specific translate adapter; it
  89. // will also be used to translate validation error messages for all
  90. // elements:
  91. $form->setTranslator($translate);
  92. // Tell *this* element to use a specific translate adapter; it will
  93. // also be used to translate validation error messages for this
  94. // particular element:
  95. $element->setTranslator($translate);
  96. ]]></programlisting>
  97. </listitem>
  98. </itemizedlist>
  99. </sect2>
  100. <sect2 id="zend.form.i18n.standard">
  101. <title>Standard I18n Targets</title>
  102. <para>
  103. Now that you've attached a translation object to, what exactly can
  104. you translate by default?
  105. </para>
  106. <itemizedlist>
  107. <listitem>
  108. <para>
  109. <emphasis>Validation error messages.</emphasis> Validation
  110. error messages may be translated. To do so, use the various
  111. error code constants from the <classname>Zend_Validate</classname>
  112. validation classes as the message IDs. For more information
  113. on these codes, see the <link
  114. linkend="zend.validate">Zend_Validate</link>
  115. documentation.
  116. </para>
  117. <para>
  118. Alternately, as of 1.6.0, you may provide translation
  119. strings using the actual error messages as message
  120. identifiers. This is the preferred use case for 1.6.0 and
  121. up, as we will be deprecating translation of message keys in
  122. future releases.
  123. </para>
  124. </listitem>
  125. <listitem>
  126. <para>
  127. <emphasis>Labels.</emphasis> Element labels will be
  128. translated, if a translation exists.
  129. </para>
  130. </listitem>
  131. <listitem>
  132. <para>
  133. <emphasis>Fieldset Legends.</emphasis> Display groups and
  134. sub forms render in fieldsets by default. The Fieldset
  135. decorator attempts to translate the legend before rendering
  136. the fieldset.
  137. </para>
  138. </listitem>
  139. <listitem>
  140. <para>
  141. <emphasis>Form and Element Descriptions.</emphasis> All form
  142. types (element, form, display group, sub form) allow
  143. specifying an optional item description. The Description
  144. decorator can be used to render this, and by default will
  145. take the value and attempt to translate it.
  146. </para>
  147. </listitem>
  148. <listitem>
  149. <para>
  150. <emphasis>Multi-option Values.</emphasis> for the various
  151. items inheriting from <classname>Zend_Form_Element_Multi</classname>
  152. (including the MultiCheckbox, Multiselect, and Radio
  153. elements), the option values (not keys) will be translated
  154. if a translation is available; this means that the option
  155. labels presented to the user will be translated.
  156. </para>
  157. </listitem>
  158. <listitem>
  159. <para>
  160. <emphasis>Submit and Button Labels.</emphasis> The various
  161. Submit and Button elements (Button, Submit, and Reset) will
  162. translate the label displayed to the user.
  163. </para>
  164. </listitem>
  165. </itemizedlist>
  166. </sect2>
  167. </sect1>
  168. <!--
  169. vim:se ts=4 sw=4 tw=80 et:
  170. -->