Zend_Translate-SourceCreation.xml 12 KB

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