Zend_Translate-Additional.xml 32 KB

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