Zend_Translate-SourceCreation.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 16394 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.translate.sourcecreation">
  5. <title>Creating source files</title>
  6. <para>
  7. Below you will find a description of the different source formats
  8. which can be used with <classname>Zend_Translate</classname>.
  9. </para>
  10. <note>
  11. <para>
  12. Note that most of the described formats should be created by using
  13. a tool or a generation process. These Tools and processes are not part
  14. of Zend Framework and for most of the described formats free tools
  15. are available.
  16. </para>
  17. </note>
  18. <sect2 id="zend.translate.sourcecreation.array">
  19. <title>Creating Array source files</title>
  20. <para>
  21. Array source files are plain arrays. But you have to define them
  22. manually since there is no tool to aid this.
  23. But because they are so simple, it's the fastest way to look up
  24. messages if your code works as expected. It's generally the best
  25. adapter to get started with translation business.
  26. </para>
  27. <programlisting role="php"><![CDATA[
  28. $english = array(
  29. 'message1' => 'message1',
  30. 'message2' => 'message2',
  31. 'message3' => 'message3');
  32. $german = array(
  33. 'message1' => 'Nachricht1',
  34. 'message2' => 'Nachricht2',
  35. 'message3' => 'Nachricht3');
  36. $translate = new Zend_Translate('array', $english, 'en');
  37. $translate->addTranslation($deutsch, 'de');
  38. ]]></programlisting>
  39. <para>
  40. Since release 1.5 it is also supported to have arrays included within an external file.
  41. You just have to provide the filename and <classname>Zend_Translate</classname> will automatically
  42. include it and look for the array. See the following example for details:
  43. </para>
  44. <programlisting role="php"><![CDATA[
  45. // myarray.php
  46. return array(
  47. 'message1' => 'Nachricht1',
  48. 'message2' => 'Nachricht2',
  49. 'message3' => 'Nachricht3');
  50. // controller
  51. $translate = new Zend_Translate('array', '/path/to/myarray.php', 'de');
  52. ]]></programlisting>
  53. <note>
  54. <para>
  55. Files which do not return an array will fail to be included.
  56. Also any output within this file will be ignored and suppressed.
  57. </para>
  58. </note>
  59. </sect2>
  60. <sect2 id="zend.translate.sourcecreation.gettext">
  61. <title>Creating Gettext source files</title>
  62. <para>
  63. Gettext source files are created by GNU's gettext library.
  64. There are several free tools available that can parse your
  65. code files and create the needed gettext source files.
  66. These have the extension <emphasis>*.mo</emphasis>
  67. and they are binary files.
  68. An open source tool for creating the files is
  69. <ulink url="http://sourceforge.net/projects/poedit/">poEdit</ulink>.
  70. This tool also supports you during the translation process itself.
  71. </para>
  72. <programlisting role="php"><![CDATA[
  73. // We accume that we have created the mo files and translated them
  74. $translate = new Zend_Translate('gettext', '/path/to/english.mo', 'en');
  75. $translate->addTranslation('/path/to/german.mo', 'de');
  76. ]]></programlisting>
  77. <para>
  78. As you can see the adapters are used exactly the same way,
  79. with one small difference:
  80. change <emphasis>array</emphasis> to <emphasis>gettext</emphasis>. All other usages are exactly
  81. the same as with all other adapters.
  82. With the gettext adapter you no longer have to be aware of
  83. gettext's standard directory structure,
  84. bindtextdomain and textdomain.
  85. Just give the path and filename to the adapter.
  86. </para>
  87. <note>
  88. <para>
  89. You should always use UTF-8 as source encoding.
  90. Otherwise you will have problems when using two
  91. different source encodings.
  92. E.g. one of your source files is encoded
  93. with ISO-8815-11 and another one with CP815.
  94. You can set only one encoding for your source file,
  95. so one of your languages probably will not display correctly.
  96. </para>
  97. <para>
  98. UTF-8 is a portable format which supports all languages.
  99. When using UTF-8 for all languages, you will eliminate
  100. the problem of incompatible encodings.
  101. </para>
  102. </note>
  103. <para>
  104. Many gettext editors add adapter informations as empty translation string.
  105. This is the reason why empty strings are not translated when using the
  106. gettext adapter. Instead they are erased from the translation table and
  107. provided by the <code>getAdapterInfo()</code> method. It will return
  108. the adapter informations for all added gettext files as array using the
  109. filename as key.
  110. </para>
  111. <programlisting role="php"><![CDATA[
  112. // Getting the adapter informations
  113. $translate = new Zend_Translate('gettext', '/path/to/english.mo', 'en');
  114. print_r($translate->getAdapterInfo());
  115. ]]></programlisting>
  116. </sect2>
  117. <sect2 id="zend.translate.sourcecreation.tmx">
  118. <title>Creating TMX source files</title>
  119. <para>
  120. TMX source files are a new industry standard.
  121. They have the advantage of being XML files and so they are
  122. readable by every editor and of course by humans.
  123. You can either create TMX files manually with a text editor,
  124. or you can use a special tool. But most tools currently available for
  125. creating TMX source files are not freely available.
  126. </para>
  127. <example id="zend.translate.sourcecreation.tmx.example">
  128. <title>Example TMX file</title>
  129. <programlisting role="xml"><![CDATA[
  130. <?xml version="1.0" ?>
  131. <!DOCTYPE tmx SYSTEM "tmx14.dtd">
  132. <tmx version="1.4">
  133. <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence"
  134. adminlang="en-us" srclang="de-at" o-tmf="abc"
  135. creationtool="XYZTool" >
  136. </header>
  137. <body>
  138. <tu tuid='message1'>
  139. <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
  140. <tuv xml:lang="en"><seg>message1</seg></tuv>
  141. </tu>
  142. <tu tuid='message2'>
  143. <tuv xml:lang="en"><seg>message2</seg></tuv>
  144. <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
  145. </tu>
  146. ]]></programlisting>
  147. <programlisting role="php"><![CDATA[
  148. $translate = new Zend_Translate('tmx', 'path/to/mytranslation.tmx', 'en');
  149. ]]></programlisting>
  150. </example>
  151. <para>
  152. TMX files can have several languages within the same file.
  153. All other included languages are added automatically,
  154. so you do not have to call <code>addLanguage()</code>.
  155. </para>
  156. <para>
  157. If you want to have only specified languages from the source translated
  158. you can set the option '<code>defined_language</code>' to <constant>TRUE</constant>.
  159. With this option you can add the wished languages explicitly with
  160. <code>addLanguage()</code>. The default value for this option is to add all
  161. languages.
  162. </para>
  163. </sect2>
  164. <sect2 id="zend.translate.sourcecreation.csv">
  165. <title>Creating CSV source files</title>
  166. <para>
  167. CSV source files are small and human readable.
  168. If your customers want to translate their own,
  169. you will probably use the CSV adapter.
  170. </para>
  171. <example id="zend.translate.sourcecreation.csv.example">
  172. <title>Example CSV file</title>
  173. <programlisting language="txt"><![CDATA[
  174. #Example csv file
  175. message1;Nachricht1
  176. message2;Nachricht2
  177. ]]></programlisting>
  178. <programlisting role="php"><![CDATA[
  179. $translate = new Zend_Translate('csv', '/path/to/mytranslation.csv', 'de');
  180. $translate->addTranslation('path/to/other.csv', 'fr');
  181. ]]></programlisting>
  182. </example>
  183. <para>
  184. There are three different options for the CSV adapter.
  185. You can set '<code>delimiter</code>', '<code>limit</code>' and
  186. '<code>enclosure</code>'.
  187. </para>
  188. <para>
  189. The default delimiter for CSV string is '<code>;</code>', but
  190. with the option '<code>delimiter</code>'
  191. you can decide to use another one.
  192. </para>
  193. <para>
  194. The default limit for a line within a CSV file is '<code>0</code>'. This means
  195. that the end of a CSV line is searched automatically. If you set
  196. '<code>limit</code>' to any value, then the CSV file will be
  197. read faster, but any line exceeding this limit will be truncated.
  198. </para>
  199. <para>
  200. The default enclosure to use for CSV files is '<code>"</code>'. You can
  201. set a different one using the option '<code>enclosure</code>'.
  202. </para>
  203. <example id="zend.translate.sourcecreation.csv.example2">
  204. <title>Second CSV file example</title>
  205. <programlisting language="txt"><![CDATA[
  206. # Example CSV file
  207. "message,1",Nachricht1
  208. message2,"Nachricht,2"
  209. "message3,",Nachricht3
  210. ]]></programlisting>
  211. <programlisting role="php"><![CDATA[
  212. $translate = new Zend_Translate(
  213. 'csv',
  214. '/path/to/mytranslation.csv',
  215. 'de',
  216. array('delimiter' => ','));
  217. $translate->addTranslation('/path/to/other.csv', 'fr');
  218. ]]></programlisting>
  219. </example>
  220. <note>
  221. <para>
  222. When you are using non-ASCII characters within your CSV file, like umlauts or UTF-8
  223. chars, then you should always use enclosure. Omitting the enclosure can lead to
  224. missing characters in your translation.
  225. </para>
  226. </note>
  227. </sect2>
  228. <sect2 id="zend.translate.sourcecreation.ini">
  229. <title>Creating INI source files</title>
  230. <para>
  231. INI source files are human readable but normally not very small as they also
  232. include other data beside translations. If you have data which shall be
  233. editable by your customers you can use the INI adapter.
  234. </para>
  235. <example id="zend.translate.sourcecreation.ini.example">
  236. <title>Example INI file</title>
  237. <programlisting language="txt"><![CDATA[
  238. [Test]
  239. ;TestPage Comment
  240. Message_1="Nachricht 1 (de)"
  241. Message_2="Nachricht 2 (de)"
  242. Message_3="Nachricht :3 (de)"
  243. ]]></programlisting>
  244. <programlisting role="php"><![CDATA[
  245. $translate = new Zend_Translate('ini', '/path/to/mytranslation.ini', 'de');
  246. $translate->addTranslation('/path/to/other.ini', 'it');
  247. ]]></programlisting>
  248. </example>
  249. <para>
  250. INI files have several restrictions. If a value in the ini file contains any
  251. non-alphanumeric characters it needs to be enclosed in double-quotes (<code>"</code>).
  252. There are also reserved words which must not be used as keys for ini files.
  253. These include: <constant>NULL</constant>, <code>yes</code>, <code>no</code>, <constant>TRUE</constant>,
  254. and <constant>FALSE</constant>. Values <constant>NULL</constant>, <code>no</code> and <constant>FALSE</constant> results
  255. in <code>""</code>, <code>yes</code> and <constant>TRUE</constant> results in <code>1</code>. Characters <code>{}|&amp;~![()"</code> must not be used anywhere
  256. in the key and have a special meaning in the value. Do not use them as it will
  257. produce unexpected behaviour.
  258. </para>
  259. </sect2>
  260. </sect1>
  261. <!--
  262. vim:se ts=4 sw=4 et:
  263. -->