Zend_Translate-Additional.xml 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.translate.additional">
  4. <title>Additional features for translation</title>
  5. <para>
  6. There are several additional features which are supported by
  7. <classname>Zend_Translate</classname>. Read here for these additional informations.
  8. </para>
  9. <sect2 id="zend.translate.additional.options">
  10. <title>Options for adapters</title>
  11. <para>
  12. Options can be used with all adapters. Of course the options are different for all
  13. adapters. You can set options when you create the adapter. Actually there is one option
  14. which is available to all adapters: '<code>clear</code>' sets if translation data
  15. should be added to existing one or not. Standard behaviour is to add new translation
  16. data to existing one. But the translation data is only cleared for the selected
  17. language. So other languages remain untouched.
  18. </para>
  19. <para>
  20. You can set options temporarily when using <code>addTranslation($data, $locale, array
  21. $options = array())</code> as third and optional parameter. And you can use the method
  22. <methodname>setOptions()</methodname> to set the options permanently.
  23. </para>
  24. <example id="zend.translate..additional.options.example">
  25. <title>Using translation options</title>
  26. <programlisting language="php"><![CDATA[
  27. // define ':' as separator for the translation source files
  28. $options = array('delimiter' => ':');
  29. $translate = new Zend_Translate(
  30. 'csv',
  31. '/path/to/mytranslation.csv',
  32. 'de',
  33. $options);
  34. ...
  35. // clear the defined language and use new translation data
  36. $options = array('clear' => true);
  37. $translate->addTranslation('/path/to/new.csv', 'fr', $options);
  38. ]]></programlisting>
  39. </example>
  40. <para>
  41. Here you can find all available options for the different adapters with a description
  42. of their usage:
  43. </para>
  44. <table id="zend.translate.additional.options.alloptions">
  45. <title>Options for translation adapters</title>
  46. <tgroup cols="4">
  47. <thead>
  48. <row>
  49. <entry>Option</entry>
  50. <entry>Adapter</entry>
  51. <entry>Description</entry>
  52. <entry>Default value</entry>
  53. </row>
  54. </thead>
  55. <tbody>
  56. <row>
  57. <entry>clear</entry>
  58. <entry>all</entry>
  59. <entry>
  60. If set to true, the already read translations will be cleared. This can
  61. be used instead of creating a new instance when reading new translation
  62. data
  63. </entry>
  64. <entry><emphasis>false</emphasis></entry>
  65. </row>
  66. <row>
  67. <entry>disableNotices</entry>
  68. <entry>all</entry>
  69. <entry>
  70. If set to true, all notices regarding not available translations will
  71. be disabled. You should set this option to true in production
  72. environment
  73. </entry>
  74. <entry><emphasis>false</emphasis></entry>
  75. </row>
  76. <row>
  77. <entry>ignore</entry>
  78. <entry>all</entry>
  79. <entry>
  80. All directories and files beginning with this prefix will be ignored
  81. when searching for files. This value defaults to
  82. <emphasis>'.'</emphasis> which leads to the behavior that all hidden
  83. files will be ignored. Setting this value to <code>'tmp'</code> would
  84. mean that directories and files like <code>'tmpImages'</code> and
  85. <code>'tmpFiles'</code> would be ignored as well as all subsequent
  86. directories
  87. </entry>
  88. <entry><emphasis>.</emphasis></entry>
  89. </row>
  90. <row>
  91. <entry>log</entry>
  92. <entry>all</entry>
  93. <entry>
  94. An instance of <classname>Zend_Log</classname> where untranslated
  95. messages and notices will be written to
  96. </entry>
  97. <entry><emphasis>null</emphasis></entry>
  98. </row>
  99. <row>
  100. <entry>logMessage</entry>
  101. <entry>all</entry>
  102. <entry>
  103. The message which will be written into the log
  104. </entry>
  105. <entry>
  106. <emphasis>Untranslated message within '%locale%': %message%</emphasis>
  107. </entry>
  108. </row>
  109. <row>
  110. <entry>logUntranslated</entry>
  111. <entry>all</entry>
  112. <entry>
  113. When this option is set to true, all message IDs which can not be
  114. translated will be written into the attached log
  115. </entry>
  116. <entry><emphasis>false</emphasis></entry>
  117. </row>
  118. <row>
  119. <entry>scan</entry>
  120. <entry>all</entry>
  121. <entry>
  122. If set to null, no scanning of the directory structure will be done.
  123. If set to <constant>Zend_Translate::LOCALE_DIRECTORY</constant> the
  124. locale will be detected within the directory. If set to
  125. <constant>Zend_Translate::LOCALE_FILENAME</constant> the locale will
  126. be detected within the filename. See <xref
  127. linkend="zend.translate.additional.detection" /> for details
  128. </entry>
  129. <entry><emphasis>null</emphasis></entry>
  130. </row>
  131. <row>
  132. <entry>delimiter</entry>
  133. <entry>Csv</entry>
  134. <entry>
  135. Defines which sign is used as delimiter for separating source and
  136. translation
  137. </entry>
  138. <entry><emphasis>;</emphasis></entry>
  139. </row>
  140. <row>
  141. <entry>enclosure</entry>
  142. <entry>Csv</entry>
  143. <entry>
  144. Defines the enclosure character to be used. Defaults to a doublequote
  145. </entry>
  146. <entry><emphasis>"</emphasis></entry>
  147. </row>
  148. <row>
  149. <entry>length</entry>
  150. <entry>Csv</entry>
  151. <entry>
  152. Defines the maximum length of a csv line. When set to 0 it will be
  153. detected automatically
  154. </entry>
  155. <entry><emphasis>0</emphasis></entry>
  156. </row>
  157. </tbody>
  158. </tgroup>
  159. </table>
  160. <para>
  161. When you want to have self defined options, you are also able to use them within all
  162. adapters. The <methodname>setOptions()</methodname> method can be used to define your option.
  163. <methodname>setOptions()</methodname> needs an array with the options you want to set. If an given
  164. option exists it will be signed over. You can define as much options as needed as they
  165. will not be checked by the adapter. Just make sure not to overwrite any existing option
  166. which is used by an adapter.
  167. </para>
  168. <para>
  169. To return the option you can use the <methodname>getOptions()</methodname> method. When
  170. <methodname>getOptions()</methodname> is called without a parameter it will return all options set.
  171. When the optional parameter is given you will only get the specified option.
  172. </para>
  173. </sect2>
  174. <sect2 id="zend.translate.additional.languages">
  175. <title>Handling languages</title>
  176. <para>
  177. When working with different languages there are a few methods which will be useful.
  178. </para>
  179. <para>
  180. The <methodname>getLocale()</methodname> method can be used to get the currently set language. It
  181. can either hold an instance of <classname>Zend_Locale</classname> or the identifier of
  182. a locale.
  183. </para>
  184. <para>
  185. The <methodname>setLocale()</methodname> method sets a new standard language for translation. This
  186. prevents the need of setting the optional language parameter more than once to the
  187. <methodname>translate()</methodname> method. If the given language does not exist, or no
  188. translation data is available for the language, <methodname>setLocale()</methodname> tries to
  189. downgrade to the language without the region if any was given. A language of
  190. <code>en_US</code> would be downgraded to <code>en</code>. When even the downgraded
  191. language can not be found an exception will be thrown.
  192. </para>
  193. <para>
  194. The <methodname>isAvailable()</methodname> method checks if a given language is already available.
  195. It returns <constant>TRUE</constant> if data for the given language exist.
  196. </para>
  197. <para>
  198. And finally the <methodname>getList()</methodname> method can be used to get all currently set
  199. languages for an adapter returned as array.
  200. </para>
  201. <example id="zend.translate.additional.languages.example">
  202. <title>Handling languages with adapters</title>
  203. <programlisting language="php"><![CDATA[
  204. // returns the currently set language
  205. $actual = $translate->getLocale();
  206. // you can use the optional parameter while translating
  207. echo $translate->_("my_text", "fr");
  208. // or set a new language
  209. $translate->setLocale("fr");
  210. echo $translate->_("my_text");
  211. // refer to the base language
  212. // fr_CH will be downgraded to fr
  213. $translate->setLocale("fr_CH");
  214. echo $translate->_("my_text");
  215. // check if this language exist
  216. if ($translate->isAvailable("fr")) {
  217. // language exists
  218. }
  219. ]]></programlisting>
  220. </example>
  221. <sect3 id="zend.translate.additional.languages.automatic">
  222. <title>Automatical handling of languages</title>
  223. <para>
  224. Note that as long as you only add new translation sources with the
  225. <methodname>addTranslation()</methodname> method <classname>Zend_Translate</classname> will
  226. automatically set the best fitting language for your environment when you use one
  227. of the automatic locales which are '<code>auto</code>' or '<code>browser</code>'.
  228. So normally you will not need to call <methodname>setLocale()</methodname>. This should only be
  229. used in conjunction with automatic source detection.
  230. </para>
  231. <para>
  232. The algorithm will search for the best fitting locale depending on the user's
  233. browser and your environment. See the following example for details:
  234. </para>
  235. <example id="zend.translate.additional.languages.automatic.example">
  236. <title>Automatically language detection</title>
  237. <programlisting language="php"><![CDATA[
  238. // Let's expect the browser returns these language settings:
  239. // HTTP_ACCEPT_LANGUAGE = "de_AT=1;fr=1;en_US=0.8";
  240. // Example 1:
  241. // When no fitting language is found, the message ID is returned
  242. $translate = new Zend_Translate(
  243. 'gettext',
  244. 'my_it.mo',
  245. 'auto',
  246. array('scan' => Zend_Translate::LOCALE_FILENAME));
  247. // Example 2:
  248. // Best found fitting language is 'fr'
  249. $translate = new Zend_Translate(
  250. 'gettext',
  251. 'my_fr.mo',
  252. 'auto',
  253. array('scan' => Zend_Translate::LOCALE_FILENAME));
  254. // Example 3:
  255. // Best found fitting language is 'de' ('de_AT' will be degraded)
  256. $translate = new Zend_Translate(
  257. 'gettext',
  258. 'my_de.mo',
  259. 'auto',
  260. array('scan' => Zend_Translate::LOCALE_FILENAME));
  261. // Example 4:
  262. // Returns 'it' as translation source and overrides the automatic settings
  263. $translate = new Zend_Translate(
  264. 'gettext',
  265. 'my_it.mo',
  266. 'auto',
  267. array('scan' => Zend_Translate::LOCALE_FILENAME));
  268. $translate->addTranslation('my_ru.mo', 'ru');
  269. $translate->setLocale('it_IT');
  270. ]]></programlisting>
  271. </example>
  272. <para>
  273. After setting a language manually with the <methodname>setLocale()</methodname> method the
  274. automatic detection will be switched off and overridden.
  275. </para>
  276. <para>
  277. If you want to use it again, you can set the language
  278. <emphasis>auto</emphasis> with <methodname>setLocale()</methodname> which will reactivate
  279. the automatic detection for <classname>Zend_Translate</classname>.
  280. </para>
  281. <para>
  282. Since Zend Framework 1.7.0 <classname>Zend_Translate</classname> also recognises an
  283. application wide locale. You can simply set a <classname>Zend_Locale</classname>
  284. instance to the registry like shown below. With this notation you can forget about
  285. setting the locale manually with each instance when you want to use the same locale
  286. multiple times.
  287. </para>
  288. <programlisting language="php"><![CDATA[
  289. // in your bootstrap file
  290. $locale = new Zend_Locale();
  291. Zend_Registry::set('Zend_Locale', $locale);
  292. // default language when requested language is not available
  293. $defaultlanguage = 'en';
  294. // somewhere in your application
  295. $translate = new Zend_Translate('gettext', 'my_de.mo');
  296. if (!$translate->isAvailable($locale->getLanguage())) {
  297. // not available languages are rerouted to another language
  298. $translate->setLocale($defaultlanguage);
  299. }
  300. $translate->getLocale();
  301. ]]></programlisting>
  302. </sect3>
  303. </sect2>
  304. <sect2 id="zend.translate.additional.detection">
  305. <title>Automatic source detection</title>
  306. <para>
  307. <classname>Zend_Translate</classname> can detect translation sources automatically. So
  308. you don't have to declare each source file manually. You can let
  309. <classname>Zend_Translate</classname> do this job and scan the complete directory
  310. structure for source files.
  311. </para>
  312. <note>
  313. <para>
  314. Automatic source detection is available since Zend Framework version 1.5 .
  315. </para>
  316. </note>
  317. <para>
  318. The usage is quite the same as initiating a single translation source with one
  319. difference. You must give a directory which has to be scanned instead a file.
  320. </para>
  321. <example id="zend.translate.additional.languages.directory.example">
  322. <title>Scanning a directory structure for sources</title>
  323. <programlisting language="php"><![CDATA[
  324. // assuming we have the following structure
  325. // /language/
  326. // /language/login/login.tmx
  327. // /language/logout/logout.tmx
  328. // /language/error/loginerror.tmx
  329. // /language/error/logouterror.tmx
  330. $translate = new Zend_Translate('tmx', '/language');
  331. ]]></programlisting>
  332. </example>
  333. <para>
  334. So <classname>Zend_Translate</classname> does not only search the given directory, but
  335. also all subdirectories for translation source files. This makes the usage quite
  336. simple. But <classname>Zend_Translate</classname> will ignore all files which are not
  337. sources or which produce failures while reading the translation data. So you have to
  338. make sure that all of your translation sources are correct and readable because you
  339. will not get any failure if a file is bogus or can not be read.
  340. </para>
  341. <note>
  342. <para>
  343. Depending on how deep your directory structure is and how much files are within
  344. this structure it can take a long time for <classname>Zend_Translate</classname>
  345. to complete.
  346. </para>
  347. </note>
  348. <para>
  349. In our example we have used the TMX format which includes the language to be used
  350. within the source. But many of the other source formats are not able to include the
  351. language within the file. Even this sources can be used with automatic scanning if you
  352. do some pre-requisits as described below:
  353. </para>
  354. <sect3 id="zend.translate.additional.detection.directory">
  355. <title>Language through naming directories</title>
  356. <para>
  357. One way to include automatic language detection is to name the directories related
  358. to the language which is used for the sources within this directory. This is the
  359. easiest way and is used for example within standard gettext implementations.
  360. </para>
  361. <para>
  362. <classname>Zend_Translate</classname> needs the '<code>scan</code>' option to know
  363. that it should search the names of all directories for languages. See the following
  364. example for details:
  365. </para>
  366. <example id="zend.translate.additional.detection.directory.example">
  367. <title>Directory scanning for languages</title>
  368. <programlisting language="php"><![CDATA[
  369. // assuming we have the following structure
  370. // /language/
  371. // /language/de/login/login.mo
  372. // /language/de/error/loginerror.mo
  373. // /language/en/login/login.mo
  374. // /language/en/error/loginerror.mo
  375. $translate = new Zend_Translate(
  376. 'gettext',
  377. '/language',
  378. null,
  379. array('scan' => Zend_Translate::LOCALE_DIRECTORY));
  380. ]]></programlisting>
  381. </example>
  382. <note>
  383. <para>
  384. This works only for adapters which do not include the language within the
  385. source file. Using this option for example with TMX will be ignored. Also
  386. language definitions within the filename will be ignored when using this
  387. option.
  388. </para>
  389. </note>
  390. <note>
  391. <para>
  392. You should be aware if you have several subdirectories under the same
  393. structure. Assuming we have a structure like
  394. <code>/language/module/de/en/file.mo</code>. In this case the path contains
  395. multiple strings which would be detected as locale. It could be either
  396. <code>de</code> or <code>en</code>. In such a case the behaviour is
  397. undefined and it is recommended to use file detection in such situations.
  398. </para>
  399. </note>
  400. </sect3>
  401. <sect3 id="zend.translate.additional.detection.filename">
  402. <title>Language through filenames</title>
  403. <para>
  404. Another way to detect the language automatically is to use special filenames. You
  405. can either name the complete file or parts of a file after the used language. To
  406. use this way of detection you will have to set the '<code>scan</code>' option at
  407. initiation. There are several ways of naming the sourcefiles which are described
  408. below:
  409. </para>
  410. <example id="zend.translate.additional.detection.filename.example">
  411. <title>Filename scanning for languages</title>
  412. <programlisting language="php"><![CDATA[
  413. // assuming we have the following structure
  414. // /language/
  415. // /language/login/login_en.mo
  416. // /language/login/login_de.mo
  417. // /language/error/loginerror_en.mo
  418. // /language/error/loginerror_de.mo
  419. $translate = new Zend_Translate(
  420. 'gettext',
  421. '/language',
  422. null,
  423. array('scan' => Zend_Translate::LOCALE_FILENAME));
  424. ]]></programlisting>
  425. </example>
  426. <sect4 id="zend.translate.additional.detection.filename.complete">
  427. <title>Complete filename</title>
  428. <para>
  429. Having the whole file named after the language is the simplest way but only
  430. viable if you have only one file per language.
  431. </para>
  432. <programlisting><![CDATA[
  433. /languages/
  434. /languages/en.mo
  435. /languages/de.mo
  436. /languages/es.mo
  437. ]]></programlisting>
  438. </sect4>
  439. <sect4 id="zend.translate.additional.detection.filename.extension">
  440. <title>Extension of the file</title>
  441. <para>
  442. Another simple way to use the extension of the file for language detection.
  443. But this may be confusing since you will no longer have an idea which extension
  444. the file originally had.
  445. </para>
  446. <programlisting><![CDATA[
  447. /languages/
  448. /languages/view.en
  449. /languages/view.de
  450. /languages/view.es
  451. ]]></programlisting>
  452. </sect4>
  453. <sect4 id="zend.translate.additional.detection.filename.token">
  454. <title>Filename tokens</title>
  455. <para>
  456. <classname>Zend_Translate</classname> is also capable of detecting the language
  457. if it is included within the filename. But if you go this way you will have to
  458. separate the language with a token. There are three supported tokens which can
  459. be used: a dot '.', an underscore '_', or a hyphen '-'.
  460. </para>
  461. <programlisting><![CDATA[
  462. /languages/
  463. /languages/view_en.mo -> detects english
  464. /languages/view_de.mo -> detects german
  465. /languages/view_it.mo -> detects italian
  466. ]]></programlisting>
  467. <para>
  468. The first found string delimited by a token which can be interpreted as a
  469. locale will be used. See the following example for details.
  470. </para>
  471. <programlisting><![CDATA[
  472. /languages/
  473. /languages/view_en_de.mo -> detects english
  474. /languages/view_en_es.mo -> detects english and overwrites the first file
  475. /languages/view_it_it.mo -> detects italian
  476. ]]></programlisting>
  477. <para>
  478. All three tokens are used to detect the locale. When the filename contains
  479. multiple tokens, the first found token depends on the order of the tokens
  480. which are used. See the following example for details.
  481. </para>
  482. <programlisting><![CDATA[
  483. /languages/
  484. /languages/view_en-it.mo -> detects english because '_' will be used before '-'
  485. /languages/view-en_it.mo -> detects italian because '_' will be used before '-'
  486. /languages/view_en.it.mo -> detects italian because '.' will be used before '_'
  487. ]]></programlisting>
  488. </sect4>
  489. </sect3>
  490. </sect2>
  491. <sect2 id="zend.translate.additional.istranslated">
  492. <title>Checking for translations</title>
  493. <para>
  494. Normally text will be translated without any computation. But sometimes it is necessary
  495. to know if a text is translated or not, therefor the <methodname>isTranslated()</methodname>
  496. method can be used.
  497. </para>
  498. <para>
  499. <methodname>isTranslated($messageId, $original = false, $locale = null)</methodname> takes
  500. the text you want to check as its first parameter, and as optional third parameter the
  501. locale for which you want to do the check. The optional second parameter declares
  502. whether translation is fixed to the declared language or a lower set of translations
  503. can be used. If you have a text which can be returned for 'en' but not for 'en_US' you
  504. will normally get the translation returned, but by setting <varname>$original</varname> to
  505. true, <methodname>isTranslated()</methodname> will return false.
  506. </para>
  507. <example id="zend.translate.additional.istranslated.example">
  508. <title>Checking if a text is translatable</title>
  509. <programlisting language="php"><![CDATA[
  510. $english = array(
  511. 'message1' => 'Nachricht 1',
  512. 'message2' => 'Nachricht 2',
  513. 'message3' => 'Nachricht 3');
  514. $translate = new Zend_Translate('array', $english, 'de_AT');
  515. if ($translate->isTranslated('message1')) {
  516. print "'message1' can be translated";
  517. }
  518. if (!($translate->isTranslated('message1', true, 'de'))) {
  519. print "'message1' can not be translated to 'de'"
  520. . " as it's available only in 'de_AT'";
  521. }
  522. if ($translate->isTranslated('message1', false, 'de')) {
  523. print "'message1' can be translated in 'de_AT' as it falls back to 'de'";
  524. }
  525. ]]></programlisting>
  526. </example>
  527. </sect2>
  528. <sect2 id="zend.translate.additional.logging">
  529. <title>How to log not found translations</title>
  530. <para>
  531. When you have a bigger site or you are creating the translation files manually, you
  532. often have the problem that some messages are not translated. But there is an easy
  533. solution for you when you are using <classname>Zend_Translate</classname>.
  534. </para>
  535. <para>
  536. You have to follow two or three simple steps. First, you have to create an instance of
  537. <classname>Zend_Log</classname>. Then you have to attach this instance to
  538. <classname>Zend_Translate</classname>. See the following example:
  539. </para>
  540. <example id="zend.translate.additional.logging.example">
  541. <title>Log translations</title>
  542. <programlisting language="php"><![CDATA[
  543. $translate = new Zend_Translate('gettext', $path, 'de');
  544. // Create a log instance
  545. $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
  546. $log = new Zend_Log($writer);
  547. // Attach it to the translation instance
  548. $translate->setOptions(array(
  549. 'log' => $log,
  550. 'logUntranslated' => true));
  551. $translate->translate('unknown string');
  552. ]]></programlisting>
  553. </example>
  554. <para>
  555. Now you will have a new notice in the log: <code>Untranslated message within 'de':
  556. unknown string</code>.
  557. </para>
  558. <note>
  559. <para>
  560. You should note that any translation which can not be found will be logged. This
  561. means all translations when a user requests a language which is not supported. Also
  562. every request for a message which can not be translated will be logged. Be aware,
  563. that 100 people requesting the same translation, will result 100 logged notices.
  564. </para>
  565. </note>
  566. <para>
  567. This feature can not only be used to log messages but also to attach this untranslated
  568. messages into an empty translation file. To do so you will have to write your own log
  569. writer which writes the format you want to have and strips the prepending "Untranslated
  570. message".
  571. </para>
  572. <para>
  573. You can also set the '<code>logMessage</code>' option when you want to have your own
  574. log message. Use the '<code>%message%</code>' token for placing the messageId within
  575. your log message, and the '<code>%locale%</code>' token for the requested locale. See
  576. the following example for a self defined log message:
  577. </para>
  578. <example id="zend.translate.additional.logging.example2">
  579. <title>Self defined log messages</title>
  580. <programlisting language="php"><![CDATA[
  581. $translate = new Zend_Translate('gettext', $path, 'de');
  582. // Create a log instance
  583. $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
  584. $log = new Zend_Log($writer);
  585. // Attach it to the translation instance
  586. $translate->setOptions(array(
  587. 'log' => $log,
  588. 'logMessage' => "Missing '%message%' within locale '%locale%'",
  589. 'logUntranslated' => true));
  590. $translate->translate('unknown string');
  591. ]]></programlisting>
  592. </example>
  593. </sect2>
  594. <sect2 id="zend.translate.additional.sourcedata">
  595. <title>Accessing source data</title>
  596. <para>
  597. Sometimes it is useful to have access to the translation source data. Therefor
  598. the following two functions are provided.
  599. </para>
  600. <para>
  601. The <methodname>getMessageIds($locale = null)</methodname> method returns all known message IDs as
  602. array.
  603. </para>
  604. <para>
  605. The <methodname>getMessages($locale = null)</methodname> method returns the complete translation
  606. source as an array. The message ID is used as key and the translation data as value.
  607. </para>
  608. <para>
  609. Both methods accept an optional parameter <varname>$locale</varname> which, if set, returns
  610. the translation data for the specified language. If this parameter is not given, the
  611. actual set language will be used. Keep in mind that normally all translations should be
  612. available in all languages. Which means that in a normal situation you will not have to
  613. set this parameter.
  614. </para>
  615. <para>
  616. Additionally the <methodname>getMessages()</methodname> method can be used to return the complete
  617. translation dictionary using the pseudo-locale 'all'. This will return all available
  618. translation data for each added locale.
  619. </para>
  620. <note>
  621. <para>
  622. Attention: the returned array can be <emphasis>very big</emphasis>,
  623. depending on the number of added locales and the amount of translation data.
  624. </para>
  625. </note>
  626. <example id="zend.translate.additional.sourcedata.example">
  627. <title>Handling languages with adapters</title>
  628. <programlisting language="php"><![CDATA[
  629. // returns all known message IDs
  630. $messageIds = $translate->getMessageIds();
  631. print_r($messageIds);
  632. // or just for the specified language
  633. $messageIds = $translate->getMessageIds('en_US');
  634. print_r($messageIds);
  635. // returns all the complete translation data
  636. $source = $translate->getMessages();
  637. print_r($source);
  638. ]]></programlisting>
  639. </example>
  640. </sect2>
  641. </sect1>
  642. <!--
  643. vim:se ts=4 sw=4 et:
  644. -->