Zend_Translate-SourceCreation.xml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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(
  36. array(
  37. 'adapter' => 'array',
  38. 'content' => $english,
  39. 'locale' => 'en'
  40. )
  41. );
  42. $translate->addTranslation(array('content' => $german, 'locale' => 'de'));
  43. ]]></programlisting>
  44. <para>
  45. Since release 1.5 it is also supported to have arrays included within an external file.
  46. You just have to provide the filename and <classname>Zend_Translate</classname> will
  47. automatically include it and look for the array. See the following example for details:
  48. </para>
  49. <programlisting language="php"><![CDATA[
  50. // myarray.php
  51. return array(
  52. 'message1' => 'Nachricht1',
  53. 'message2' => 'Nachricht2',
  54. 'message3' => 'Nachricht3');
  55. // controller
  56. $translate = new Zend_Translate(
  57. array(
  58. 'adapter' => 'array',
  59. 'content' => '/path/to/myarray.php',
  60. 'locale' => 'de'
  61. )
  62. );
  63. ]]></programlisting>
  64. <note>
  65. <para>
  66. Files which do not return an array will fail to be included.
  67. Also any output within this file will be ignored and suppressed.
  68. </para>
  69. </note>
  70. </sect2>
  71. <sect2 id="zend.translate.sourcecreation.gettext">
  72. <title>Creating Gettext source files</title>
  73. <para>
  74. Gettext source files are created by GNU's gettext library.
  75. There are several free tools available that can parse your
  76. code files and create the needed gettext source files.
  77. These have the extension <emphasis>*.mo</emphasis>
  78. and they are binary files.
  79. An open source tool for creating the files is
  80. <ulink url="http://sourceforge.net/projects/poedit/">poEdit</ulink>.
  81. This tool also supports you during the translation process itself.
  82. </para>
  83. <programlisting language="php"><![CDATA[
  84. // We accume that we have created the mo files and translated them
  85. $translate = new Zend_Translate(
  86. array(
  87. 'adapter' => 'gettext',
  88. 'content' => '/path/to/english.mo',
  89. 'locale' => 'en'
  90. )
  91. );
  92. $translate->addTranslation(
  93. array(
  94. 'content' => '/path/to/german.mo',
  95. 'locale' => 'de'
  96. )
  97. );
  98. ]]></programlisting>
  99. <para>
  100. As you can see the adapters are used exactly the same way,
  101. with one small difference:
  102. change <emphasis>array</emphasis> to <emphasis>gettext</emphasis>. All other usages are
  103. exactly the same as with all other adapters.
  104. With the gettext adapter you no longer have to be aware of
  105. gettext's standard directory structure,
  106. bindtextdomain and textdomain.
  107. Just give the path and filename to the adapter.
  108. </para>
  109. <note>
  110. <para>
  111. You should always use UTF-8 as source encoding.
  112. Otherwise you will have problems when using two
  113. different source encodings.
  114. E.g. one of your source files is encoded
  115. with ISO-8815-11 and another one with CP815.
  116. You can set only one encoding for your source file,
  117. so one of your languages probably will not display correctly.
  118. </para>
  119. <para>
  120. UTF-8 is a portable format which supports all languages.
  121. When using UTF-8 for all languages, you will eliminate
  122. the problem of incompatible encodings.
  123. </para>
  124. </note>
  125. <para>
  126. Many gettext editors add adapter informations as empty translation string.
  127. This is the reason why empty strings are not translated when using the
  128. gettext adapter. Instead they are erased from the translation table and
  129. provided by the <methodname>getAdapterInfo()</methodname> method. It will return
  130. the adapter informations for all added gettext files as array using the
  131. filename as key.
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. // Getting the adapter informations
  135. $translate = new Zend_Translate(
  136. array(
  137. 'adapter' => 'gettext',
  138. 'content' => '/path/to/english.mo',
  139. 'locale' => 'en'
  140. )
  141. );
  142. print_r($translate->getAdapterInfo());
  143. ]]></programlisting>
  144. </sect2>
  145. <sect2 id="zend.translate.sourcecreation.tmx">
  146. <title>Creating TMX source files</title>
  147. <para>
  148. TMX source files are a new industry standard.
  149. They have the advantage of being <acronym>XML</acronym> files and so they are
  150. readable by every editor and of course by humans.
  151. You can either create TMX files manually with a text editor,
  152. or you can use a special tool. But most tools currently available for
  153. creating TMX source files are not freely available.
  154. </para>
  155. <example id="zend.translate.sourcecreation.tmx.example">
  156. <title>Example TMX file</title>
  157. <programlisting language="xml"><![CDATA[
  158. <?xml version="1.0" ?>
  159. <!DOCTYPE tmx SYSTEM "tmx14.dtd">
  160. <tmx version="1.4">
  161. <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence"
  162. adminlang="en-us" srclang="de-at" o-tmf="abc"
  163. creationtool="XYZTool" >
  164. </header>
  165. <body>
  166. <tu tuid='message1'>
  167. <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
  168. <tuv xml:lang="en"><seg>message1</seg></tuv>
  169. </tu>
  170. <tu tuid='message2'>
  171. <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
  172. <tuv xml:lang="en"><seg>message2</seg></tuv>
  173. </tu>
  174. </body>
  175. </tmx>
  176. ]]></programlisting>
  177. <programlisting language="php"><![CDATA[
  178. $translate = new Zend_Translate(
  179. array(
  180. 'adapter' => 'tmx',
  181. 'content' => 'path/to/mytranslation.tmx',
  182. 'locale' => 'en'
  183. )
  184. );
  185. ]]></programlisting>
  186. </example>
  187. <para>
  188. TMX files can have several languages within the same file.
  189. All other included languages are added automatically,
  190. so you do not have to call <methodname>addLanguage()</methodname>.
  191. </para>
  192. <para>
  193. If you want to have only specified languages from the source translated you can
  194. set the option <property>defined_language</property> to <constant>TRUE</constant>.
  195. With this option you can add the wished languages explicitly with
  196. <methodname>addLanguage()</methodname>. The default value for this option is to add all
  197. languages.
  198. </para>
  199. <note>
  200. <title>Option useId</title>
  201. <para>
  202. When you set the <emphasis>useId</emphasis> option to <constant>FALSE</constant>
  203. then the <emphasis>srclang</emphasis> header will be used to define the language
  204. which sets the message.
  205. </para>
  206. <para>
  207. In our example the message key would <emphasis>message1</emphasis> per default.
  208. When this option is set to <constant>FALSE</constant> the message key
  209. <emphasis>Nachricht1</emphasis> would be used.
  210. </para>
  211. <para>
  212. Note that the <emphasis>tuv</emphasis> entry which is related to the
  213. <emphasis>srclang</emphasis> entry must be the first
  214. <emphasis>tuv</emphasis> entry which is set like shown in the above example.
  215. </para>
  216. </note>
  217. </sect2>
  218. <sect2 id="zend.translate.sourcecreation.csv">
  219. <title>Creating CSV source files</title>
  220. <para>
  221. CSV source files are small and human readable.
  222. If your customers want to translate their own,
  223. you will probably use the CSV adapter.
  224. </para>
  225. <example id="zend.translate.sourcecreation.csv.example">
  226. <title>Example CSV file</title>
  227. <programlisting language="txt"><![CDATA[
  228. #Example csv file
  229. message1;Nachricht1
  230. message2;Nachricht2
  231. ]]></programlisting>
  232. <programlisting language="php"><![CDATA[
  233. $translate = new Zend_Translate(
  234. array(
  235. 'adapter' => 'csv',
  236. 'content' => '/path/to/mytranslation.csv',
  237. 'locale' => 'de'
  238. )
  239. );
  240. $translate->addTranslation(
  241. array(
  242. 'content' => 'path/to/other.csv',
  243. 'locale' => 'fr'
  244. )
  245. );
  246. ]]></programlisting>
  247. </example>
  248. <para>
  249. There are three different options for the CSV adapter.
  250. You can set <property>delimiter</property>, <property>limit</property> and
  251. <property>enclosure</property>.
  252. </para>
  253. <para>
  254. The default delimiter for CSV string is '<emphasis>;</emphasis>', but
  255. with the option <property>delimiter</property>
  256. you can decide to use another one.
  257. </para>
  258. <para>
  259. The default limit for a line within a CSV file is '<emphasis>0</emphasis>'. This means
  260. that the end of a CSV line is searched automatically. If you set
  261. <property>limit</property> to any value, then the CSV file will be
  262. read faster, but any line exceeding this limit will be truncated.
  263. </para>
  264. <para>
  265. The default enclosure to use for CSV files is '<emphasis>"</emphasis>'. You can
  266. set a different one using the option <property>enclosure</property>.
  267. </para>
  268. <example id="zend.translate.sourcecreation.csv.example2">
  269. <title>Second CSV file example</title>
  270. <programlisting language="txt"><![CDATA[
  271. # Example CSV file
  272. "message,1",Nachricht1
  273. message2,"Nachricht,2"
  274. "message3,",Nachricht3
  275. ]]></programlisting>
  276. <programlisting language="php"><![CDATA[
  277. $translate = new Zend_Translate(
  278. array(
  279. 'adapter' => 'csv',
  280. 'content' => '/path/to/mytranslation.csv',
  281. 'locale' => 'de',
  282. 'delimiter' => ','
  283. )
  284. );
  285. $translate->addTranslation(
  286. array(
  287. 'content' => '/path/to/other.csv',
  288. 'locale' => 'fr'
  289. )
  290. );
  291. ]]></programlisting>
  292. </example>
  293. <note>
  294. <para>
  295. When you are using non-ASCII characters within your CSV file, like umlauts or UTF-8
  296. chars, then you should always use enclosure. Omitting the enclosure can lead to
  297. missing characters in your translation.
  298. </para>
  299. </note>
  300. </sect2>
  301. <sect2 id="zend.translate.sourcecreation.ini">
  302. <title>Creating INI source files</title>
  303. <para>
  304. <acronym>INI</acronym> source files are human readable but normally not very small as
  305. they also include other data beside translations. If you have data which shall be
  306. editable by your customers you can use the <acronym>INI</acronym> adapter.
  307. </para>
  308. <example id="zend.translate.sourcecreation.ini.example">
  309. <title>Example INI file</title>
  310. <programlisting language="txt"><![CDATA[
  311. [Test]
  312. ;TestPage Comment
  313. Message_1="Nachricht 1 (de)"
  314. Message_2="Nachricht 2 (de)"
  315. Message_3="Nachricht :3 (de)"
  316. ]]></programlisting>
  317. <programlisting language="php"><![CDATA[
  318. $translate = new Zend_Translate(
  319. array(
  320. 'adapter' => 'ini',
  321. 'content' => '/path/to/mytranslation.ini',
  322. 'locale' => 'de'
  323. )
  324. );
  325. $translate->addTranslation(
  326. array(
  327. 'content' => '/path/to/other.ini',
  328. 'locale' => 'it'
  329. )
  330. );
  331. ]]></programlisting>
  332. </example>
  333. <para>
  334. <acronym>INI</acronym> files have several restrictions. If a value in the
  335. <acronym>INI</acronym> file contains any non-alphanumeric characters it needs to be
  336. enclosed in double-quotes (<emphasis>"</emphasis>). There are also reserved words which
  337. must not be used as keys for <acronym>INI</acronym> files. These include:
  338. <constant>NULL</constant>, yes, no, <constant>TRUE</constant>, and
  339. <constant>FALSE</constant>. Values <constant>NULL</constant>, <emphasis>no</emphasis>
  340. and <constant>FALSE</constant> results in <emphasis>""</emphasis>,
  341. <emphasis>yes</emphasis> and <constant>TRUE</constant> results in '1'. Characters
  342. <emphasis>{}|&amp;~![()"</emphasis> must not be used anywhere in the key and have a
  343. special meaning in the value. Do not use them as it will produce unexpected behaviour.
  344. </para>
  345. </sect2>
  346. </sect1>
  347. <!--
  348. vim:se ts=4 sw=4 et:
  349. -->