Zend_View-Helpers-Translate.xml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.view.helpers.initial.translate">
  4. <title>Translate Helper</title>
  5. <para>
  6. Often web sites are available in several languages. To translate the
  7. content of a site you should simply use <link
  8. linkend="zend.translate.introduction">Zend_Translate</link> and to
  9. integrate <classname>Zend_Translate</classname> within your view you should use
  10. the <emphasis>Translate</emphasis> View Helper.
  11. </para>
  12. <para>
  13. In all following examples we are using the simple Array Translation
  14. Adapter. Of course you can also use any instance of
  15. <classname>Zend_Translate</classname> and also any subclasses of
  16. <classname>Zend_Translate_Adapter</classname>. There are several ways to initiate
  17. the <emphasis>Translate</emphasis> View Helper:
  18. </para>
  19. <itemizedlist>
  20. <listitem>
  21. <para>
  22. Registered, through a previously registered instance in
  23. <classname>Zend_Registry</classname>
  24. </para>
  25. </listitem>
  26. <listitem>
  27. <para>
  28. Afterwards, through the fluent interface
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. Directly, through initiating the class
  34. </para>
  35. </listitem>
  36. </itemizedlist>
  37. <para>
  38. A registered instance of <classname>Zend_Translate</classname> is the preferred
  39. usage for this helper. You can also select the locale to be used simply
  40. before you add the adapter to the registry.
  41. </para>
  42. <note>
  43. <para>
  44. We are speaking of locales instead of languages because a language
  45. also may contain a region. For example English is spoken in
  46. different dialects. There may be a translation for British and one
  47. for American English. Therefore, we say "locale" instead of
  48. "language."
  49. </para>
  50. </note>
  51. <example id="zend.view.helpers.initial.translate.registered">
  52. <title>Registered instance</title>
  53. <para>
  54. To use a registered instance just create an instance of
  55. <classname>Zend_Translate</classname> or <classname>Zend_Translate_Adapter</classname>
  56. and register it within <classname>Zend_Registry</classname> using
  57. <classname>Zend_Translate</classname> as its key.
  58. </para>
  59. <programlisting language="php"><![CDATA[
  60. // our example adapter
  61. $adapter = new Zend_Translate(
  62. array(
  63. 'adapter' => 'array',
  64. 'content' => array('simple' => 'einfach'),
  65. 'locale' => 'de'
  66. )
  67. );
  68. Zend_Registry::set('Zend_Translate', $adapter);
  69. // within your view
  70. echo $this->translate('simple');
  71. // this returns 'einfach'
  72. ]]></programlisting>
  73. </example>
  74. <para>
  75. If you are more familiar with the fluent interface, then you can also
  76. create an instance within your view and initiate the helper afterwards.
  77. </para>
  78. <example id="zend.view.helpers.initial.translate.afterwards">
  79. <title>Within the view</title>
  80. <para>
  81. To use the fluent interface, create an instance of
  82. <classname>Zend_Translate</classname> or <classname>Zend_Translate_Adapter</classname>,
  83. call the helper without a parameter, and call the
  84. <methodname>setTranslator()</methodname> method.
  85. </para>
  86. <programlisting language="php"><![CDATA[
  87. // within your view
  88. $adapter = new Zend_Translate(
  89. array(
  90. 'adapter' => 'array',
  91. 'content' => array('simple' => 'einfach'),
  92. 'locale' => 'de'
  93. )
  94. );
  95. $this->translate()->setTranslator($adapter)->translate('simple');
  96. // this returns 'einfach'
  97. ]]></programlisting>
  98. </example>
  99. <para>
  100. If you are using the helper without <classname>Zend_View</classname> then you can
  101. also use it directly.
  102. </para>
  103. <example id="zend.view.helpers.initial.translate.directly">
  104. <title>Direct usage</title>
  105. <programlisting language="php"><![CDATA[
  106. // our example adapter
  107. $adapter = new Zend_Translate(
  108. array(
  109. 'adapter' => 'array',
  110. 'content' => array('simple' => 'einfach'),
  111. 'locale' => 'de'
  112. )
  113. );
  114. // initiate the adapter
  115. $translate = new Zend_View_Helper_Translate($adapter);
  116. print $translate->translate('simple'); // this returns 'einfach'
  117. ]]></programlisting>
  118. <para>
  119. You would use this way if you are not working with
  120. <classname>Zend_View</classname> and need to create translated output.
  121. </para>
  122. </example>
  123. <para>
  124. As already seen, the <methodname>translate()</methodname> method is used to return
  125. the translation. Just call it with the needed messageid of your
  126. translation adapter. But it can also replace parameters within the
  127. translation string. Therefore, it accepts variable parameters in two ways:
  128. either as a list of parameters, or as an array of parameters. As examples:
  129. </para>
  130. <example id="zend.view.helpers.initial.translate.parameter">
  131. <title>Single parameter</title>
  132. <para>
  133. To use a single parameter just add it to the method.
  134. </para>
  135. <programlisting language="php"><![CDATA[
  136. // within your view
  137. $date = "Monday";
  138. $this->translate("Today is %1\$s", $date);
  139. // could return 'Heute ist Monday'
  140. ]]></programlisting>
  141. </example>
  142. <note>
  143. <para>
  144. Keep in mind that if you are using parameters which are also text,
  145. you may also need to translate these parameters.
  146. </para>
  147. </note>
  148. <example id="zend.view.helpers.initial.translate.parameterlist">
  149. <title>List of parameters</title>
  150. <para>
  151. Or use a list of parameters and add it to the method.
  152. </para>
  153. <programlisting language="php"><![CDATA[
  154. // within your view
  155. $date = "Monday";
  156. $month = "April";
  157. $time = "11:20:55";
  158. $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s",
  159. $date,
  160. $month,
  161. $time);
  162. // Could return 'Heute ist Monday in April. Aktuelle Zeit: 11:20:55'
  163. ]]></programlisting>
  164. </example>
  165. <example id="zend.view.helpers.initial.translate.parameterarray">
  166. <title>Array of parameters</title>
  167. <para>
  168. Or use an array of parameters and add it to the method.
  169. </para>
  170. <programlisting language="php"><![CDATA[
  171. // within your view
  172. $date = array("Monday", "April", "11:20:55");
  173. $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date);
  174. // Could return 'Heute ist Monday in April. Aktuelle Zeit: 11:20:55'
  175. ]]></programlisting>
  176. </example>
  177. <para>
  178. Sometimes it is necessary to change the locale of the translation. This
  179. can be done either dynamically per translation or statically for all
  180. following translations. And you can use it with both a parameter list
  181. and an array of parameters. In both cases the locale must be given as
  182. the last single parameter.
  183. </para>
  184. <example id="zend.view.helpers.initial.translate.dynamic">
  185. <title>Change locale dynamically</title>
  186. <programlisting language="php"><![CDATA[
  187. // within your view
  188. $date = array("Monday", "April", "11:20:55");
  189. $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date, 'it');
  190. ]]></programlisting>
  191. </example>
  192. <para>
  193. This example returns the Italian translation for the messageid. But it
  194. will only be used once. The next translation will use the locale from
  195. the adapter. Normally you will set the desired locale within the
  196. translation adapter before you add it to the registry. But you can also
  197. set the locale from within the helper:
  198. </para>
  199. <example id="zend.view.helpers.initial.translate.static">
  200. <title>Change locale statically</title>
  201. <programlisting language="php"><![CDATA[
  202. // within your view
  203. $date = array("Monday", "April", "11:20:55");
  204. $this->translate()->setLocale('it');
  205. $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date);
  206. ]]></programlisting>
  207. </example>
  208. <para>
  209. The above example sets <emphasis>'it'</emphasis> as the new default locale which
  210. will be used for all further translations.
  211. </para>
  212. <para>
  213. Of course there is also a <methodname>getLocale()</methodname> method to get the
  214. currently set locale.
  215. </para>
  216. <example id="zend.view.helpers.initial.translate.getlocale">
  217. <title>Get the currently set locale</title>
  218. <programlisting language="php"><![CDATA[
  219. // within your view
  220. $date = array("Monday", "April", "11:20:55");
  221. // returns 'de' as set default locale from our above examples
  222. $this->translate()->getLocale();
  223. $this->translate()->setLocale('it');
  224. $this->translate("Today is %1\$s in %2\$s. Actual time: %3\$s", $date);
  225. // returns 'it' as new set default locale
  226. $this->translate()->getLocale();
  227. ]]></programlisting>
  228. </example>
  229. </sect3>
  230. <!--
  231. vim:se ts=4 sw=4 et:
  232. -->