Zend_Translate-Using.xml 10.0 KB

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