RegionInfo.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // RegionInfo.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Globalization;
  30. using System.Runtime.CompilerServices;
  31. namespace System.Globalization
  32. {
  33. #if NET_2_0
  34. [System.Runtime.InteropServices.ComVisible(true)]
  35. #endif
  36. [Serializable]
  37. public class RegionInfo
  38. {
  39. static RegionInfo currentRegion;
  40. // This property is not synchronized with CurrentCulture, so
  41. // we need to use bootstrap CurrentCulture LCID.
  42. public static RegionInfo CurrentRegion {
  43. get {
  44. if (currentRegion == null) {
  45. // make sure to fill BootstrapCultureID.
  46. CultureInfo ci = CultureInfo.CurrentCulture;
  47. // If current culture is invariant then region is not available.
  48. if (ci == null || CultureInfo.BootstrapCultureID == 0x7F)
  49. return null;
  50. currentRegion = new RegionInfo (CultureInfo.BootstrapCultureID);
  51. }
  52. return currentRegion;
  53. }
  54. }
  55. int regionId;
  56. string iso2Name;
  57. string iso3Name;
  58. string win3Name;
  59. string englishName;
  60. string currencySymbol;
  61. string isoCurrencySymbol;
  62. string currencyEnglishName;
  63. public RegionInfo (int lcid)
  64. {
  65. if (!construct_internal_region_from_lcid (lcid))
  66. throw new ArgumentException (
  67. String.Format ("Region ID {0} (0x{0:X4}) is not a " +
  68. "supported region.", lcid), "lcid");
  69. }
  70. public RegionInfo (string name)
  71. {
  72. if (name == null)
  73. throw new ArgumentNullException ();
  74. if (!construct_internal_region_from_name (name.ToUpperInvariant ()))
  75. throw new ArgumentException ("Region name " + name +
  76. " is not supported.", "name");
  77. }
  78. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  79. private extern bool construct_internal_region_from_lcid (int lcid);
  80. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  81. private extern bool construct_internal_region_from_name (string name);
  82. #if NET_2_0
  83. [System.Runtime.InteropServices.ComVisible(false)]
  84. public virtual string CurrencyEnglishName {
  85. get { return currencyEnglishName; }
  86. }
  87. #endif
  88. public virtual string CurrencySymbol {
  89. get { return currencySymbol; }
  90. }
  91. [MonoTODO]
  92. public virtual string DisplayName {
  93. get { return englishName; }
  94. }
  95. public virtual string EnglishName {
  96. get { return englishName; }
  97. }
  98. public virtual bool IsMetric {
  99. get {
  100. switch (iso2Name) {
  101. case "US":
  102. case "UK":
  103. return false;
  104. default:
  105. return true;
  106. }
  107. }
  108. }
  109. public virtual string ISOCurrencySymbol {
  110. get { return isoCurrencySymbol; }
  111. }
  112. #if NET_2_0
  113. [MonoTODO]
  114. [System.Runtime.InteropServices.ComVisible(false)]
  115. public virtual string NativeName {
  116. get { return DisplayName; }
  117. }
  118. [MonoTODO]
  119. public virtual string CurrencyNativeName {
  120. get { throw new NotImplementedException (); }
  121. }
  122. #endif
  123. public virtual string Name {
  124. get { return iso2Name; }
  125. }
  126. public virtual string ThreeLetterISORegionName {
  127. get { return iso3Name; }
  128. }
  129. public virtual string ThreeLetterWindowsRegionName {
  130. get { return win3Name; }
  131. }
  132. public virtual string TwoLetterISORegionName {
  133. get { return iso2Name; }
  134. }
  135. //
  136. // methods
  137. #if NET_2_0
  138. #else
  139. public override bool Equals (object value)
  140. {
  141. RegionInfo other = value as RegionInfo;
  142. return other != null && regionId == other.regionId;
  143. }
  144. public override int GetHashCode () {
  145. return regionId;
  146. }
  147. #endif
  148. public override string ToString ()
  149. {
  150. return Name;
  151. }
  152. }
  153. }