Zend_Translate-Additional.xml 47 KB

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