Zend_Translate-Additional.xml 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 by giving them to
  21. <methodname>addTranslation()</methodname>. And you can use the method
  22. <methodname>setOptions()</methodname> to set options permanent.
  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. $translate = new Zend_Translate(
  29. array(
  30. 'adapter' => 'csv',
  31. 'content' => '/path/to/mytranslation.csv',
  32. 'locale' => 'de',
  33. 'delimiter' => ':'
  34. )
  35. );
  36. ...
  37. // clear the defined language and use new translation data
  38. $translate->addTranslation(
  39. array(
  40. 'content' => '/path/to/new.csv',
  41. 'locale' => 'fr',
  42. 'clear' => true
  43. )
  44. );
  45. ]]></programlisting>
  46. </example>
  47. <para>
  48. Here you can find all available options for the different adapters with a description
  49. of their usage:
  50. </para>
  51. <table id="zend.translate.additional.options.alloptions">
  52. <title>Options for translation adapters</title>
  53. <tgroup cols="4">
  54. <thead>
  55. <row>
  56. <entry>Option</entry>
  57. <entry>Adapter</entry>
  58. <entry>Description</entry>
  59. <entry>Default value</entry>
  60. </row>
  61. </thead>
  62. <tbody>
  63. <row>
  64. <entry>adapter</entry>
  65. <entry>Zend_Translate only</entry>
  66. <entry>
  67. Defines the adapter which will be used for the translation. This option
  68. can only be given when a new instance of
  69. <classname>Zend_Translate</classname> is created. When it is set
  70. afterwards, then it will be ignored
  71. </entry>
  72. <entry><emphasis>Must be set as it has no default value</emphasis></entry>
  73. </row>
  74. <row>
  75. <entry>clear</entry>
  76. <entry>all</entry>
  77. <entry>
  78. If set to <constant>TRUE</constant>, the already read translations will
  79. be cleared. This can be used instead of creating a new instance when
  80. reading new translation data
  81. </entry>
  82. <entry><emphasis><constant>FALSE</constant></emphasis></entry>
  83. </row>
  84. <row>
  85. <entry>content</entry>
  86. <entry>all</entry>
  87. <entry>
  88. Sets the content for the translation adapter. This could be an array,
  89. a filename or a directory. Which type of content is supported depends
  90. on the used adapter
  91. </entry>
  92. <entry>
  93. <emphasis>The default value depends on the used adapter</emphasis>
  94. </entry>
  95. </row>
  96. <row>
  97. <entry>disableNotices</entry>
  98. <entry>all</entry>
  99. <entry>
  100. If set to <constant>TRUE</constant>, all notices regarding not available
  101. translations will be disabled. You should set this option to
  102. <constant>TRUE</constant> in production environment
  103. </entry>
  104. <entry><emphasis><constant>FALSE</constant></emphasis></entry>
  105. </row>
  106. <row>
  107. <entry>ignore</entry>
  108. <entry>all</entry>
  109. <entry>
  110. All directories and files beginning with this prefix will be ignored
  111. when searching for files. This value defaults to
  112. <emphasis>'.'</emphasis> which leads to the behavior that all hidden
  113. files will be ignored. Setting this value to <code>'tmp'</code> would
  114. mean that directories and files like <code>'tmpImages'</code> and
  115. <code>'tmpFiles'</code> would be ignored as well as all subsequent
  116. directories. This option also accepts an array which can be used when
  117. you want to ignore more than one prefix.
  118. </entry>
  119. <entry><emphasis>.</emphasis></entry>
  120. </row>
  121. <row>
  122. <entry>log</entry>
  123. <entry>all</entry>
  124. <entry>
  125. An instance of <classname>Zend_Log</classname> where untranslated
  126. messages and notices will be written to
  127. </entry>
  128. <entry><emphasis><constant>NULL</constant></emphasis></entry>
  129. </row>
  130. <row>
  131. <entry>logMessage</entry>
  132. <entry>all</entry>
  133. <entry>The message which will be written into the log</entry>
  134. <entry>
  135. <emphasis>Untranslated message within '%locale%': %message%</emphasis>
  136. </entry>
  137. </row>
  138. <row>
  139. <entry>logUntranslated</entry>
  140. <entry>all</entry>
  141. <entry>
  142. When this option is set to <constant>TRUE</constant>, all message IDs
  143. which can not be translated will be written into the attached log
  144. </entry>
  145. <entry><emphasis><constant>FALSE</constant></emphasis></entry>
  146. </row>
  147. <row>
  148. <entry>reload</entry>
  149. <entry>all</entry>
  150. <entry>
  151. When this option is set to <constant>TRUE</constant>, then files are
  152. reloaded into the cache. This option can be used to recreate the cache,
  153. or to add translations to already cached data after the cache has
  154. already been created.
  155. </entry>
  156. <entry><emphasis><constant>FALSE</constant></emphasis></entry>
  157. </row>
  158. <row>
  159. <entry>scan</entry>
  160. <entry>all</entry>
  161. <entry>
  162. If set to <constant>NULL</constant>, no scanning of the directory
  163. structure will be done. If set to
  164. <constant>Zend_Translate::LOCALE_DIRECTORY</constant> the
  165. locale will be detected within the directory. If set to
  166. <constant>Zend_Translate::LOCALE_FILENAME</constant> the locale will
  167. be detected within the filename. See <xref
  168. linkend="zend.translate.additional.detection" /> for details
  169. </entry>
  170. <entry><emphasis><constant>NULL</constant></emphasis></entry>
  171. </row>
  172. <row>
  173. <entry>delimiter</entry>
  174. <entry>Csv</entry>
  175. <entry>
  176. Defines which sign is used as delimiter for separating source and
  177. translation
  178. </entry>
  179. <entry><emphasis>;</emphasis></entry>
  180. </row>
  181. <row>
  182. <entry>enclosure</entry>
  183. <entry>Csv</entry>
  184. <entry>
  185. Defines the enclosure character to be used. Defaults to a doublequote
  186. </entry>
  187. <entry><emphasis>"</emphasis></entry>
  188. </row>
  189. <row>
  190. <entry>length</entry>
  191. <entry>Csv</entry>
  192. <entry>
  193. Defines the maximum length of a csv line. When set to 0 it will be
  194. detected automatically
  195. </entry>
  196. <entry><emphasis>0</emphasis></entry>
  197. </row>
  198. <row>
  199. <entry>useId</entry>
  200. <entry>Xliff and Tmx</entry>
  201. <entry>
  202. If you set this option to <constant>FALSE</constant>, then the source
  203. string will be used as message Id. The default for this option is
  204. <constant>TRUE</constant>, which means that the Id from the trans-unit
  205. element will be used as message Id
  206. </entry>
  207. <entry><emphasis><constant>TRUE</constant></emphasis></entry>
  208. </row>
  209. </tbody>
  210. </tgroup>
  211. </table>
  212. <para>
  213. When you want to have self defined options, you are also able to use them within all
  214. adapters. The <methodname>setOptions()</methodname> method can be used to define your
  215. option. <methodname>setOptions()</methodname> needs an array with the options you want
  216. to set. If an given option exists it will be signed over. You can define as much options
  217. as needed as they will not be checked by the adapter. Just make sure not to overwrite
  218. any existing option which is used by an adapter.
  219. </para>
  220. <para>
  221. To return the option you can use the <methodname>getOptions()</methodname> method. When
  222. <methodname>getOptions()</methodname> is called without a parameter it will return all
  223. options set. When the optional parameter is given you will only get the specified
  224. option.
  225. </para>
  226. </sect2>
  227. <sect2 id="zend.translate.additional.languages">
  228. <title>Handling languages</title>
  229. <para>
  230. When working with different languages there are a few methods which will be useful.
  231. </para>
  232. <para>
  233. The <methodname>getLocale()</methodname> method can be used to get the currently set
  234. language. It can either hold an instance of <classname>Zend_Locale</classname> or the
  235. identifier of a locale.
  236. </para>
  237. <para>
  238. The <methodname>setLocale()</methodname> method sets a new standard language for
  239. translation. This prevents the need of setting the optional language parameter more than
  240. once to the <methodname>translate()</methodname> method. If the given language does not
  241. exist, or no translation data is available for the language,
  242. <methodname>setLocale()</methodname> tries to downgrade to the language without the
  243. region if any was given. A language of <code>en_US</code> would be downgraded to
  244. <code>en</code>. When even the downgraded language can not be found an exception will be
  245. thrown.
  246. </para>
  247. <para>
  248. The <methodname>isAvailable()</methodname> method checks if a given language is already
  249. available. It returns <constant>TRUE</constant> if data for the given language exist.
  250. </para>
  251. <para>
  252. And finally the <methodname>getList()</methodname> method can be used to get all
  253. currently set languages for an adapter returned as array.
  254. </para>
  255. <example id="zend.translate.additional.languages.example">
  256. <title>Handling languages with adapters</title>
  257. <programlisting language="php"><![CDATA[
  258. // returns the currently set language
  259. $actual = $translate->getLocale();
  260. // you can use the optional parameter while translating
  261. echo $translate->_("my_text", "fr");
  262. // or set a new language
  263. $translate->setLocale("fr");
  264. echo $translate->_("my_text");
  265. // refer to the base language
  266. // fr_CH will be downgraded to fr
  267. $translate->setLocale("fr_CH");
  268. echo $translate->_("my_text");
  269. // check if this language exist
  270. if ($translate->isAvailable("fr")) {
  271. // language exists
  272. }
  273. ]]></programlisting>
  274. </example>
  275. <sect3 id="zend.translate.additional.languages.automatic">
  276. <title>Automatical handling of languages</title>
  277. <para>
  278. Note that as long as you only add new translation sources with the
  279. <methodname>addTranslation()</methodname> method
  280. <classname>Zend_Translate</classname> will automatically set the best fitting
  281. language for your environment when you use one of the automatic locales which are
  282. '<code>auto</code>' or '<code>browser</code>'. So normally you will not need to call
  283. <methodname>setLocale()</methodname>. This should only be used in conjunction with
  284. automatic source detection.
  285. </para>
  286. <para>
  287. The algorithm will search for the best fitting locale depending on the user's
  288. browser and your environment. See the following example for details:
  289. </para>
  290. <example id="zend.translate.additional.languages.automatic.example">
  291. <title>Automatically language detection</title>
  292. <programlisting language="php"><![CDATA[
  293. // Let's expect the browser returns these language settings:
  294. // HTTP_ACCEPT_LANGUAGE = "de_AT=1;fr=1;en_US=0.8";
  295. // Example 1:
  296. // When no fitting language is found, the message ID is returned
  297. $translate = new Zend_Translate(
  298. array(
  299. 'adapter' => 'gettext',
  300. 'content' => 'my_it.mo',
  301. 'locale' => 'auto',
  302. 'scan' => Zend_Translate::LOCALE_FILENAME
  303. )
  304. );
  305. // Example 2:
  306. // Best found fitting language is 'fr'
  307. $translate = new Zend_Translate(
  308. array(
  309. 'adapter' => 'gettext',
  310. 'content' => 'my_fr.mo',
  311. 'locale' => 'auto',
  312. 'scan' => Zend_Translate::LOCALE_FILENAME
  313. )
  314. );
  315. // Example 3:
  316. // Best found fitting language is 'de' ('de_AT' will be degraded)
  317. $translate = new Zend_Translate(
  318. array(
  319. 'adapter' => 'gettext',
  320. 'content' => 'my_de.mo',
  321. 'locale' => 'auto',
  322. 'scan' => Zend_Translate::LOCALE_FILENAME
  323. )
  324. );
  325. // Example 4:
  326. // Returns 'it' as translation source and overrides the automatic settings
  327. $translate = new Zend_Translate(
  328. array(
  329. 'adapter' => 'gettext',
  330. 'content' => 'my_it.mo',
  331. 'locale' => 'auto',
  332. 'scan' => Zend_Translate::LOCALE_FILENAME
  333. )
  334. );
  335. $translate->addTranslation(array('content' => 'my_ru.mo', 'locale' => 'ru'));
  336. $translate->setLocale('it_IT');
  337. ]]></programlisting>
  338. </example>
  339. <para>
  340. After setting a language manually with the <methodname>setLocale()</methodname>
  341. method the automatic detection will be switched off and overridden.
  342. </para>
  343. <para>
  344. If you want to use it again, you can set the language
  345. <emphasis>auto</emphasis> with <methodname>setLocale()</methodname> which will
  346. reactivate the automatic detection for <classname>Zend_Translate</classname>.
  347. </para>
  348. <para>
  349. Since Zend Framework 1.7.0 <classname>Zend_Translate</classname> also recognises an
  350. application wide locale. You can simply set a <classname>Zend_Locale</classname>
  351. instance to the registry like shown below. With this notation you can forget about
  352. setting the locale manually with each instance when you want to use the same locale
  353. multiple times.
  354. </para>
  355. <programlisting language="php"><![CDATA[
  356. // in your bootstrap file
  357. $locale = new Zend_Locale();
  358. Zend_Registry::set('Zend_Locale', $locale);
  359. // default language when requested language is not available
  360. $defaultlanguage = 'en';
  361. // somewhere in your application
  362. $translate = new Zend_Translate(
  363. array('adapter' => 'gettext', 'content' => 'my_de.mo')
  364. );
  365. if (!$translate->isAvailable($locale->getLanguage())) {
  366. // not available languages are rerouted to another language
  367. $translate->setLocale($defaultlanguage);
  368. }
  369. $translate->getLocale();
  370. ]]></programlisting>
  371. </sect3>
  372. <sect3 id="zend.translate.additional.languages.territory">
  373. <title>Using a country as language</title>
  374. <para>
  375. You can also use a country as locale parameter. This could be useful when you
  376. provide your user with flags, which represent the country in which he lives, and
  377. when he selects his flag, he would automatically get the default language for this
  378. country.
  379. </para>
  380. <para>
  381. For example, when the user selected <emphasis>US</emphasis> then you would get
  382. <emphasis>en_US</emphasis> in return as locale which is being used. This leads
  383. automatically to the language <emphasis>en</emphasis> which is the default language
  384. for the country <emphasis>US</emphasis>.
  385. </para>
  386. <programlisting language="php"><![CDATA[
  387. $translate = new Zend_Translate(
  388. array(
  389. 'adapter' => 'gettext',
  390. 'content' => 'my_de.mo',
  391. 'locale' => 'US'
  392. )
  393. );
  394. ]]></programlisting>
  395. <note>
  396. <title>Always uppercase countries</title>
  397. <para>
  398. Using this syntax you should always uppercase the input when you know that it's
  399. a country. The reason is that there are also languages which have the same
  400. letters as a country. Take for example <emphasis>om</emphasis>. You could expect
  401. to get <emphasis>ar_OM</emphasis> when you mean the country "Oman", or you could
  402. expect the language "Oromo" which is spoken in Kenia for example.
  403. </para>
  404. <para>
  405. As <classname>Zend_Translate</classname> is related to languages it will always
  406. use the language in this case. Therefor always uppercase the locale when you
  407. want it to be recognised as country.
  408. </para>
  409. </note>
  410. </sect3>
  411. </sect2>
  412. <sect2 id="zend.translate.additional.detection">
  413. <title>Automatic source detection</title>
  414. <para>
  415. <classname>Zend_Translate</classname> can detect translation sources automatically. So
  416. you don't have to declare each source file manually. You can let
  417. <classname>Zend_Translate</classname> do this job and scan the complete directory
  418. structure for source files.
  419. </para>
  420. <note>
  421. <para>
  422. Automatic source detection is available since Zend Framework version 1.5 .
  423. </para>
  424. </note>
  425. <para>
  426. The usage is quite the same as initiating a single translation source with one
  427. difference. You must give a directory which has to be scanned instead a file.
  428. </para>
  429. <example id="zend.translate.additional.languages.directory.example">
  430. <title>Scanning a directory structure for sources</title>
  431. <programlisting language="php"><![CDATA[
  432. // assuming we have the following structure
  433. // /language/
  434. // /language/login/login.tmx
  435. // /language/logout/logout.tmx
  436. // /language/error/loginerror.tmx
  437. // /language/error/logouterror.tmx
  438. $translate = new Zend_Translate(
  439. array('adapter' => 'tmx', 'content' => '/language')
  440. );
  441. ]]></programlisting>
  442. </example>
  443. <para>
  444. So <classname>Zend_Translate</classname> does not only search the given directory, but
  445. also all subdirectories for translation source files. This makes the usage quite
  446. simple. But <classname>Zend_Translate</classname> will ignore all files which are not
  447. sources or which produce failures while reading the translation data. So you have to
  448. make sure that all of your translation sources are correct and readable because you
  449. will not get any failure if a file is bogus or can not be read.
  450. </para>
  451. <note>
  452. <para>
  453. Depending on how deep your directory structure is and how much files are within
  454. this structure it can take a long time for <classname>Zend_Translate</classname>
  455. to complete.
  456. </para>
  457. </note>
  458. <para>
  459. In our example we have used the TMX format which includes the language to be used
  460. within the source. But many of the other source formats are not able to include the
  461. language within the file. Even this sources can be used with automatic scanning if you
  462. do some pre-requisits as described below:
  463. </para>
  464. <sect3 id="zend.translate.additional.detection.directory">
  465. <title>Language through naming directories</title>
  466. <para>
  467. One way to include automatic language detection is to name the directories related
  468. to the language which is used for the sources within this directory. This is the
  469. easiest way and is used for example within standard gettext implementations.
  470. </para>
  471. <para>
  472. <classname>Zend_Translate</classname> needs the '<code>scan</code>' option to know
  473. that it should search the names of all directories for languages. See the following
  474. example for details:
  475. </para>
  476. <example id="zend.translate.additional.detection.directory.example">
  477. <title>Directory scanning for languages</title>
  478. <programlisting language="php"><![CDATA[
  479. // assuming we have the following structure
  480. // /language/
  481. // /language/de/login/login.mo
  482. // /language/de/error/loginerror.mo
  483. // /language/en/login/login.mo
  484. // /language/en/error/loginerror.mo
  485. $translate = new Zend_Translate(
  486. array(
  487. 'adapter' => 'gettext',
  488. 'content' => '/language',
  489. 'scan' => Zend_Translate::LOCALE_DIRECTORY
  490. )
  491. );
  492. ]]></programlisting>
  493. </example>
  494. <note>
  495. <para>
  496. This works only for adapters which do not include the language within the
  497. source file. Using this option for example with TMX will be ignored. Also
  498. language definitions within the filename will be ignored when using this
  499. option.
  500. </para>
  501. </note>
  502. <note>
  503. <para>
  504. You should be aware if you have several subdirectories under the same
  505. structure. Assuming we have a structure like
  506. <code>/language/module/de/en/file.mo</code>. In this case the path contains
  507. multiple strings which would be detected as locale. It could be either
  508. <code>de</code> or <code>en</code>. In such a case the behaviour is
  509. undefined and it is recommended to use file detection in such situations.
  510. </para>
  511. </note>
  512. </sect3>
  513. <sect3 id="zend.translate.additional.detection.filename">
  514. <title>Language through filenames</title>
  515. <para>
  516. Another way to detect the language automatically is to use special filenames. You
  517. can either name the complete file or parts of a file after the used language. To
  518. use this way of detection you will have to set the '<code>scan</code>' option at
  519. initiation. There are several ways of naming the sourcefiles which are described
  520. below:
  521. </para>
  522. <example id="zend.translate.additional.detection.filename.example">
  523. <title>Filename scanning for languages</title>
  524. <programlisting language="php"><![CDATA[
  525. // assuming we have the following structure
  526. // /language/
  527. // /language/login/login_en.mo
  528. // /language/login/login_de.mo
  529. // /language/error/loginerror_en.mo
  530. // /language/error/loginerror_de.mo
  531. $translate = new Zend_Translate(
  532. array(
  533. 'adapter' => 'gettext',
  534. 'content' => '/language',
  535. 'scan' => Zend_Translate::LOCALE_FILENAME
  536. )
  537. );
  538. ]]></programlisting>
  539. </example>
  540. <sect4 id="zend.translate.additional.detection.filename.complete">
  541. <title>Complete filename</title>
  542. <para>
  543. Having the whole file named after the language is the simplest way but only
  544. viable if you have only one file per language.
  545. </para>
  546. <programlisting><![CDATA[
  547. /languages/
  548. /languages/en.mo
  549. /languages/de.mo
  550. /languages/es.mo
  551. ]]></programlisting>
  552. </sect4>
  553. <sect4 id="zend.translate.additional.detection.filename.extension">
  554. <title>Extension of the file</title>
  555. <para>
  556. Another simple way to use the extension of the file for language detection.
  557. But this may be confusing since you will no longer have an idea which extension
  558. the file originally had.
  559. </para>
  560. <programlisting><![CDATA[
  561. /languages/
  562. /languages/view.en
  563. /languages/view.de
  564. /languages/view.es
  565. ]]></programlisting>
  566. </sect4>
  567. <sect4 id="zend.translate.additional.detection.filename.token">
  568. <title>Filename tokens</title>
  569. <para>
  570. <classname>Zend_Translate</classname> is also capable of detecting the language
  571. if it is included within the filename. But if you go this way you will have to
  572. separate the language with a token. There are three supported tokens which can
  573. be used: a dot '.', an underscore '_', or a hyphen '-'.
  574. </para>
  575. <programlisting><![CDATA[
  576. /languages/
  577. /languages/view_en.mo -> detects english
  578. /languages/view_de.mo -> detects german
  579. /languages/view_it.mo -> detects italian
  580. ]]></programlisting>
  581. <para>
  582. The first found string delimited by a token which can be interpreted as a
  583. locale will be used. See the following example for details.
  584. </para>
  585. <programlisting><![CDATA[
  586. /languages/
  587. /languages/view_en_de.mo -> detects english
  588. /languages/view_en_es.mo -> detects english and overwrites the first file
  589. /languages/view_it_it.mo -> detects italian
  590. ]]></programlisting>
  591. <para>
  592. All three tokens are used to detect the locale. When the filename contains
  593. multiple tokens, the first found token depends on the order of the tokens
  594. which are used. See the following example for details.
  595. </para>
  596. <programlisting><![CDATA[
  597. /languages/
  598. /languages/view_en-it.mo -> detects english because '_' will be used before '-'
  599. /languages/view-en_it.mo -> detects italian because '_' will be used before '-'
  600. /languages/view_en.it.mo -> detects italian because '.' will be used before '_'
  601. ]]></programlisting>
  602. </sect4>
  603. </sect3>
  604. <sect3 id="zend.translate.additional.detection.ignore">
  605. <title>Ignoring special files and directories</title>
  606. <para>
  607. Sometimes it is useful to exclude files or even directories from being added
  608. automatically. Therefor you can use the <property>ignore</property> option which
  609. accepts 3 possible usages.
  610. </para>
  611. <sect4 id="zend.translate.additional.detection.ignore.string">
  612. <title>Ignore a special directory or file</title>
  613. <para>
  614. Per default <classname>Zend_Translate</classname> is set to ignore all
  615. files and directories beginning with <emphasis>'/.'</emphasis>. This means that
  616. all <acronym>SVN</acronym> files will be ignored.
  617. </para>
  618. <para>
  619. You can set your own syntax by giving a string for the
  620. <property>ignore</property> option. The directory separator will be attached
  621. automatically and has to be omitted.
  622. </para>
  623. <programlisting><![CDATA[
  624. $options = array('ignore' => 'test');
  625. $translate = new Zend_Translate(
  626. array(
  627. 'adapter' => $adapter,
  628. 'content' => $content,
  629. 'locale' => $locale,
  630. 'ignore' => 'test'
  631. )
  632. );
  633. ]]></programlisting>
  634. <para>
  635. The above example will ignore all files and directories beginning with
  636. <emphasis>test</emphasis>. This means for example
  637. <filename>/test/en.mo</filename>, <filename>/testing/en.mo</filename> and
  638. <filename>/dir/test_en.mo</filename>. But it would still add
  639. <filename>/mytest/en.mo</filename> or <filename>/dir/atest.mo</filename>.
  640. </para>
  641. <note>
  642. <title>Prevent SVN files from being searched</title>
  643. <para>
  644. When you set this option, then the default <emphasis>'/.'</emphasis> will
  645. be erased. This means that <classname>Zend_Translate</classname> will then
  646. add all files from the hidden <acronym>SVN</acronym> directories. When you
  647. are working with <acronym>SVN</acronym>, then you should use the array
  648. syntax described in the next section.
  649. </para>
  650. </note>
  651. </sect4>
  652. <sect4 id="zend.translate.additional.detection.ignore.array.files">
  653. <title>Ignore several directories or files</title>
  654. <para>
  655. You can also ignore several files and directories. Instead of a string,
  656. you can simply give an array with all wished names which will be ignored.
  657. </para>
  658. <programlisting><![CDATA[
  659. $options = array('ignore' => array('.', 'test', 'old'));
  660. $translate = new Zend_Translate(
  661. array(
  662. 'adapter' => $adapter,
  663. 'content' => $content,
  664. 'locale' => $locale,
  665. 'ignore' => array('.', 'test', 'old')
  666. )
  667. );
  668. ]]></programlisting>
  669. <para>
  670. In the above case all 3 syntax will be ignored. But still they have to
  671. begin with the syntax to be detected and ignored.
  672. </para>
  673. </sect4>
  674. <sect4 id="zend.translate.additional.detection.ignore.array.names">
  675. <title>Ignore specific names</title>
  676. <para>
  677. To ignore files and directories which are not beginning with a defined syntax
  678. but have a special syntax anywhere within their name you can use a regular
  679. expression.
  680. </para>
  681. <para>
  682. To use a regular expression the array key of the <property>ignore</property>
  683. option has to begin with <emphasis>regex</emphasis>.
  684. </para>
  685. <programlisting><![CDATA[
  686. $options = array('ignore' => array('regex' => '/test/u', 'regex_2' => '/deleted$/u'));
  687. $translate = new Zend_Translate(
  688. array(
  689. 'adapter' => $adapter,
  690. 'content' => $content,
  691. 'locale' => $locale,
  692. 'ignore' => array('regex' => '/test/u', 'regex_2' => '/deleted$/u')
  693. )
  694. );
  695. ]]></programlisting>
  696. <para>
  697. In the above case we defined 2 regular expressions. The files and directories
  698. will always being searched with all given regular expressions. In our example
  699. this means that any files which contains <emphasis>test</emphasis> anywhere in
  700. their name will be ignored. Additionally all files and directories which end
  701. with <emphasis>deleted</emphasis> will not be added as translation.
  702. </para>
  703. </sect4>
  704. </sect3>
  705. </sect2>
  706. <sect2 id="zend.translate.additional.combination">
  707. <title>Combining multiple translation sources</title>
  708. <para>
  709. When you are working with multiple translations you may come into a situation where you
  710. want to use different source types. For example the resource files which are provided
  711. by the framework and your own translations which are available by using the gettext
  712. adapter.
  713. </para>
  714. <para>
  715. By combining multiple translation adapters you can use them within one instance. See
  716. the following example:
  717. </para>
  718. <programlisting><![CDATA[
  719. $translate = new Zend_Translate(
  720. array(
  721. 'adapter' => 'gettext',
  722. 'content' => '\path\to\translation.mo',
  723. 'locale' => 'en'
  724. )
  725. );
  726. $translate_second = new Zend_Translate(
  727. array(
  728. 'adapter' => 'array',
  729. 'content' => '\resources\languages\en\Zend_Validate.php',
  730. 'locale' => 'en'
  731. )
  732. );
  733. $translate->addTranslation(array('content' => $translate_second));
  734. ]]></programlisting>
  735. <para>
  736. Now the first instance holds all translations from the second instance and you can use
  737. it within the application even if you used different source types.
  738. </para>
  739. <note>
  740. <title>Memory savings</title>
  741. <para>
  742. As you may have noted the second instance is no longer used as soon as it has been
  743. added to the first instance. To save some memory you may want to unset it.
  744. </para>
  745. </note>
  746. <para>
  747. When you are scanning for directories you may additionally want to use only one defined
  748. language. The predefined resources for example are available in more than 10 languages.
  749. But your application is not available in all of those language. Therefor you can also
  750. add only one language from the second adapter.
  751. </para>
  752. <programlisting><![CDATA[
  753. $translate->addTranslation(
  754. array(
  755. 'content' => $translate_second,
  756. 'locale' => 'en'
  757. )
  758. );
  759. ]]></programlisting>
  760. <para>
  761. This allows you still to scan through the directories and still add only those languages
  762. which are relevant for your application.
  763. </para>
  764. </sect2>
  765. <sect2 id="zend.translate.additional.istranslated">
  766. <title>Checking for translations</title>
  767. <para>
  768. Normally text will be translated without any computation. But sometimes it is necessary
  769. to know if a text is translated or not, therefor the
  770. <methodname>isTranslated()</methodname> method can be used.
  771. </para>
  772. <para>
  773. <methodname>isTranslated($messageId, $original = false, $locale = null)</methodname>
  774. takes the text you want to check as its first parameter, and as optional third parameter
  775. the locale for which you want to do the check. The optional second parameter declares
  776. whether translation is fixed to the declared language or a lower set of translations
  777. can be used. If you have a text which can be returned for 'en' but not for 'en_US' you
  778. will normally get the translation returned, but by setting <varname>$original</varname>
  779. to <constant>TRUE</constant>, <methodname>isTranslated()</methodname> will return
  780. <constant>FALSE</constant>.
  781. </para>
  782. <example id="zend.translate.additional.istranslated.example">
  783. <title>Checking if a text is translatable</title>
  784. <programlisting language="php"><![CDATA[
  785. $english = array(
  786. 'message1' => 'Nachricht 1',
  787. 'message2' => 'Nachricht 2',
  788. 'message3' => 'Nachricht 3');
  789. $translate = new Zend_Translate(
  790. array(
  791. 'adapter' => 'array',
  792. 'content' => $english,
  793. 'locale' => 'de_AT'
  794. )
  795. );
  796. if ($translate->isTranslated('message1')) {
  797. print "'message1' can be translated";
  798. }
  799. if (!($translate->isTranslated('message1', true, 'de'))) {
  800. print "'message1' can not be translated to 'de'"
  801. . " as it's available only in 'de_AT'";
  802. }
  803. if ($translate->isTranslated('message1', false, 'de')) {
  804. print "'message1' can be translated in 'de_AT' as it falls back to 'de'";
  805. }
  806. ]]></programlisting>
  807. </example>
  808. </sect2>
  809. <sect2 id="zend.translate.additional.logging">
  810. <title>How to log not found translations</title>
  811. <para>
  812. When you have a bigger site or you are creating the translation files manually, you
  813. often have the problem that some messages are not translated. But there is an easy
  814. solution for you when you are using <classname>Zend_Translate</classname>.
  815. </para>
  816. <para>
  817. You have to follow two or three simple steps. First, you have to create an instance of
  818. <classname>Zend_Log</classname>. Then you have to attach this instance to
  819. <classname>Zend_Translate</classname>. See the following example:
  820. </para>
  821. <example id="zend.translate.additional.logging.example">
  822. <title>Log translations</title>
  823. <programlisting language="php"><![CDATA[
  824. $translate = new Zend_Translate(
  825. array(
  826. 'adapter' => 'gettext',
  827. 'content' => $path,
  828. 'locale' => 'de'
  829. )
  830. );
  831. // Create a log instance
  832. $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
  833. $log = new Zend_Log($writer);
  834. // Attach it to the translation instance
  835. $translate->setOptions(
  836. array(
  837. 'log' => $log,
  838. 'logUntranslated' => true
  839. )
  840. );
  841. $translate->translate('unknown string');
  842. ]]></programlisting>
  843. </example>
  844. <para>
  845. Now you will have a new notice in the log: <code>Untranslated message within 'de':
  846. unknown string</code>.
  847. </para>
  848. <note>
  849. <para>
  850. You should note that any translation which can not be found will be logged. This
  851. means all translations when a user requests a language which is not supported. Also
  852. every request for a message which can not be translated will be logged. Be aware,
  853. that 100 people requesting the same translation, will result 100 logged notices.
  854. </para>
  855. </note>
  856. <para>
  857. This feature can not only be used to log messages but also to attach this untranslated
  858. messages into an empty translation file. To do so you will have to write your own log
  859. writer which writes the format you want to have and strips the prepending "Untranslated
  860. message".
  861. </para>
  862. <para>
  863. You can also set the '<code>logMessage</code>' option when you want to have your own
  864. log message. Use the '<code>%message%</code>' token for placing the messageId within
  865. your log message, and the '<code>%locale%</code>' token for the requested locale. See
  866. the following example for a self defined log message:
  867. </para>
  868. <example id="zend.translate.additional.logging.example2">
  869. <title>Self defined log messages</title>
  870. <programlisting language="php"><![CDATA[
  871. $translate = new Zend_Translate(
  872. array(
  873. 'adapter' => 'gettext',
  874. 'content' => $path,
  875. 'locale' => 'de'
  876. )
  877. );
  878. // Create a log instance
  879. $writer = new Zend_Log_Writer_Stream('/path/to/file.log');
  880. $log = new Zend_Log($writer);
  881. // Attach it to the translation instance
  882. $translate->setOptions(
  883. array(
  884. 'log' => $log,
  885. 'logMessage' => "Missing '%message%' within locale '%locale%'",
  886. 'logUntranslated' => true
  887. )
  888. );
  889. $translate->translate('unknown string');
  890. ]]></programlisting>
  891. </example>
  892. </sect2>
  893. <sect2 id="zend.translate.additional.sourcedata">
  894. <title>Accessing source data</title>
  895. <para>
  896. Sometimes it is useful to have access to the translation source data. Therefor
  897. the following two functions are provided.
  898. </para>
  899. <para>
  900. The <methodname>getMessageIds($locale = null)</methodname> method returns all known
  901. message IDs as array.
  902. </para>
  903. <para>
  904. The <methodname>getMessages($locale = null)</methodname> method returns the complete
  905. translation source as an array. The message ID is used as key and the translation data
  906. as value.
  907. </para>
  908. <para>
  909. Both methods accept an optional parameter <varname>$locale</varname> which, if set,
  910. returns the translation data for the specified language. If this parameter is not given,
  911. the actual set language will be used. Keep in mind that normally all translations should
  912. be available in all languages. Which means that in a normal situation you will not have
  913. to set this parameter.
  914. </para>
  915. <para>
  916. Additionally the <methodname>getMessages()</methodname> method can be used to return the
  917. complete translation dictionary using the pseudo-locale 'all'. This will return all
  918. available translation data for each added locale.
  919. </para>
  920. <note>
  921. <para>
  922. Attention: the returned array can be <emphasis>very big</emphasis>,
  923. depending on the number of added locales and the amount of translation data.
  924. </para>
  925. </note>
  926. <example id="zend.translate.additional.sourcedata.example">
  927. <title>Handling languages with adapters</title>
  928. <programlisting language="php"><![CDATA[
  929. // returns all known message IDs
  930. $messageIds = $translate->getMessageIds();
  931. print_r($messageIds);
  932. // or just for the specified language
  933. $messageIds = $translate->getMessageIds('en_US');
  934. print_r($messageIds);
  935. // returns all the complete translation data
  936. $source = $translate->getMessages();
  937. print_r($source);
  938. ]]></programlisting>
  939. </example>
  940. </sect2>
  941. </sect1>
  942. <!--
  943. vim:se ts=4 sw=4 et:
  944. -->