Zend_Translate-Using.xml 9.9 KB

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