Zend_Translate-Using.xml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.translate.using">
  4. <title>Using Translation Adapters</title>
  5. <para>
  6. The next step is to use the adapter within your code.
  7. </para>
  8. <example id="zend.translate.using.example1">
  9. <title>Example of single-language PHP code</title>
  10. <programlisting language="php"><![CDATA[
  11. print "Example\n";
  12. print "=======\n";
  13. print "Here is line one\n";
  14. print "Today is the " . date("d.m.Y") . "\n";
  15. print "\n";
  16. print "Here is line two\n";
  17. ]]></programlisting>
  18. </example>
  19. <para>
  20. The example above shows some output with no support for translation.
  21. You probably write your code in your native language.
  22. Generally you need to translate not only the output,
  23. but also error and log messages.
  24. </para>
  25. <para>
  26. The next step is to integrate Zend Translate into your existing code.
  27. Of course it is much easier if you had already written your code with
  28. translation in mind, than changing your code afterwards.
  29. </para>
  30. <example id="zend.translate.using.example2">
  31. <title>Example of multi-lingual PHP code</title>
  32. <programlisting language="php"><![CDATA[
  33. $translate = new Zend_Translate('gettext', '/my/path/source-de.mo', 'de');
  34. $translate->addTranslation('/path/to/translation/fr-source.mo', 'fr');
  35. print $translate->_("Example") . "\n";
  36. print "=======\n";
  37. print $translate->_("Here is line one") . "\n";
  38. printf($translate->_("Today is the %1\$s") . "\n", date('d.m.Y'));
  39. print "\n";
  40. $translate->setLocale('fr');
  41. print $translate->_("Here is line two") . "\n";
  42. ]]></programlisting>
  43. </example>
  44. <para>
  45. Now let's take a deeper look into what has been done and how to
  46. integrate <classname>Zend_Translate</classname> into your own code.
  47. </para>
  48. <para>
  49. Create a new <classname>Zend_Translate</classname> object and define the base adapter:
  50. <programlisting language="php"><![CDATA[
  51. $translate = new Zend_Translate
  52. 'gettext',
  53. '/path/to/translation/source-de.mo',
  54. 'de'
  55. );
  56. ]]></programlisting>
  57. In this example we chose the
  58. <emphasis>Gettext Adapter</emphasis>.
  59. We place our file <emphasis>source-de.mo</emphasis>
  60. into the directory <emphasis>/path/to/translation</emphasis>.
  61. The gettext file will have German translation included,
  62. and we also added another language source for French.
  63. </para>
  64. <para>
  65. The next step is to wrap all strings which are to be translated.
  66. The simplest approach is to have only simple strings or sentences
  67. like this:
  68. <programlisting language="php"><![CDATA[
  69. print $translate->_("Example") . "\n";
  70. print "=======\n";
  71. print $translate->_("Here is line one") . "\n";
  72. ]]></programlisting>
  73. Some strings do not needed to be translated.
  74. The separating line is always a separating line,
  75. even in other languages.
  76. </para>
  77. <para>
  78. Having data values integrated into a translation string is also
  79. supported through the use of embedded parameters.
  80. <programlisting language="php"><![CDATA[
  81. printf($translate->_("Today is the %1\$s") . "\n", date("d.m.Y"));
  82. ]]></programlisting>
  83. Instead of <methodname>print()</methodname>, use the <methodname>printf()</methodname>
  84. function and replace all parameters with <code>%1\$s</code> parts.
  85. The first is <code>%1\$s</code>, the second is <code>%2\$s</code>,
  86. and so on. This way a translation can be done without knowing
  87. the exact value. In our example, the date is always the actual day,
  88. but the string can be translated without the knowledge of the actual
  89. day.
  90. </para>
  91. <para>
  92. Each string is identified in the translation storage by a message ID.
  93. You can use message IDs instead of strings in your code, like this:
  94. <programlisting language="php"><![CDATA[
  95. print $translate->_(1) . "\n";
  96. print "=======\n";
  97. print $translate->_(2) . "\n";
  98. ]]></programlisting>
  99. But doing this has several disadvantages:
  100. </para>
  101. <para>
  102. You can not see what your code should output just by viewing your code.
  103. </para>
  104. <para>
  105. Also you will have problems if some strings are not translated.
  106. You must always keep in mind how translation works.
  107. First <classname>Zend_Translate</classname> checks whether the specified language has a translation
  108. for the given message ID or string.
  109. If no translation string has been found it refers to the next lower
  110. level language as defined within <classname>Zend_Locale</classname>.
  111. So "<emphasis>de_AT</emphasis>" becomes
  112. "<emphasis>de</emphasis>" only.
  113. If there is no translation found for
  114. "<emphasis>de</emphasis>" either,
  115. then the original message is returned.
  116. This way you always have an output, even in case the message translation
  117. does not exist in your message storage.
  118. <classname>Zend_Translate</classname> never throws an error or exception when translating
  119. strings.
  120. </para>
  121. <sect2 id="zend.translate.using.structure">
  122. <title>Translation Source Structures</title>
  123. <para>
  124. Your next step is to create the translation sources for the
  125. languages you want to translate.
  126. Every adapter is created its own way as described here,
  127. but there are common features applicable for all adapters.
  128. </para>
  129. <para>
  130. You have to decide where to store your translation source files.
  131. Using <classname>Zend_Translate</classname> you are not restricted in any way.
  132. The following structures are preferable:
  133. </para>
  134. <itemizedlist>
  135. <listitem>
  136. <para>
  137. Single structured source
  138. </para>
  139. <programlisting language="txt"><![CDATA[
  140. /application/
  141. /languages/
  142. /languages/lang.en
  143. /languages/lang.de
  144. /library/
  145. ]]></programlisting>
  146. <para>
  147. Positive: all source files for every languages are stored
  148. in one directory. No splitting of related files.
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. Language structured source
  154. </para>
  155. <programlisting language="txt"><![CDATA[
  156. /application/
  157. /languages/
  158. /languages/en/
  159. /languages/en/first.en
  160. /languages/en/second.en
  161. /languages/de/
  162. /languages/de/first.de
  163. /languages/de/second.de
  164. /library
  165. ]]></programlisting>
  166. <para>
  167. Positive: Every language is stored in their own directories.
  168. Easy translation, as every language team has to translate
  169. only one directory. Also the usage of multiple files is transparent.
  170. </para>
  171. </listitem>
  172. <listitem>
  173. <para>
  174. Application structured source
  175. </para>
  176. <programlisting language="txt"><![CDATA[
  177. /application/
  178. /application/languages/
  179. /application/languages/first.en
  180. /application/languages/first.de
  181. /application/languages/second.en
  182. /application/languages/second.de
  183. /library/
  184. ]]></programlisting>
  185. <para>
  186. Positive: all source files for every language are stored
  187. in one directory. No splitting of related files.
  188. </para>
  189. <para>
  190. Negative: having multiple files for the same language can be
  191. problematic.
  192. </para>
  193. </listitem>
  194. <listitem>
  195. <para>
  196. Gettext structured source
  197. </para>
  198. <programlisting language="txt"><![CDATA[
  199. /application/
  200. /languages/
  201. /languages/de/
  202. /languages/de/LC_MESSAGES/
  203. /languages/de/LC_MESSAGES/first.mo
  204. /languages/de/LC_MESSAGES/second.mo
  205. /languages/en/
  206. /languages/en/LC_MESSAGES/
  207. /languages/en/LC_MESSAGES/first.mo
  208. /languages/en/LC_MESSAGES/second.mo
  209. /library/
  210. ]]></programlisting>
  211. <para>
  212. Positive: existing gettext sources can be used without changing
  213. structure.
  214. </para>
  215. <para>
  216. Negative: having sub-sub directories may be confusing
  217. for people who have not used gettext before.
  218. </para>
  219. </listitem>
  220. <listitem>
  221. <para>
  222. File structured source
  223. </para>
  224. <programlisting language="txt"><![CDATA[
  225. /application/
  226. /application/models/
  227. /application/models/MyModel.php
  228. /application/models/MyModel.de
  229. /application/models/MyModel.en
  230. /application/controllers/
  231. /application/controllers/MyController.php
  232. /application/controllers/MyController.de
  233. /application/controllers/MyController.en
  234. /library/
  235. ]]></programlisting>
  236. <para>
  237. Positive: translation files are localted near their source.
  238. </para>
  239. <para>
  240. Negative: too many and also small translation files result in
  241. being tedious to translate.
  242. Also every file has to be added as translation source.
  243. </para>
  244. </listitem>
  245. </itemizedlist>
  246. <para>
  247. Single structured and language structured source files are most
  248. usable for <classname>Zend_Translate</classname>.
  249. </para>
  250. <para>
  251. So now, that we know which structure we want to have,
  252. we should create our translation source files.
  253. </para>
  254. </sect2>
  255. </sect1>
  256. <!--
  257. vim:se ts=4 sw=4 et:
  258. -->