Zend_Translate-Additional.xml 32 KB

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