Zend_Translate-Plurals.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.translate.plurals">
  4. <title>Plural notations for Translation</title>
  5. <para>
  6. As of Zend Framework 1.9, <classname>Zend_Translate</classname> is able to provide plural
  7. support. Professional translation will always have the need to use plurals as they are
  8. native in almost all languages.
  9. </para>
  10. <para>
  11. So what are plurals? Generally spoken plurals are words which take into account numeric
  12. meanings. But as you may imagine each language has it's own definition of plurals.
  13. English, for example, supports one plural. We have a singular definition, for example
  14. "car", which means implicit one car, and we have the plural definition, "cars" which could
  15. mean more than one car but also zero cars. Other languages like Russian or Polish have
  16. more plurals and also the rules for plurals are different.
  17. </para>
  18. <para>
  19. When you want to use plurals with <classname>Zend_Translate</classname> you must not need
  20. to know how the plurals are defined, only the translator must know as he does the
  21. translation. The only information you need to have is the language.
  22. </para>
  23. <para>
  24. There are two ways for using plurals... the traditional one, which means that you use a own
  25. method, and a modern one, which allows you to do plural translations with the same method
  26. as normal translations.
  27. </para>
  28. <sect2 id="zend.translate.plurals.traditional">
  29. <title>Traditional plural translations</title>
  30. <para>
  31. People who worked with gettext in past will be more common with traditional plural
  32. translations. There is a own <methodname>plural()</methodname> method which can be
  33. used for plural translations.
  34. </para>
  35. <example id="zend.translate.plurals.traditional.example1">
  36. <title>Example of traditional plural translations</title>
  37. <para>
  38. The <methodname>plural()</methodname> method accepts 4 parameters. The first
  39. parameter is the singular messageId, the second is the plural messageId and the
  40. third is the number or amount.
  41. </para>
  42. <para>
  43. The number will be used to detect the plural which has to be returned. A optional
  44. fourth parameter can be used to give a locale which will be used to return the
  45. translation.
  46. </para>
  47. <programlisting language="php"><![CDATA[
  48. $translate = new Zend_Translate(
  49. array(
  50. 'adapter' => 'gettext',
  51. 'content' => '/path/to/german.mo',
  52. 'locale' => 'de'
  53. )
  54. );
  55. $translate->plural('Car', 'Cars', $number);
  56. ]]></programlisting>
  57. </example>
  58. </sect2>
  59. <sect2 id="zend.translate.plurals.modern">
  60. <title>Modern plural translations</title>
  61. <para>
  62. As traditional plural translations are restricted to source code using English plurals
  63. we added a new way for plural translations. It allows to use the same
  64. <methodname>translate()</methodname> for standard and for plural translations.
  65. </para>
  66. <para>
  67. To use plural translations with <methodname>translate()</methodname> you need to give
  68. an array as messageId instead of an string. This array must have the original plural
  69. messageId's, then the amount and at last an optional locale when your given messageId's
  70. are not in English notation.
  71. </para>
  72. <example id="zend.translate.plurals.modern.example1">
  73. <title>Example of modern plural translations</title>
  74. <para>
  75. When we want to translate the same plural definitions like in the previous our
  76. example would have to be defined like below.
  77. </para>
  78. <programlisting language="php"><![CDATA[
  79. $translate = new Zend_Translate(
  80. array(
  81. 'adapter' => 'gettext',
  82. 'content' => '/path/to/german.mo',
  83. 'locale' => 'de'
  84. )
  85. );
  86. $translate->translate(array('Car', 'Cars', $number));
  87. ]]></programlisting>
  88. </example>
  89. <para>
  90. Using modern plural translations it is also possible to use any language as source
  91. for messageId's.
  92. </para>
  93. <example id="zend.translate.plurals.modern.example2">
  94. <title>Example of modern plural translations using a different source language</title>
  95. <para>
  96. Let's expect we want to use Russian and let's also expect that the given
  97. messageId's are Russian and not English.
  98. </para>
  99. <programlisting language="php"><![CDATA[
  100. $translate = new Zend_Translate(
  101. array(
  102. 'adapter' => 'gettext',
  103. 'content' => '/path/to/german.mo',
  104. 'locale' => 'de'
  105. )
  106. );
  107. $translate->translate(
  108. array(
  109. 'Car',
  110. 'Cars first plural',
  111. 'Cars second plural',
  112. $number,
  113. 'ru'
  114. )
  115. );
  116. ]]></programlisting>
  117. </example>
  118. <para>
  119. As you can see you can give more than just the one English plural. But you must give
  120. the source language in this case so <classname>Zend_Translate</classname> knows which
  121. plural rules it has to apply.
  122. </para>
  123. <para>
  124. When you omit the plural language then English will be used per default and any
  125. additional plural definition will be ignored.
  126. </para>
  127. </sect2>
  128. <sect2 id="zend.translate.plurals.source">
  129. <title>Plural source files</title>
  130. <para>
  131. Not all source formats support plural forms. Look into this list for details:
  132. </para>
  133. <table id="zend.translate.plurals.source.supportedadapters">
  134. <title>Plural support</title>
  135. <tgroup cols="4">
  136. <thead>
  137. <row>
  138. <entry>Adapter</entry>
  139. <entry>Plurals supported</entry>
  140. </row>
  141. </thead>
  142. <tbody>
  143. <row>
  144. <entry>Array</entry>
  145. <entry><emphasis>yes</emphasis></entry>
  146. </row>
  147. <row>
  148. <entry>Csv</entry>
  149. <entry><emphasis>yes</emphasis></entry>
  150. </row>
  151. <row>
  152. <entry>Gettext</entry>
  153. <entry><emphasis>yes</emphasis></entry>
  154. </row>
  155. <row>
  156. <entry>Ini</entry>
  157. <entry><emphasis>no</emphasis></entry>
  158. </row>
  159. <row>
  160. <entry>Qt</entry>
  161. <entry><emphasis>no</emphasis></entry>
  162. </row>
  163. <row>
  164. <entry>Tbx</entry>
  165. <entry><emphasis>no</emphasis></entry>
  166. </row>
  167. <row>
  168. <entry>Tmx</entry>
  169. <entry><emphasis>no</emphasis></entry>
  170. </row>
  171. <row>
  172. <entry>Xliff</entry>
  173. <entry><emphasis>no</emphasis></entry>
  174. </row>
  175. <row>
  176. <entry>XmlTm</entry>
  177. <entry><emphasis>no</emphasis></entry>
  178. </row>
  179. </tbody>
  180. </tgroup>
  181. </table>
  182. <para>
  183. Below you can find examples of plural defined source files.
  184. </para>
  185. <sect3 id="zend.translate.plurals.source.array">
  186. <title>Array source with plural definitions</title>
  187. <para>
  188. An array with plural definitions has to look like the following example.
  189. </para>
  190. <programlisting language="php"><![CDATA[
  191. array(
  192. 'plural_0' => array(
  193. 'plural_0 (ru)',
  194. 'plural_1 (ru)',
  195. 'plural_2 (ru)',
  196. 'plural_3 (ru)'
  197. ),
  198. 'plural_1' => ''
  199. );
  200. ]]></programlisting>
  201. <para>
  202. In the above example 'plural_0' and 'plural_1' are the
  203. plural definitions from the source code. And the array at 'plural_0'
  204. has all translated plural forms available. Take a look at the following example
  205. with real content and translation from English source to German.
  206. </para>
  207. <programlisting language="php"><![CDATA[
  208. array(
  209. 'Car' => array(
  210. 'Auto',
  211. 'Autos'
  212. ),
  213. 'Cars' => ''
  214. );
  215. ]]></programlisting>
  216. <para>
  217. When your translated language supports more plural forms then simply add them to
  218. the array below the first plural form. When your source language supports more
  219. plural forms, than simply add a new empty translation.
  220. </para>
  221. </sect3>
  222. <sect3 id="zend.translate.plurals.source.csv">
  223. <title>Csv source with plural definitions</title>
  224. <para>
  225. A csv file with plural definitions has to look like the following example.
  226. </para>
  227. <programlisting language="php"><![CDATA[
  228. "plural_0";"plural_0 (ru)";"plural_1 (ru)";"plural_2 (ru)";"plural_3 (ru)"
  229. "plural_1";
  230. ]]></programlisting>
  231. <para>
  232. All translated plural forms have to be added after the first plural of the source
  233. language. And all further plural forms of the source language have to be added
  234. below but without translation. Note that you must add a delimiter to empty
  235. source plurals.
  236. </para>
  237. </sect3>
  238. <sect3 id="zend.translate.plurals.source.gettext">
  239. <title>Gettext source with plural definitions</title>
  240. <para>
  241. Gettext sources support plural forms out of the box. There is no need for adoption
  242. as the <filename>*.mo</filename> file will contain all necessary data.
  243. </para>
  244. <note>
  245. <para>
  246. Note that gettext does not support the usage of source languages which are not
  247. using english plural forms. When you plan to use a source language which
  248. supports other plural forms like russian for example, then you can not use
  249. gettext sources.
  250. </para>
  251. </note>
  252. </sect3>
  253. </sect2>
  254. <sect2 id="zend.translate.plurals.customrules">
  255. <title>Custom plural rules</title>
  256. <para>
  257. In rare cases it could be useful to be able to define own plural rules. See Chinese for
  258. example. This language defines two plural rules. Per default it does not use plurals.
  259. But in rare cases it uses a rule like <emphasis>(number == 1) ? 0 : 1</emphasis>.
  260. </para>
  261. <para>
  262. Also when you want to use a language which has no known plural rules, and would want to
  263. define your own rules.
  264. </para>
  265. <para>
  266. This can be done by using <methodname>Zend_Translate_Plural::setRule()</methodname>.
  267. The method expects two parameters which must be given. A rule, which is simply a
  268. callback to a self defined method. And a locale for which the rule will be used.
  269. </para>
  270. <para>
  271. Your rule could look like this:
  272. </para>
  273. <programlisting language="php"><![CDATA[
  274. public function MyRule($number) {
  275. return ($number == 10) ? 0 : 1;
  276. }
  277. ]]></programlisting>
  278. <para>
  279. As you see, your rule must accept one parameter. It is the number which you will use to
  280. return which plural the translation has to use. In our example we defined that when we
  281. get a '10' the plural definition 0 has to be used, in all other cases we're using 1.
  282. </para>
  283. <para>
  284. Your rules can be as simple or as complicated as you want. You must only return an
  285. integer value. The plural definition 0 stands for singular translation, and 1 stands for
  286. the first plural rule.
  287. </para>
  288. <para>
  289. To activate your rule, and to link it to the wished locale, you have to call it like
  290. this:
  291. </para>
  292. <programlisting language="php"><![CDATA[
  293. Zend_Translate_Plural::setPlural('MyPlural', 'zh');
  294. ]]></programlisting>
  295. <para>
  296. Now we linked our plural definition to the Chinese language.
  297. </para>
  298. <para>
  299. You can define one plural rule for every language. But you should be aware that you set
  300. the plural rules before you are doing translations.
  301. </para>
  302. <note>
  303. <title>Define custom plurals only when needed</title>
  304. <para>
  305. <classname>Zend_Translate</classname> defines plurals for most known languages.
  306. You should not define own plurals when you are not in need. The default rules work
  307. most of time.
  308. </para>
  309. </note>
  310. </sect2>
  311. </sect1>
  312. <!--
  313. vim:se ts=4 sw=4 et:
  314. -->