Zend_Translate-Using.xml 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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 Zend Translate 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('gettext', '/my/path/source-de.mo', 'de');
  34. $translate->addTranslation('/path/to/translation/fr-source.mo', 'fr');
  35. print $translate->_("Example") . "\n";
  36. print "=======\n";
  37. print $translate->_("Here is line one") . "\n";
  38. printf($translate->_("Today is the %1\$s") . "\n", date('d.m.Y'));
  39. print "\n";
  40. $translate->setLocale('fr');
  41. print $translate->_("Here is line two") . "\n";
  42. ]]></programlisting>
  43. </example>
  44. <para>
  45. Now let's take a deeper look into what has been done and how to
  46. integrate <classname>Zend_Translate</classname> into your own code.
  47. </para>
  48. <para>
  49. Create a new <classname>Zend_Translate</classname> object and define the base adapter:
  50. <programlisting language="php"><![CDATA[
  51. $translate = new Zend_Translate
  52. 'gettext',
  53. '/path/to/translation/source-de.mo',
  54. 'de'
  55. );
  56. ]]></programlisting>
  57. In this example we chose the
  58. <emphasis>Gettext Adapter</emphasis>.
  59. We place our file <emphasis>source-de.mo</emphasis>
  60. into the directory <emphasis>/path/to/translation</emphasis>.
  61. The gettext file will have German translation included,
  62. and we also added another language source for French.
  63. </para>
  64. <para>
  65. The next step is to wrap all strings which are to be translated.
  66. The simplest approach is to have only simple strings or sentences
  67. like this:
  68. <programlisting language="php"><![CDATA[
  69. print $translate->_("Example") . "\n";
  70. print "=======\n";
  71. print $translate->_("Here is line one") . "\n";
  72. ]]></programlisting>
  73. Some strings do not needed to be translated.
  74. The separating line is always a separating line,
  75. even in other languages.
  76. </para>
  77. <para>
  78. Having data values integrated into a translation string is also
  79. supported through the use of embedded parameters.
  80. <programlisting language="php"><![CDATA[
  81. printf($translate->_("Today is the %1\$s") . "\n", date("d.m.Y"));
  82. ]]></programlisting>
  83. Instead of <code>print()</code>, use the <code>printf()</code>
  84. function and replace all parameters with <code>%1\$s</code> parts.
  85. The first is <code>%1\$s</code>, the second is <code>%2\$s</code>,
  86. and so on. This way a translation can be done without knowing
  87. the exact value. In our example, the date is always the actual day,
  88. but the string can be translated without the knowledge of the actual
  89. day.
  90. </para>
  91. <para>
  92. Each string is identified in the translation storage by a message ID.
  93. You can use message IDs instead of strings in your code, like this:
  94. <programlisting language="php"><![CDATA[
  95. print $translate->_(1) . "\n";
  96. print "=======\n";
  97. print $translate->_(2) . "\n";
  98. ]]></programlisting>
  99. But doing this has several disadvantages:
  100. </para>
  101. <para>
  102. You can not see what your code should output just by viewing your code.
  103. </para>
  104. <para>
  105. Also you will have problems if some strings are not translated.
  106. You must always keep in mind how translation works.
  107. First <classname>Zend_Translate</classname> checks whether the specified language has a translation
  108. for the given message ID or string.
  109. If no translation string has been found it refers to the next lower
  110. level language as defined within <classname>Zend_Locale</classname>.
  111. So "<emphasis>de_AT</emphasis>" becomes
  112. "<emphasis>de</emphasis>" only.
  113. If there is no translation found for
  114. "<emphasis>de</emphasis>" either,
  115. then the original message is returned.
  116. This way you always have an output, even in case the message translation
  117. does not exist in your message storage.
  118. <classname>Zend_Translate</classname> never throws an error or exception when translating
  119. strings.
  120. </para>
  121. <sect2 id="zend.translate.using.structure">
  122. <title>Translation Source Structures</title>
  123. <para>
  124. Your next step is to create the translation sources for the
  125. languages you want to translate.
  126. Every adapter is created its own way as described here,
  127. but there are common features applicable for all adapters.
  128. </para>
  129. <para>
  130. You have to decide where to store your translation source files.
  131. Using <classname>Zend_Translate</classname> you are not restricted in any way.
  132. The following structures are preferable:
  133. </para>
  134. <itemizedlist>
  135. <listitem>
  136. <para>
  137. Single structured source
  138. </para>
  139. <programlisting><![CDATA[
  140. /application/
  141. /languages/
  142. /languages/lang.en
  143. /languages/lang.de
  144. /library/
  145. ]]></programlisting>
  146. <para>
  147. Positive: all source files for every languages are stored
  148. in one directory. No splitting of related files.
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. Language structured source
  154. </para>
  155. <programlisting><![CDATA[
  156. /application/
  157. /languages/
  158. /languages/en/
  159. /languages/en/first.en
  160. /languages/en/second.en
  161. /languages/de/
  162. /languages/de/first.de
  163. /languages/de/second.de
  164. /library
  165. ]]></programlisting>
  166. <para>
  167. Positive: Every language is stored in their own directories.
  168. Easy translation, as every language team has to translate
  169. only one directory. Also the usage of multiple files is transparent.
  170. </para>
  171. </listitem>
  172. <listitem>
  173. <para>
  174. Application structured source
  175. </para>
  176. <programlisting><![CDATA[
  177. /application/
  178. /application/languages/
  179. /application/languages/first.en
  180. /application/languages/first.de
  181. /application/languages/second.en
  182. /application/languages/second.de
  183. /library/
  184. ]]></programlisting>
  185. <para>
  186. Positive: all source files for every language are stored
  187. in one directory. No splitting of related files.
  188. </para>
  189. <para>
  190. Negative: having multiple files for the same language can be
  191. problematic.
  192. </para>
  193. </listitem>
  194. <listitem>
  195. <para>
  196. Gettext structured source
  197. </para>
  198. <programlisting><![CDATA[
  199. /application/
  200. /languages/
  201. /languages/de/
  202. /languages/de/LC_MESSAGES/
  203. /languages/de/LC_MESSAGES/first.mo
  204. /languages/de/LC_MESSAGES/second.mo
  205. /languages/en/
  206. /languages/en/LC_MESSAGES/
  207. /languages/en/LC_MESSAGES/first.mo
  208. /languages/en/LC_MESSAGES/second.mo
  209. /library/
  210. ]]></programlisting>
  211. <para>
  212. Positive: existing gettext sources can be used without changing
  213. structure.
  214. </para>
  215. <para>
  216. Negative: having sub-sub directories may be confusing
  217. for people who have not used gettext before.
  218. </para>
  219. </listitem>
  220. <listitem>
  221. <para>
  222. File structured source
  223. </para>
  224. <programlisting><![CDATA[
  225. /application/
  226. /application/models/
  227. /application/models/MyModel.php
  228. /application/models/MyModel.de
  229. /application/models/MyModel.en
  230. /application/controllers/
  231. /application/controllers/MyController.php
  232. /application/controllers/MyController.de
  233. /application/controllers/MyController.en
  234. /library/
  235. ]]></programlisting>
  236. <para>
  237. Positive: translation files are localted near their source.
  238. </para>
  239. <para>
  240. Negative: too many and also small translation files result in
  241. being tendious to translate.
  242. Also every file has to be added as translation source.
  243. </para>
  244. </listitem>
  245. </itemizedlist>
  246. <para>
  247. Single structured and language structured source files are most
  248. usable for <classname>Zend_Translate</classname>.
  249. </para>
  250. <para>
  251. So now, that we know which structure we want to have,
  252. we should create our translation source files.
  253. </para>
  254. </sect2>
  255. <sect2 id="zend.translate.using.source.array">
  256. <title>Creating Array source files</title>
  257. <para>
  258. Array source files are plain arrays. But you have to define them
  259. manually since there is no tool to aid this.
  260. But because they are so simple, it's the fastest way to look up
  261. messages if your code works as expected. It's generally the best
  262. adapter to get started with translation business.
  263. </para>
  264. <programlisting language="php"><![CDATA[
  265. $english = array(
  266. 'message1' => 'message1',
  267. 'message2' => 'message2',
  268. 'message3' => 'message3');
  269. $german = array(
  270. 'message1' => 'Nachricht1',
  271. 'message2' => 'Nachricht2',
  272. 'message3' => 'Nachricht3');
  273. $translate = new Zend_Translate('array', $english, 'en');
  274. $translate->addTranslation($deutsch, 'de');
  275. ]]></programlisting>
  276. <para>
  277. Since release 1.5 it is also supported to have arrays included within an external file.
  278. You just have to provide the filename and <classname>Zend_Translate</classname> will automatically
  279. include it and look for the array. See the following example for details:
  280. </para>
  281. <programlisting language="php"><![CDATA[
  282. // myarray.php
  283. return array(
  284. 'message1' => 'Nachricht1',
  285. 'message2' => 'Nachricht2',
  286. 'message3' => 'Nachricht3');
  287. // controller
  288. $translate = new Zend_Translate('array', '/path/to/myarray.php', 'de');
  289. ]]></programlisting>
  290. <note>
  291. <para>
  292. Files which do not return an array will fail to be included.
  293. Also any output within this file will be ignored and suppressed.
  294. </para>
  295. </note>
  296. </sect2>
  297. <sect2 id="zend.translate.using.source.gettext">
  298. <title>Creating Gettext source files</title>
  299. <para>
  300. Gettext source files are created by GNU's gettext library.
  301. There are several free tools available that can parse your
  302. code files and create the needed gettext source files.
  303. These have the extension <emphasis>*.mo</emphasis>
  304. and they are binary files.
  305. An open source tool for creating the files is
  306. <ulink url="http://sourceforge.net/projects/poedit/">poEdit</ulink>.
  307. This tool also supports you during the translation process itself.
  308. </para>
  309. <programlisting language="php"><![CDATA[
  310. // We accume that we have created the mo files and translated them
  311. $translate = new Zend_Translate('gettext', '/path/to/english.mo', 'en');
  312. $translate->addTranslation('/path/to/german.mo', 'de');
  313. ]]></programlisting>
  314. <para>
  315. As you can see the adapters are used exactly the same way,
  316. with one small difference:
  317. change <emphasis>array</emphasis> to <emphasis>gettext</emphasis>. All other usages are exactly
  318. the same as with all other adapters.
  319. With the gettext adapter you no longer have to be aware of
  320. gettext's standard directory structure,
  321. bindtextdomain and textdomain.
  322. Just give the path and filename to the adapter.
  323. </para>
  324. <note>
  325. <para>
  326. You should always use UTF-8 as source encoding.
  327. Otherwise you will have problems when using two
  328. different source encodings.
  329. E.g. one of your source files is encoded
  330. with ISO-8815-11 and another one with CP815.
  331. You can set only one encoding for your source file,
  332. so one of your languages probably will not display correctly.
  333. </para>
  334. <para>
  335. UTF-8 is a portable format which supports all languages.
  336. When using UTF-8 for all languages, you will eliminate
  337. the problem of incompatible encodings.
  338. </para>
  339. </note>
  340. <para>
  341. Many gettext editors add adapter informations as empty translation string.
  342. This is the reason why empty strings are not translated when using the
  343. gettext adapter. Instead they are erased from the translation table and
  344. provided by the <code>getAdapterInfo()</code> method. It will return
  345. the adapter informations for all added gettext files as array using the
  346. filename as key.
  347. </para>
  348. <programlisting language="php"><![CDATA[
  349. // Getting the adapter informations
  350. $translate = new Zend_Translate('gettext', '/path/to/english.mo', 'en');
  351. print_r($translate->getAdapterInfo());
  352. ]]></programlisting>
  353. </sect2>
  354. <sect2 id="zend.translate.using.source.tmx">
  355. <title>Creating TMX source files</title>
  356. <para>
  357. TMX source files are a new industry standard.
  358. They have the advantage of being XML files and so they are
  359. readable by every editor and of course by humans.
  360. You can either create TMX files manually with a text editor,
  361. or you can use a special tool. But most tools currently available for
  362. creating TMX source files are not freely available.
  363. </para>
  364. <example id="zend.translate.using.source.tmx.example">
  365. <title>Example TMX file</title>
  366. <programlisting language="xml"><![CDATA[
  367. <?xml version="1.0" ?>
  368. <!DOCTYPE tmx SYSTEM "tmx14.dtd">
  369. <tmx version="1.4">
  370. <header creationtoolversion="1.0.0" datatype="winres" segtype="sentence"
  371. adminlang="en-us" srclang="de-at" o-tmf="abc"
  372. creationtool="XYZTool" >
  373. </header>
  374. <body>
  375. <tu tuid='message1'>
  376. <tuv xml:lang="de"><seg>Nachricht1</seg></tuv>
  377. <tuv xml:lang="en"><seg>message1</seg></tuv>
  378. </tu>
  379. <tu tuid='message2'>
  380. <tuv xml:lang="en"><seg>message2</seg></tuv>
  381. <tuv xml:lang="de"><seg>Nachricht2</seg></tuv>
  382. </tu>
  383. ]]></programlisting>
  384. <programlisting language="php"><![CDATA[
  385. $translate = new Zend_Translate('tmx', 'path/to/mytranslation.tmx', 'en');
  386. ]]></programlisting>
  387. </example>
  388. <para>
  389. TMX files can have several languages within the same file.
  390. All other included languages are added automatically,
  391. so you do not have to call <code>addLanguage()</code>.
  392. </para>
  393. <para>
  394. If you want to have only specified languages from the source translated
  395. you can set the option '<code>defined_language</code>' to <code>true</code>.
  396. With this option you can add the wished languages explicitly with
  397. <code>addLanguage()</code>. The default value for this option is to add all
  398. languages.
  399. </para>
  400. </sect2>
  401. <sect2 id="zend.translate.using.source.csv">
  402. <title>Creating CSV source files</title>
  403. <para>
  404. CSV source files are small and human readable.
  405. If your customers want to translate their own,
  406. you will probably use the CSV adapter.
  407. </para>
  408. <example id="zend.translate.using.source.csv.example">
  409. <title>Example CSV file</title>
  410. <programlisting><![CDATA[
  411. #Example csv file
  412. message1;Nachricht1
  413. message2;Nachricht2
  414. ]]></programlisting>
  415. <programlisting language="php"><![CDATA[
  416. $translate = new Zend_Translate('csv', '/path/to/mytranslation.csv', 'de');
  417. $translate->addTranslation('path/to/other.csv', 'fr');
  418. ]]></programlisting>
  419. </example>
  420. <para>
  421. There are three different options for the CSV adapter.
  422. You can set '<code>delimiter</code>', '<code>limit</code>' and
  423. '<code>enclosure</code>'.
  424. </para>
  425. <para>
  426. The default delimiter for CSV string is '<code>;</code>', but
  427. with the option '<code>delimiter</code>'
  428. you can decide to use another one.
  429. </para>
  430. <para>
  431. The default limit for a line within a CSV file is '<code>0</code>'. This means
  432. that the end of a CSV line is searched automatically. If you set
  433. '<code>limit</code>' to any value, then the CSV file will be
  434. read faster, but any line exceeding this limit will be truncated.
  435. </para>
  436. <para>
  437. The default enclosure to use for CSV files is '<code>"</code>'. You can
  438. set a different one using the option '<code>enclosure</code>'.
  439. </para>
  440. <example id="zend.translate.using.source.csv.example2">
  441. <title>Second CSV file example</title>
  442. <programlisting><![CDATA[
  443. # Example CSV file
  444. "message,1",Nachricht1
  445. message2,"Nachricht,2"
  446. "message3,",Nachricht3
  447. ]]></programlisting>
  448. <programlisting language="php"><![CDATA[
  449. $translate = new Zend_Translate(
  450. 'csv',
  451. '/path/to/mytranslation.csv',
  452. 'de',
  453. array('delimiter' => ','));
  454. $translate->addTranslation('/path/to/other.csv', 'fr');
  455. ]]></programlisting>
  456. </example>
  457. </sect2>
  458. <sect2 id="zend.translate.using.source.ini">
  459. <title>Creating INI source files</title>
  460. <para>
  461. INI source files are human readable but normally not very small as they also
  462. include other data beside translations. If you have data which shall be
  463. editable by your customers you can use the INI adapter.
  464. </para>
  465. <example id="zend.translate.using.source.ini.example">
  466. <title>Example INI file</title>
  467. <programlisting><![CDATA[
  468. [Test]
  469. ;TestPage Comment
  470. Message_1="Nachricht 1 (de)"
  471. Message_2="Nachricht 2 (de)"
  472. Message_3="Nachricht :3 (de)"
  473. ]]></programlisting>
  474. <programlisting language="php"><![CDATA[
  475. $translate = new Zend_Translate('ini', '/path/to/mytranslation.ini', 'de');
  476. $translate->addTranslation('/path/to/other.ini', 'it');
  477. ]]></programlisting>
  478. </example>
  479. <para>
  480. INI files have several restrictions. If a value in the ini file contains any
  481. non-alphanumeric characters it needs to be enclosed in double-quotes (<code>"</code>).
  482. There are also reserved words which must not be used as keys for ini files.
  483. These include: <code>null</code>, <code>yes</code>, <code>no</code>, <code>true</code>,
  484. and <code>false</code>. Values <code>null</code>, <code>no</code> and <code>false</code> results
  485. in <code>""</code>, <code>yes</code> and <code>true</code> results in <code>1</code>. Characters <code>{}|&amp;~![()"</code> must not be used anywhere
  486. in the key and have a special meaning in the value. Do not use them as it will
  487. produce unexpected behaviour.
  488. </para>
  489. </sect2>
  490. <sect2 id="zend.translate.using.options">
  491. <title>Options for adapters</title>
  492. <para>
  493. Options can be used with all adapters. Of course the options are different for all adapters.
  494. You can set options when you create the adapter. Actually there is one option which is available
  495. to all adapters: '<code>clear</code>' sets if translation data should be added to existing
  496. one or not. Standard behaviour is to add new translation data to existing one. But the
  497. translation data is only cleared for the selected language. So other languages remain
  498. untouched.
  499. </para>
  500. <para>
  501. You can set options temporarily when using <code>addTranslation($data, $locale, array $options = array())</code>
  502. as third and optional parameter. And you can use the method <code>setOptions()</code> to
  503. set the options permanently.
  504. </para>
  505. <example id="zend.translate.using.options.example">
  506. <title>Using translation options</title>
  507. <programlisting language="php"><![CDATA[
  508. // define ':' as separator for the translation source files
  509. $options = array('delimiter' => ':');
  510. $translate = new Zend_Translate(
  511. 'csv',
  512. '/path/to/mytranslation.csv',
  513. 'de',
  514. $options);
  515. ...
  516. // clear the defined language and use new translation data
  517. $options = array('clear' => true);
  518. $translate->addTranslation('/path/to/new.csv', 'fr', $options);
  519. ]]></programlisting>
  520. </example>
  521. <para>
  522. Here you can find all available options for the different adapters with a description of their usage:
  523. </para>
  524. <table id="zend.translate.using.options.alloptions">
  525. <title>Options for translation adapters</title>
  526. <tgroup cols="4">
  527. <thead>
  528. <row>
  529. <entry>Option</entry>
  530. <entry>Adapter</entry>
  531. <entry>Description</entry>
  532. <entry>Default value</entry>
  533. </row>
  534. </thead>
  535. <tbody>
  536. <row>
  537. <entry>clear</entry>
  538. <entry>all</entry>
  539. <entry>
  540. If set to true, the already read translations will be cleared. This can be used
  541. instead of creating a new instance when reading new translation data
  542. </entry>
  543. <entry><emphasis>false</emphasis></entry>
  544. </row>
  545. <row>
  546. <entry>disableNotices</entry>
  547. <entry>all</entry>
  548. <entry>
  549. If set to true, all notices regarding not available translations will be
  550. disabled. You should set this option to true in production environment
  551. </entry>
  552. <entry><emphasis>false</emphasis></entry>
  553. </row>
  554. <row>
  555. <entry>ignore</entry>
  556. <entry>all</entry>
  557. <entry>
  558. All directories and files beginning with this prefix will be ignored when
  559. searching for files. This value defaults to <emphasis>'.'</emphasis>
  560. which leads to the behavior that all hidden files will be ignored. Setting this
  561. value to <code>'tmp'</code> would mean that directories and files like
  562. <code>'tmpImages'</code> and <code>'tmpFiles'</code>
  563. would be ignored as well as all subsequent directories
  564. </entry>
  565. <entry><emphasis>.</emphasis></entry>
  566. </row>
  567. <row>
  568. <entry>log</entry>
  569. <entry>all</entry>
  570. <entry>
  571. An instance of <classname>Zend_Log</classname> where untranslated messages and notices will be
  572. written to
  573. </entry>
  574. <entry><emphasis>null</emphasis></entry>
  575. </row>
  576. <row>
  577. <entry>logMessage</entry>
  578. <entry>all</entry>
  579. <entry>
  580. The message which will be written into the log
  581. </entry>
  582. <entry><emphasis>Untranslated message within '%locale%': %message%</emphasis></entry>
  583. </row>
  584. <row>
  585. <entry>logUntranslated</entry>
  586. <entry>all</entry>
  587. <entry>
  588. When this option is set to true, all message IDs which can not be
  589. translated will be written into the attached log
  590. </entry>
  591. <entry><emphasis>false</emphasis></entry>
  592. </row>
  593. <row>
  594. <entry>scan</entry>
  595. <entry>all</entry>
  596. <entry>
  597. If set to null, no scanning of the directory structure will be done.
  598. If set to <classname>Zend_Translate::LOCALE_DIRECTORY</classname> the locale will be detected within the
  599. directory. If set to <classname>Zend_Translate::LOCALE_FILENAME</classname> the locale will be detected
  600. within the filename. See <xref linkend="zend.translate.using.detection" />
  601. for details
  602. </entry>
  603. <entry><emphasis>null</emphasis></entry>
  604. </row>
  605. <row>
  606. <entry>delimiter</entry>
  607. <entry>Csv</entry>
  608. <entry>Defines which sign is used as delimiter for separating source and translation</entry>
  609. <entry><emphasis>;</emphasis></entry>
  610. </row>
  611. <row>
  612. <entry>enclosure</entry>
  613. <entry>Csv</entry>
  614. <entry>Defines the enclosure character to be used. Defaults to a doublequote</entry>
  615. <entry><emphasis>"</emphasis></entry>
  616. </row>
  617. <row>
  618. <entry>length</entry>
  619. <entry>Csv</entry>
  620. <entry>Defines the maximum length of a csv line. When set to 0 it will be detected automatically</entry>
  621. <entry><emphasis>0</emphasis></entry>
  622. </row>
  623. </tbody>
  624. </tgroup>
  625. </table>
  626. <para>
  627. When you want to have self defined options, you are also able to use them within all adapters.
  628. The <code>setOptions()</code> method can be used to define your option. <code>setOptions()</code>
  629. needs an array with the options you want to set. If an given option exists it will be signed over.
  630. You can define as much options as needed as they will not be checked by the adapter. Just make sure
  631. not to overwrite any existing option which is used by an adapter.
  632. </para>
  633. <para>
  634. To return the option you can use the <code>getOptions()</code> method. When <code>getOptions()</code>
  635. is called without a parameter it will return all options set. When the optional parameter is given
  636. you will only get the specified option.
  637. </para>
  638. </sect2>
  639. <sect2 id="zend.translate.using.languages">
  640. <title>Handling languages</title>
  641. <para>
  642. When working with different languages there are a few methods which will be useful.
  643. </para>
  644. <para>
  645. The <code>getLocale()</code> method can be used to get the currently set language. It can either hold
  646. an instance of <classname>Zend_Locale</classname> or the identifier of a locale.
  647. </para>
  648. <para>
  649. The <code>setLocale()</code> method sets a new standard language for translation. This prevents the
  650. need of setting the optional language parameter more than once to the <code>translate()</code> method.
  651. If the given language does not exist, or no translation data is available for the language,
  652. <code>setLocale()</code> tries to downgrade to the language without the region if any was given.
  653. A language of <code>en_US</code> would be downgraded to <code>en</code>. When even the downgraded
  654. language can not be found an exception will be thrown.
  655. </para>
  656. <para>
  657. The <code>isAvailable()</code> method checks if a given language is already available. It returns
  658. <code>true</code> if data for the given language exist.
  659. </para>
  660. <para>
  661. And finally the <code>getList()</code> method can be used to get all currently set languages for an adapter
  662. returned as array.
  663. </para>
  664. <example id="zend.translate.using.languages.example">
  665. <title>Handling languages with adapters</title>
  666. <programlisting language="php"><![CDATA[
  667. // returns the currently set language
  668. $actual = $translate->getLocale();
  669. // you can use the optional parameter while translating
  670. echo $translate->_("my_text", "fr");
  671. // or set a new language
  672. $translate->setLocale("fr");
  673. echo $translate->_("my_text");
  674. // refer to the base language
  675. // fr_CH will be downgraded to fr
  676. $translate->setLocale("fr_CH");
  677. echo $translate->_("my_text");
  678. // check if this language exist
  679. if ($translate->isAvailable("fr")) {
  680. // language exists
  681. }
  682. ]]></programlisting>
  683. </example>
  684. <sect3 id="zend.translate.using.languages.automatic">
  685. <title>Automatical handling of languages</title>
  686. <para>
  687. Note that as long as you only add new translation sources with the <code>addTranslation()</code>
  688. method <classname>Zend_Translate</classname> will automatically set the best fitting language for your
  689. environment when you use one of the automatic locales which are '<code>auto</code>' or '<code>browser</code>'. So
  690. normally you will not need to call <code>setLocale()</code>. This should only be used in
  691. conjunction with automatic source detection.
  692. </para>
  693. <para>
  694. The algorithm will search for the best fitting locale depending on the user's browser and
  695. your environment. See the following example for details:
  696. </para>
  697. <example id="zend.translate.using.languages.automatic.example">
  698. <title>Automatically language detection</title>
  699. <programlisting language="php"><![CDATA[
  700. // Let's expect the browser returns these language settings:
  701. // HTTP_ACCEPT_LANGUAGE = "de_AT=1;fr=1;en_US=0.8";
  702. // Example 1:
  703. // When no fitting language is found, the message ID is returned
  704. $translate = new Zend_Translate(
  705. 'gettext',
  706. 'my_it.mo',
  707. 'auto',
  708. array('scan' => Zend_Translate::LOCALE_FILENAME));
  709. // Example 2:
  710. // Best found fitting language is 'fr'
  711. $translate = new Zend_Translate(
  712. 'gettext',
  713. 'my_fr.mo',
  714. 'auto',
  715. array('scan' => Zend_Translate::LOCALE_FILENAME));
  716. // Example 3:
  717. // Best found fitting language is 'de' ('de_AT' will be degraded)
  718. $translate = new Zend_Translate(
  719. 'gettext',
  720. 'my_de.mo',
  721. 'auto',
  722. array('scan' => Zend_Translate::LOCALE_FILENAME));
  723. // Example 4:
  724. // Returns 'it' as translation source and overrides the automatic settings
  725. $translate = new Zend_Translate(
  726. 'gettext',
  727. 'my_it.mo',
  728. 'auto',
  729. array('scan' => Zend_Translate::LOCALE_FILENAME));
  730. $translate->addTranslation('my_ru.mo', 'ru');
  731. $translate->setLocale('it_IT');
  732. ]]></programlisting>
  733. </example>
  734. <para>
  735. After setting a language manually with the <code>setLocale()</code> method the automatic
  736. detection will be switched off and overridden.
  737. </para>
  738. <para>
  739. If you want to use it again, you can set the language
  740. <emphasis>auto</emphasis> with <code>setLocale()</code> which will reactivate
  741. the automatic detection for <classname>Zend_Translate</classname>.
  742. </para>
  743. <para>
  744. Since Zend Framework 1.7.0 <classname>Zend_Translate</classname> also recognises an application
  745. wide locale. You can simply set a <classname>Zend_Locale</classname> instance to the registry like shown
  746. below. With this notation you can forget about setting the locale manually with each instance
  747. when you want to use the same locale multiple times.
  748. </para>
  749. <programlisting language="php"><![CDATA[
  750. // in your bootstrap file
  751. $locale = new Zend_Locale();
  752. Zend_Registry::set('Zend_Locale', $locale);
  753. // default language when requested language is not available
  754. $defaultlanguage = 'en';
  755. // somewhere in your application
  756. $translate = new Zend_Translate('gettext', 'my_de.mo');
  757. if (!$translate->isAvailable($locale->getLanguage())) {
  758. // not available languages are rerouted to another language
  759. $translate->setLocale($defaultlanguage);
  760. }
  761. $translate->getLocale();
  762. ]]></programlisting>
  763. </sect3>
  764. </sect2>
  765. <sect2 id="zend.translate.using.detection">
  766. <title>Automatic source detection</title>
  767. <para>
  768. <classname>Zend_Translate</classname> can detect translation sources automatically. So you don't have
  769. to declare each source file manually. You can let <classname>Zend_Translate</classname> do this job and
  770. scan the complete directory structure for source files.
  771. </para>
  772. <note>
  773. <para>
  774. Automatic source detection is available since Zend Framework version 1.5 .
  775. </para>
  776. </note>
  777. <para>
  778. The usage is quite the same as initiating a single translation source with one difference.
  779. You must give a directory which has to be scanned instead a file.
  780. </para>
  781. <example id="zend.translate.using.languages.directory.example">
  782. <title>Scanning a directory structure for sources</title>
  783. <programlisting language="php"><![CDATA[
  784. // assuming we have the following structure
  785. // /language/
  786. // /language/login/login.tmx
  787. // /language/logout/logout.tmx
  788. // /language/error/loginerror.tmx
  789. // /language/error/logouterror.tmx
  790. $translate = new Zend_Translate('tmx', '/language');
  791. ]]></programlisting>
  792. </example>
  793. <para>
  794. So <classname>Zend_Translate</classname> does not only search the given directory, but also all subdirectories for
  795. translation source files. This makes the usage quite simple. But <classname>Zend_Translate</classname> will ignore all
  796. files which are not sources or which produce failures while reading the translation data. So you
  797. have to make sure that all of your translation sources are correct and readable because you will
  798. not get any failure if a file is bogus or can not be read.
  799. </para>
  800. <note>
  801. <para>
  802. Depending on how deep your directory structure is and how much files are within this structure
  803. it can take a long time for <classname>Zend_Translate</classname> to complete.
  804. </para>
  805. </note>
  806. <para>
  807. In our example we have used the TMX format which includes the language to be used within the
  808. source. But many of the other source formats are not able to include the language within the
  809. file. Even this sources can be used with automatic scanning if you do some pre-requisits as
  810. described below:
  811. </para>
  812. <sect3 id="zend.translate.using.detection.directory">
  813. <title>Language through naming directories</title>
  814. <para>
  815. One way to include automatic language detection is to name the directories related to the
  816. language which is used for the sources within this directory. This is the easiest way and
  817. is used for example within standard gettext implementations.
  818. </para>
  819. <para>
  820. <classname>Zend_Translate</classname> needs the '<code>scan</code>' option to know that it should search the names of all
  821. directories for languages. See the following example for details:
  822. </para>
  823. <example id="zend.translate.using.detection.directory.example">
  824. <title>Directory scanning for languages</title>
  825. <programlisting language="php"><![CDATA[
  826. // assuming we have the following structure
  827. // /language/
  828. // /language/de/login/login.mo
  829. // /language/de/error/loginerror.mo
  830. // /language/en/login/login.mo
  831. // /language/en/error/loginerror.mo
  832. $translate = new Zend_Translate(
  833. 'gettext',
  834. '/language',
  835. null,
  836. array('scan' => Zend_Translate::LOCALE_DIRECTORY));
  837. ]]></programlisting>
  838. </example>
  839. <note>
  840. <para>
  841. This works only for adapters which do not include the language within the source file.
  842. Using this option for example with TMX will be ignored. Also language definitions within
  843. the filename will be ignored when using this option.
  844. </para>
  845. </note>
  846. <note>
  847. <para>
  848. You should be aware if you have several subdirectories under the same
  849. structure. Assuming we have a structure like
  850. <code>/language/module/de/en/file.mo</code>. In this case the path contains
  851. multiple strings which would be detected as locale. It could be either
  852. <code>de</code> or <code>en</code>. In such a case the behaviour is
  853. undefined and it is recommended to use file detection in such situations.
  854. </para>
  855. </note>
  856. </sect3>
  857. <sect3 id="zend.translate.using.detection.filename">
  858. <title>Language through filenames</title>
  859. <para>
  860. Another way to detect the language automatically is to use special filenames. You can either
  861. name the complete file or parts of a file after the used language. To use this way of detection
  862. you will have to set the '<code>scan</code>' option at initiation. There are several ways of naming the
  863. sourcefiles which are described below:
  864. </para>
  865. <example id="zend.translate.using.detection.filename.example">
  866. <title>Filename scanning for languages</title>
  867. <programlisting language="php"><![CDATA[
  868. // assuming we have the following structure
  869. // /language/
  870. // /language/login/login_en.mo
  871. // /language/login/login_de.mo
  872. // /language/error/loginerror_en.mo
  873. // /language/error/loginerror_de.mo
  874. $translate = new Zend_Translate(
  875. 'gettext',
  876. '/language',
  877. null,
  878. array('scan' => Zend_Translate::LOCALE_FILENAME));
  879. ]]></programlisting>
  880. </example>
  881. <sect4 id="zend.translate.using.detection.filename.complete">
  882. <title>Complete filename</title>
  883. <para>
  884. Having the whole file named after the language is the simplest way but only viable
  885. if you have only one file per language.
  886. </para>
  887. <programlisting><![CDATA[
  888. /languages/
  889. /languages/en.mo
  890. /languages/de.mo
  891. /languages/es.mo
  892. ]]></programlisting>
  893. </sect4>
  894. <sect4 id="zend.translate.using.detection.filename.extension">
  895. <title>Extension of the file</title>
  896. <para>
  897. Another simple way to use the extension of the file for language detection.
  898. But this may be confusing since you will no longer have an idea which extension the file
  899. originally had.
  900. </para>
  901. <programlisting><![CDATA[
  902. /languages/
  903. /languages/view.en
  904. /languages/view.de
  905. /languages/view.es
  906. ]]></programlisting>
  907. </sect4>
  908. <sect4 id="zend.translate.using.detection.filename.token">
  909. <title>Filename tokens</title>
  910. <para>
  911. <classname>Zend_Translate</classname> is also capable of detecting the language if it is included within the
  912. filename. But if you go this way you will have to separate the language with a token.
  913. There are three supported tokens which can be used: a dot '.', an underscore '_', or
  914. a hyphen '-'.
  915. </para>
  916. <programlisting><![CDATA[
  917. /languages/
  918. /languages/view_en.mo -> detects english
  919. /languages/view_de.mo -> detects german
  920. /languages/view_it.mo -> detects italian
  921. ]]></programlisting>
  922. <para>
  923. The first found string delimited by a token which can be interpreted as a locale will be used. See the following
  924. example for details.
  925. </para>
  926. <programlisting><![CDATA[
  927. /languages/
  928. /languages/view_en_de.mo -> detects english
  929. /languages/view_en_es.mo -> detects english and overwrites the first file
  930. /languages/view_it_it.mo -> detects italian
  931. ]]></programlisting>
  932. <para>
  933. All three tokens are used to detect the locale. When the filename contains multiple tokens,
  934. the first found token depends on the order of the tokens which are used. See the following
  935. example for details.
  936. </para>
  937. <programlisting><![CDATA[
  938. /languages/
  939. /languages/view_en-it.mo -> detects english because '_' will be used before '-'
  940. /languages/view-en_it.mo -> detects italian because '_' will be used before '-'
  941. /languages/view_en.it.mo -> detects italian because '.' will be used before '_'
  942. ]]></programlisting>
  943. </sect4>
  944. </sect3>
  945. </sect2>
  946. <sect2 id="zend.translate.using.istranslated">
  947. <title>Checking for translations</title>
  948. <para>
  949. Normally text will be translated without any computation. But sometimes it is necessary to
  950. know if a text is translated or not, therefor the <code>isTranslated()</code>
  951. method can be used.
  952. </para>
  953. <para>
  954. <code>isTranslated($messageId, $original = false, $locale = null)</code> takes
  955. the text you want to check as its first parameter, and as optional third parameter the locale
  956. for which you want to do the check. The optional second parameter declares whether translation
  957. is fixed to the declared language or a lower set of translations can be used. If you have a text which
  958. can be returned for 'en' but not for 'en_US' you will normally get the translation returned, but by
  959. setting <code>$original</code> to true, <code>isTranslated()</code> will return false.
  960. </para>
  961. <example id="zend.translate.using.istranslated.example">
  962. <title>Checking if a text is translatable</title>
  963. <programlisting language="php"><![CDATA[
  964. $english = array(
  965. 'message1' => 'Nachricht 1',
  966. 'message2' => 'Nachricht 2',
  967. 'message3' => 'Nachricht 3');
  968. $translate = new Zend_Translate('array', $english, 'de_AT');
  969. if ($translate->isTranslated('message1')) {
  970. print "'message1' can be translated";
  971. }
  972. if (!($translate->isTranslated('message1', true, 'de'))) {
  973. print "'message1' can not be translated to 'de'"
  974. . " as it's available only in 'de_AT'";
  975. }
  976. if ($translate->isTranslated('message1', false, 'de')) {
  977. print "'message1' can be translated in 'de_AT' as it falls back to 'de'";
  978. }
  979. ]]></programlisting>
  980. </example>
  981. </sect2>
  982. <sect2 id="zend.translate.using.logging">
  983. <title>How to log not found translations</title>
  984. <para>
  985. When you have a bigger site or you are creating the translation files manually, you often have
  986. the problem that some messages are not translated. But there is an easy solution for you when you
  987. are using <classname>Zend_Translate</classname>.
  988. </para>
  989. <para>
  990. You have to follow two or three simple steps. First, you have to create an instance of
  991. <classname>Zend_Log</classname>. Then you have to attach this instance to <classname>Zend_Translate</classname>.
  992. See the following example:
  993. </para>
  994. <example id="zend.translate.using.logging.example">
  995. <title>Log translations</title>
  996. <programlisting language="php"><![CDATA[
  997. $translate = new Zend_Translate('gettext', $path, 'de');
  998. // Create a log instance
  999. $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
  1000. $log = new Zend_Log($writer);
  1001. // Attach it to the translation instance
  1002. $translate->setOptions(array(
  1003. 'log' => $log,
  1004. 'logUntranslated' => true));
  1005. $translate->translate('unknown string');
  1006. ]]></programlisting>
  1007. </example>
  1008. <para>
  1009. Now you will have a new notice in the log: <code>Untranslated message within 'de': unknown string</code>.
  1010. </para>
  1011. <note>
  1012. <para>
  1013. You should note that any translation which can not be found will be logged. This means
  1014. all translations when a user requests a language which is not supported. Also every request
  1015. for a message which can not be translated will be logged. Be aware, that 100 people
  1016. requesting the same translation, will result 100 logged notices.
  1017. </para>
  1018. </note>
  1019. <para>
  1020. This feature can not only be used to log messages but also to attach this untranslated messages
  1021. into an empty translation file. To do so you will have to write your own log writer which
  1022. writes the format you want to have and strips the prepending "Untranslated message".
  1023. </para>
  1024. <para>
  1025. You can also set the '<code>logMessage</code>' option when you want to have your own log message.
  1026. Use the '<code>%message%</code>' token for placing the messageId within your log message, and the
  1027. '<code>%locale%</code>' token for the requested locale. See the following example for a self
  1028. defined log message:
  1029. </para>
  1030. <example id="zend.translate.using.logging.example2">
  1031. <title>Self defined log messages</title>
  1032. <programlisting language="php"><![CDATA[
  1033. $translate = new Zend_Translate('gettext', $path, 'de');
  1034. // Create a log instance
  1035. $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
  1036. $log = new Zend_Log($writer);
  1037. // Attach it to the translation instance
  1038. $translate->setOptions(array(
  1039. 'log' => $log,
  1040. 'logMessage' => "Missing '%message%' within locale '%locale%'",
  1041. 'logUntranslated' => true));
  1042. $translate->translate('unknown string');
  1043. ]]></programlisting>
  1044. </example>
  1045. </sect2>
  1046. <sect2 id="zend.translate.using.sourcedata">
  1047. <title>Accessing source data</title>
  1048. <para>
  1049. Sometimes it is useful to have access to the translation source data. Therefor
  1050. the following two functions are provided.
  1051. </para>
  1052. <para>
  1053. The <code>getMessageIds($locale = null)</code> method returns all known message IDs as array.
  1054. </para>
  1055. <para>
  1056. The <code>getMessages($locale = null)</code> method returns the complete translation source as
  1057. an array. The message ID is used as key and the translation data as value.
  1058. </para>
  1059. <para>
  1060. Both methods accept an optional parameter <code>$locale</code> which, if set, returns the
  1061. translation data for the specified language. If this parameter is not given, the actual set
  1062. language will be used. Keep in mind that normally all translations should be available in all
  1063. languages. Which means that in a normal situation you will not have to set this parameter.
  1064. </para>
  1065. <para>
  1066. Additionally the <code>getMessages()</code> method can be used to return the complete
  1067. translation dictionary using the pseudo-locale 'all'. This will return all available
  1068. translation data for each added locale.
  1069. </para>
  1070. <note>
  1071. <para>
  1072. Attention: the returned array can be <emphasis>very big</emphasis>,
  1073. depending on the number of added locales and the amount of translation data.
  1074. </para>
  1075. </note>
  1076. <example id="zend.translate.using.sourcedata.example">
  1077. <title>Handling languages with adapters</title>
  1078. <programlisting language="php"><![CDATA[
  1079. // returns all known message IDs
  1080. $messageIds = $translate->getMessageIds();
  1081. print_r($messageIds);
  1082. // or just for the specified language
  1083. $messageIds = $translate->getMessageIds('en_US');
  1084. print_r($messageIds);
  1085. // returns all the complete translation data
  1086. $source = $translate->getMessages();
  1087. print_r($source);
  1088. ]]></programlisting>
  1089. </example>
  1090. </sect2>
  1091. </sect1>
  1092. <!--
  1093. vim:se ts=4 sw=4 et:
  1094. -->