Zend_Translate-Additional.xml 46 KB

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