Zend_Currency-Usage.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <sect1 id="zend.currency.usage">
  2. <title> 如何使用货币 </title>
  3. <para>
  4. 在自己的程序中使用 <code>Zend_Currency</code>,就是创建一个它的实例,不需要任何参数。它将创建一个带有实际地方、并定义了在这个地方使用的货币的 <code>Zend_Currency</code> 的一个实例。
  5. </para>
  6. <example id="zend.currency.usage.example1">
  7. <title> 从实际地方创建 Zend_Currency 的实例 </title>
  8. <para>
  9. 期望通过用户(的输入)或环境,你有 'en_US' 设置作为实际地方,通过使用不带参数创建<code>Zend_Currency</code>的实例从地方 'en_US' 来使用实际货币,这导致带美元实例作为带有'en_US'格式规则的实际货币。
  10. </para>
  11. <programlisting role="php"><![CDATA[
  12. $currency = new Zend_Currency();
  13. ]]>
  14. </programlisting>
  15. </example>
  16. <para>
  17. 从 Zend Framework 1.7.0 开始,<code>Zend_Currency</code> 也支持程序范围的地方的用法。你可以简单地如下设置 <code>Zend_Locale</code> 实例到注册表。
  18. 如果你想多次使用同一地方,用这个符号你不需要记住对每个实例手工设置地方。
  19. </para>
  20. <programlisting role="php"><![CDATA[
  21. // in your bootstrap file
  22. $locale = new Zend_Locale('de_AT');
  23. Zend_Registry::set('Zend_Locale', $locale);
  24. // somewhere in your application
  25. $currency = new Zend_Currency();
  26. ]]>
  27. </programlisting>
  28. <note>
  29. <para>
  30. 要知道,如果你的系统没有缺省地方,或者如果你系统的地方不能自动检测到, <code>Zend_Currency</code> 将抛出一个异常。如果这样,你应当手动设置地方。
  31. </para>
  32. </note>
  33. <para>
  34. 当然,根据需要,若干参数可以在创建时给出,每个参数都是可选的和被禁止,甚至参数的顺序也可以交换。每个参数的意思描述如下:
  35. </para>
  36. <itemizedlist mark='opencircle'>
  37. <listitem>
  38. <para>
  39. <emphasis role="strong">currency</emphasis>:
  40. </para>
  41. <para>
  42. 一个地方可以有多种货币,因此第一个参数 <emphasis role="strong">'currency'</emphasis> 可通过给出短名称或全名称来定义哪个货币应当被使用。 如果某货币不被任何地方认识,那么就抛出一个异常。货币短名总是三个字母并且是大写。众所周知的货币短名有<code>USD</code> 或 <code>EUR</code> 。想知道所有货币请参考 <code>Zend_Currency</code> 的信息方法。
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. <emphasis role="strong">locale</emphasis>:
  48. </para>
  49. <para>
  50. 第三个参数 <emphasis role="strong">'locale'</emphasis> 定义了哪个地方用来格式化货币,给出的地方也将用来获得标准脚本和货币(如果这些参数没有给出时的货币)。
  51. </para>
  52. <note>
  53. <para>
  54. 注意 Zend_Currency 只接受包括地区的地方,这意味着所有给定的只包括语言地方将抛出一个异常。例如地方 <emphasis role="strong">en</emphasis> 将抛出异常而地方 <emphasis role="strong">en_US</emphasis> 将返回 <emphasis role="strong">USD</emphasis> 作为货币。
  55. </para>
  56. </note>
  57. </listitem>
  58. </itemizedlist>
  59. <example id="zend.currency.usage.example2">
  60. <title> 创建 Zend_Currency 实例的其它例子 </title>
  61. <programlisting role="php"><![CDATA[
  62. // expect standard locale 'de_AT'
  63. // creates an instance from 'en_US' using 'USD' which is default
  64. // currency for 'en_US'
  65. $currency = new Zend_Currency('en_US');
  66. // creates an instance from the actual locale ('de_AT') using 'EUR' as
  67. // currency
  68. $currency = new Zend_Currency();
  69. // creates an instance using 'EUR' as currency, 'en_US' for number
  70. // formating
  71. $currency = new Zend_Currency('en_US', 'EUR');
  72. ]]>
  73. </programlisting>
  74. </example>
  75. <para>
  76. 如果想使用缺省值,你可以禁止任何参数,在处理货币方面没有副作用,例如这在当你不知道某地区的缺省货币很有用。
  77. </para>
  78. <note>
  79. <para>
  80. 许多国家有多种货币,一种正在使用的和一些古老的。如果 '<emphasis role="strong">currency</emphasis>' 参数被禁止,那么使用当前货币。例如在地区 '<emphasis role="strong">de</emphasis>' 有 '<emphasis role="strong">EUR</emphasis>' 和 '<emphasis role="strong">DEM</emphasis>'...'<emphasis role="strong">EUR</emphasis>' 是当前使用的货币,如果这参数被禁止它将被使用。
  81. </para>
  82. </note>
  83. <sect2 id="zend.currency.usage.tocurrency">
  84. <title> 从货币创建输出 </title>
  85. <para>
  86. 可以用方法 <emphasis role="strong">toCurrency()</emphasis> 把存在的数值转换成格式化的货币输出,它带有一个可以被转换的数值,这个数值可以是任何标准化的数字。
  87. </para>
  88. <para>
  89. 如果有个需要转换的本地化的数字,首先用 <link linkend="zend.locale.number.normalize">Zend_Locale_Format::getNumber()</link> 来标准化,然后用 <code>toCurrency()</code> 创建一个货币输出。
  90. </para>
  91. <para>
  92. <code>toCurrency(array $options)</code> 接受带有选项的数组,这个选项可用来临时设置成另外的格式或货币表示,关于选项的细节参见 <link linkend="zend.currency.usage.setformat">Changing the format of a currency</link>。
  93. </para>
  94. <example id="zend.currency.usage.tocurrency.example">
  95. <title> 为货币创建输出 </title>
  96. <programlisting role="php"><![CDATA[
  97. // creates an instance with 'en_US' using 'USD' which is the default
  98. // values for 'en_US'
  99. $currency = new Zend_Currency('en_US');
  100. // prints '$ 1,000.00'
  101. echo $currency->toCurrency(1000);
  102. // prints '$ 1.000,00'
  103. echo $currency->toCurrency(1000, array('format' => 'de_AT'));
  104. // prints '$ ١٬٠٠٠٫٠٠'
  105. echo $currency->toCurrency(1000, array('script' => 'Arab'));
  106. ]]>
  107. </programlisting>
  108. </example>
  109. </sect2>
  110. <sect2 id="zend.currency.usage.setformat">
  111. <title> 修改货币格式 </title>
  112. <para>
  113. 用来创建 <code>Zend_Currency</code> 实例的格式当然是标准格式,但有时候也需要为自己的意图需要这个格式。
  114. </para>
  115. <para>
  116. 货币输出的格式包括下面部分:
  117. </para>
  118. <itemizedlist mark='opencircle'>
  119. <listitem>
  120. <para>
  121. <emphasis role="strong"> 货币符号,短名或名字 </emphasis>:
  122. </para>
  123. <para>
  124. 或不符号一般显示在货币输出之内,如果需要或重写,它可以被禁止。
  125. </para>
  126. </listitem>
  127. <listitem>
  128. <para>
  129. <emphasis role="strong"> 货币位置 </emphasis>:
  130. </para>
  131. <para>
  132. 货币符号的位置一般由地方来自动定义,如果需要,它可以被修改。
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <emphasis role="strong">Script</emphasis>:
  138. </para>
  139. <para>
  140. Script 用来显示数字,它的详细用法可以从 <code>Zend_Locale</code> 的文档 <link linkend="zend.locale.appendix.numberscripts.supported"> 被支持的数字 scripts</link>找到。
  141. </para>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <emphasis role="strong"> 数字格式 </emphasis>:
  146. </para>
  147. <para>
  148. 货币的数量 ( 就是钱的数量 )用在地方( locale )里的格式化规则来格式化,例如在英语中 ',' 用来分离每一千,在德国就用 '.'符号(例如一百万,在英语中就是1,000,000 而在德语中就是1.000.000 好像在德语中容易和小数点混淆 by Jason Qi)。
  149. </para>
  150. </listitem>
  151. </itemizedlist>
  152. <para>
  153. 如果确实需要修改格式,你可以用 <emphasis role="strong">setFormat()</emphasis> 方法。它带有一个数组,包括所有你向修改的选项。<code>options</code> 数组支持下列设置:
  154. </para>
  155. <itemizedlist mark='opencircle'>
  156. <listitem>
  157. <para>
  158. <emphasis role="strong">position</emphasis>: 定义货币显示位置,从 <link linkend="zend.currency.usage.setformat.constantsposition">this table</link> 可以找到被支持的位置。
  159. </para>
  160. </listitem>
  161. <listitem>
  162. <para>
  163. <emphasis role="strong">script</emphasis>: 定义哪个 script 被用来显示数字,大部分地方的缺省的 script 是 <emphasis role="strong">'Latn'</emphasis> ,它包括数字 0 到 9 。并且其它的 scripts 如 'Arab'(Arabian) 也可以用。所有被支持的 scipts 可以从 <link linkend="zend.locale.appendix.numberscripts.supported">this table</link> 找到。
  164. </para>
  165. </listitem>
  166. <listitem>
  167. <para>
  168. <emphasis role="strong">format</emphasis>: 定义哪个地方(locale)用来显示数字,数字格式包括千为分隔符。如果没有指定格式,就使用 <code>Zend_Currency</code> 对象中的地方( locale )。
  169. </para>
  170. </listitem>
  171. <listitem>
  172. <para>
  173. <emphasis role="strong">display</emphasis>: 定义货币中的哪个部分用来显示货币表示,有四中表示法可用,都在 <link linkend="zend.currency.usage.setformat.constantsdescription">this table</link> 中描述。
  174. </para>
  175. </listitem>
  176. <listitem>
  177. <para>
  178. <emphasis role="strong">precision</emphasis>: 定义用于货币表示的精确位数,它的缺省值是<emphasis role="strong">2</emphasis>。
  179. </para>
  180. </listitem>
  181. <listitem>
  182. <para>
  183. <emphasis role="strong">name</emphasis>: 定义被显示的货币名称,它重写通过创建 <code>Zend_Currency</code> 产生的任何货币名称。
  184. </para>
  185. </listitem>
  186. <listitem>
  187. <para>
  188. <emphasis role="strong">currency</emphasis>: 定义被显示的国际缩写,它重写通过创建 <code>Zend_Currency</code> 产生的任何缩写。
  189. </para>
  190. </listitem>
  191. <listitem>
  192. <para>
  193. <emphasis role="strong">symbol</emphasis>: 定义被显示的货币符号,它重写通过创建 <code>Zend_Currency</code> 产生的任何符号。
  194. </para>
  195. </listitem>
  196. </itemizedlist>
  197. <table id="zend.currency.usage.setformat.constantsdescription">
  198. <title> 选择货币描述的常量 </title>
  199. <tgroup cols="2" align="left">
  200. <thead>
  201. <row>
  202. <entry> 常量 </entry>
  203. <entry> 描述 </entry>
  204. </row>
  205. </thead>
  206. <tbody>
  207. <row>
  208. <entry>NO_SYMBOL</entry>
  209. <entry> 不显示任何货币表示 </entry>
  210. </row>
  211. <row>
  212. <entry>USE_SYMBOL</entry>
  213. <entry> 显示货币符号 </entry>
  214. </row>
  215. <row>
  216. <entry>USE_SHORTNAME</entry>
  217. <entry> 显示三个字母的国际货币缩写 </entry>
  218. </row>
  219. <row>
  220. <entry>USE_NAME</entry>
  221. <entry> 显示货币全名 </entry>
  222. </row>
  223. </tbody>
  224. </tgroup>
  225. </table>
  226. <table id="zend.currency.usage.setformat.constantsposition">
  227. <title> 选择货币位置的常量 </title>
  228. <tgroup cols="2" align="left">
  229. <thead>
  230. <row>
  231. <entry> 常量 </entry>
  232. <entry> 描述 </entry>
  233. </row>
  234. </thead>
  235. <tbody>
  236. <row>
  237. <entry>STANDARD</entry>
  238. <entry> 设置在地方里定义标准位置 </entry>
  239. </row>
  240. <row>
  241. <entry>RIGHT</entry>
  242. <entry> 在数的右面显示货币表示符 </entry>
  243. </row>
  244. <row>
  245. <entry>LEFT</entry>
  246. <entry> 在数值的左面显示货币表示符 </entry>
  247. </row>
  248. </tbody>
  249. </tgroup>
  250. </table>
  251. <example id="zend.currency.usage.setformat.example">
  252. <title> 修改货币的显示格式 </title>
  253. <programlisting role="php"><![CDATA[
  254. // creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US' as
  255. // these are the default values from 'en_US'
  256. $currency = new Zend_Currency('en_US');
  257. // prints 'US$ 1,000.00'
  258. echo $currency->toCurrency(1000);
  259. $currency->setFormat('display' => Zend_Currency::USE_NAME,
  260. 'position' => Zend_Currency::RIGHT);
  261. // prints '1.000,00 US Dollar'
  262. echo $currency->toCurrency(1000);
  263. $currency->setFormat('name' => 'American Dollar');
  264. // prints '1.000,00 American Dollar'
  265. echo $currency->toCurrency(1000);
  266. ]]>
  267. </programlisting>
  268. </example>
  269. </sect2>
  270. <sect2 id="zend.currency.usage.informational">
  271. <title> Zend_Currency 的信息方法 </title>
  272. <para>
  273. 当然,<code>Zend_Currency</code> 也支持从 <code>Zend_Locale</code> 获得任何存在的和许多古老货币的信息。支持的方法是:
  274. </para>
  275. <itemizedlist mark='opencircle'>
  276. <listitem>
  277. <para>
  278. <emphasis role="strong">getSymbol()</emphasis>:
  279. </para>
  280. <para>
  281. 返回实际货币或给定货币的已知符号。例如 <emphasis role="strong">$</emphasis> 在 <emphasis role="strong">en_US</emphasis> 地方表示美元。
  282. </para>
  283. </listitem>
  284. <listitem>
  285. <para>
  286. <emphasis role="strong">getShortName()</emphasis>:
  287. </para>
  288. <para>
  289. 返回实际货币或给定货币的缩写。例如 <emphasis role="strong">USD</emphasis> 在 <emphasis role="strong">en_US</emphasis> 地方表示美元。
  290. </para>
  291. </listitem>
  292. <listitem>
  293. <para>
  294. <emphasis role="strong">getName()</emphasis>:
  295. </para>
  296. <para>
  297. 返回实际货币或给定货币的全名。例如 <emphasis role="strong">US Dollar</emphasis> 在 <emphasis role="strong">en_US</emphasis> 地方表示美元。
  298. </para>
  299. </listitem>
  300. <listitem>
  301. <para>
  302. <emphasis role="strong">getRegionList()</emphasis>:
  303. </para>
  304. <para>
  305. 返回实际货币或给定被使用货币的地区列表。因为某种货币可能被用于多个地区,所以返回值总是一个数组。
  306. </para>
  307. </listitem>
  308. <listitem>
  309. <para>
  310. <emphasis role="strong">getCurrencyList()</emphasis>:
  311. </para>
  312. <para>
  313. 返回用于给定地区的已知货币的列表。
  314. </para>
  315. </listitem>
  316. </itemizedlist>
  317. <para>
  318. 函数 <code>getSymbol()</code>、 <code>getShortName()</code> 和 <code>getName()</code> 带两个可选的参数。如果没有给出参数,期望的数据将从当前设置货币返回。第一个参数是货币的短名,短名总是三个字母,例如 EUR 是欧元,USD 是美元。第二个参数定义从哪个地方( locale )读数据。如果没有给出地方,就使用当前使用的地方设置。
  319. </para>
  320. <example id="zend.currency.usage.informational.example">
  321. <title> 从货币中获取信息 </title>
  322. <programlisting role="php"><![CDATA[
  323. // creates an instance with 'en_US' using 'USD', 'Latin' and 'en_US'
  324. // as these are the default values from 'en_US'
  325. $currency = new Zend_Currency('en_US');
  326. // prints '$'
  327. echo $currency->getSymbol();
  328. // prints 'EUR'
  329. echo $currency->getShortName('EUR');
  330. // prints 'Österreichische Schilling'
  331. echo $currency->getName('ATS', 'de_AT');
  332. // returns an array with all regions where USD is used
  333. print_r($currency->getRegionList();
  334. // returns an array with all currencies which were ever used in this
  335. // region
  336. print_r($currency->getCurrencyList('de_AT');
  337. ]]>
  338. </programlisting>
  339. </example>
  340. </sect2>
  341. <sect2 id="zend.currency.usage.setlocale">
  342. <title> 设置新缺省值 </title>
  343. <para>
  344. <code>setLocale</code> 方法允许设置新的地方给 <code>Zend_Currency</code> 。当调用这个函数所有的货币的缺省值就被重写,包括货币名、缩写和符号。
  345. </para>
  346. <example id="zend.currency.usage.setlocale.example">
  347. <title> 设置新地方 </title>
  348. <programlisting role="php"><![CDATA[
  349. // 获得 US 货币
  350. $currency = new Zend_Currency('en_US');
  351. print $currency->toCurrency(1000);
  352. // 获得 AT 货币
  353. $currency->setLocale('de_AT');
  354. print $currency->toCurrency(1000);
  355. ]]>
  356. </programlisting>
  357. </example>
  358. </sect2>
  359. <sect2 id="zend.currency.usage.cache">
  360. <title> 加速 Zend_Currency </title>
  361. <para>
  362. 通过 <code>Zend_Cache</code> 的使用可以加速 <code>Zend_Currency</code> 的工作。通过使用静态方法 <code>Zend_Currency::setCache($cache)</code> ,它接受一个选项,<code>Zend_Cache</code> 适配器,当设置它,Zend_Currency 方法的本地化数据就被缓存。
  363. 也有个静态方法 <code>Zend_Currency::getCache()</code> 方便你的使用。
  364. </para>
  365. <example id="zend.currency.usage.cache.example">
  366. <title> 缓存货币 </title>
  367. <programlisting role="php"><![CDATA[
  368. // 创建一个缓存对象
  369. $cache = Zend_Cache::factory('Core',
  370. 'File',
  371. array('lifetime' => 120,
  372. 'automatic_serialization' => true),
  373. array('cache_dir'
  374. => dirname(__FILE__) . '/_files/'));
  375. Zend_Currency::setCache($cache);
  376. ]]>
  377. </programlisting>
  378. </example>
  379. </sect2>
  380. </sect1>