Zend_Translate-Additional.xml 31 KB

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