Zend_Translate-Additional.xml 32 KB

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