Zend_Currency-Usage.xml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.currency.usage">
  4. <title>Using Zend_Currency</title>
  5. <sect2 id="zend.currency.usage.generic">
  6. <title>Generic usage</title>
  7. <para>
  8. The simplest usecase within an application is to use the clients locale. When you create
  9. a instance of <classname>Zend_Currency</classname> without giving any options, your
  10. clients locale will be used to set the proper currency.
  11. </para>
  12. <example id="zend.currency.usage.generic.example-1">
  13. <title>Creating a currency with client settings</title>
  14. <para>
  15. Let's assume that your client has set "en_US" as wished language within his browser.
  16. In this case <classname>Zend_Currency</classname> will automatically detect the
  17. currency which has to be used.
  18. </para>
  19. <programlisting language="php"><![CDATA[
  20. $currency = new Zend_Currency();
  21. // See the default settings which are depending on the client
  22. // var_dump($currency);
  23. ]]></programlisting>
  24. <para>
  25. The created object would now contain the currency "US Dollar" as this is the actual
  26. assigned currency for US (United States). It has also other options set, like
  27. "$" for the currency sign or "USD" for the abbreviation.
  28. </para>
  29. </example>
  30. <note>
  31. <title>Automatic locale detection does not always work</title>
  32. <para>
  33. You should note that this automatic locale detection does not always work properly.
  34. The reason for this behaviour is that <classname>Zend_Currency</classname> must have
  35. a locale which includes a region. When the client would only set "en" as locale
  36. <classname>Zend_Currency</classname> would not know which of the more than 30
  37. countries was meant. In this case an exception would be raised.
  38. </para>
  39. <para>
  40. A client could also omit the locale settings within his browser. This would lead to
  41. the problem that your environment settings will be used as fallback and could also
  42. lead to an exception.
  43. </para>
  44. </note>
  45. </sect2>
  46. <sect2 id="zend.currency.usage.locale">
  47. <title>Currency creation based on a locale</title>
  48. <para>
  49. To prevent the problems with your client you could simply set the wished locale
  50. manually.
  51. </para>
  52. <programlisting language="php"><![CDATA[
  53. $currency = new Zend_Currency('en_US');
  54. // You can also use the 'locale' option
  55. // $currency = new Zend_Currency(array('locale' => 'en_US'));
  56. // See the actual settings which are fixed to 'en_US'
  57. // var_dump($currency);
  58. ]]></programlisting>
  59. <para>
  60. As within our first example the used currency will be "US Dollar". But now we are no
  61. longer dependend on the clients settings.
  62. </para>
  63. <para>
  64. <classname>Zend_Currency</classname> also supports the usage of an application-wide
  65. locale. You can set a <classname>Zend_Locale</classname> instance in the registry as
  66. shown below. With this notation you can avoid setting the locale manually for each
  67. instance when you want to use the same locale throughout the application.
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. // in your bootstrap file
  71. $locale = new Zend_Locale('de_AT');
  72. Zend_Registry::set('Zend_Locale', $locale);
  73. // somewhere in your application
  74. $currency = new Zend_Currency();
  75. ]]></programlisting>
  76. </sect2>
  77. <sect2 id="zend.currency.usage.territory">
  78. <title>Currency creation based on a country</title>
  79. <para>
  80. <classname>Zend_Currency</classname> is also able to work on a given country by using
  81. <classname>Zend_Locale</classname> internally.
  82. </para>
  83. <programlisting language="php"><![CDATA[
  84. $currency = new Zend_Currency('US');
  85. // See the actual settings which are fixed to 'en_US'
  86. // var_dump($currency);
  87. ]]></programlisting>
  88. <note>
  89. <title>Uppercase territories</title>
  90. <para>
  91. When you know that you are using a territory, then you should uppercase it.
  92. Otherwise you could get an in your eyes false locale in return. For example,
  93. when you give "om" then you could expect "ar_OM" to be returned. But in fact it
  94. returns "om", as it's also a language.
  95. </para>
  96. <para>
  97. Therefor always uppercase the input when you know that a territory is meant.
  98. </para>
  99. </note>
  100. </sect2>
  101. </sect1>