Zend_Translate-Additional.xml 49 KB

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