RegionInfo.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. ////////////////////////////////////////////////////////////////////////////
  5. //
  6. //
  7. // Purpose: This class represents settings specified by de jure or
  8. // de facto standards for a particular country/region. In
  9. // contrast to CultureInfo, the RegionInfo does not represent
  10. // preferences of the user and does not depend on the user's
  11. // language or culture.
  12. //
  13. //
  14. ////////////////////////////////////////////////////////////////////////////
  15. using System.Diagnostics;
  16. namespace System.Globalization
  17. {
  18. public class RegionInfo
  19. {
  20. //--------------------------------------------------------------------//
  21. // Internal Information //
  22. //--------------------------------------------------------------------//
  23. //
  24. // Variables.
  25. //
  26. //
  27. // Name of this region (ie: es-US): serialized, the field used for deserialization
  28. //
  29. internal string _name;
  30. //
  31. // The CultureData instance that we are going to read data from.
  32. //
  33. internal CultureData _cultureData;
  34. //
  35. // The RegionInfo for our current region
  36. //
  37. internal static volatile RegionInfo s_currentRegionInfo;
  38. ////////////////////////////////////////////////////////////////////////
  39. //
  40. // RegionInfo Constructors
  41. //
  42. // Note: We prefer that a region be created with a full culture name (ie: en-US)
  43. // because otherwise the native strings won't be right.
  44. //
  45. // In Silverlight we enforce that RegionInfos must be created with a full culture name
  46. //
  47. ////////////////////////////////////////////////////////////////////////
  48. public RegionInfo(string name)
  49. {
  50. if (name == null)
  51. throw new ArgumentNullException(nameof(name));
  52. if (name.Length == 0) //The InvariantCulture has no matching region
  53. {
  54. throw new ArgumentException(SR.Argument_NoRegionInvariantCulture, nameof(name));
  55. }
  56. //
  57. // For CoreCLR we only want the region names that are full culture names
  58. //
  59. _cultureData = CultureData.GetCultureDataForRegion(name, true);
  60. if (_cultureData == null)
  61. throw new ArgumentException(
  62. string.Format(
  63. CultureInfo.CurrentCulture,
  64. SR.Argument_InvalidCultureName, name), nameof(name));
  65. // Not supposed to be neutral
  66. if (_cultureData.IsNeutralCulture)
  67. throw new ArgumentException(SR.Format(SR.Argument_InvalidNeutralRegionName, name), nameof(name));
  68. SetName(name);
  69. }
  70. public RegionInfo(int culture)
  71. {
  72. if (culture == CultureInfo.LOCALE_INVARIANT) //The InvariantCulture has no matching region
  73. {
  74. throw new ArgumentException(SR.Argument_NoRegionInvariantCulture);
  75. }
  76. if (culture == CultureInfo.LOCALE_NEUTRAL)
  77. {
  78. // Not supposed to be neutral
  79. throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), nameof(culture));
  80. }
  81. if (culture == CultureInfo.LOCALE_CUSTOM_DEFAULT)
  82. {
  83. // Not supposed to be neutral
  84. throw new ArgumentException(SR.Format(SR.Argument_CustomCultureCannotBePassedByNumber, culture), nameof(culture));
  85. }
  86. _cultureData = CultureData.GetCultureData(culture, true);
  87. _name = _cultureData.SREGIONNAME;
  88. if (_cultureData.IsNeutralCulture)
  89. {
  90. // Not supposed to be neutral
  91. throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), nameof(culture));
  92. }
  93. }
  94. internal RegionInfo(CultureData cultureData)
  95. {
  96. _cultureData = cultureData;
  97. _name = _cultureData.SREGIONNAME;
  98. }
  99. private void SetName(string name)
  100. {
  101. // Use the name of the region we found
  102. _name = _cultureData.SREGIONNAME;
  103. }
  104. ////////////////////////////////////////////////////////////////////////
  105. //
  106. // GetCurrentRegion
  107. //
  108. // This instance provides methods based on the current user settings.
  109. // These settings are volatile and may change over the lifetime of the
  110. // thread.
  111. //
  112. ////////////////////////////////////////////////////////////////////////
  113. public static RegionInfo CurrentRegion
  114. {
  115. get
  116. {
  117. RegionInfo temp = s_currentRegionInfo;
  118. if (temp == null)
  119. {
  120. temp = new RegionInfo(CultureInfo.CurrentCulture._cultureData);
  121. // Need full name for custom cultures
  122. temp._name = temp._cultureData.SREGIONNAME;
  123. s_currentRegionInfo = temp;
  124. }
  125. return temp;
  126. }
  127. }
  128. ////////////////////////////////////////////////////////////////////////
  129. //
  130. // GetName
  131. //
  132. // Returns the name of the region (ie: en-US)
  133. //
  134. ////////////////////////////////////////////////////////////////////////
  135. public virtual string Name
  136. {
  137. get
  138. {
  139. Debug.Assert(_name != null, "Expected RegionInfo._name to be populated already");
  140. return (_name);
  141. }
  142. }
  143. ////////////////////////////////////////////////////////////////////////
  144. //
  145. // GetEnglishName
  146. //
  147. // Returns the name of the region in English. (ie: United States)
  148. //
  149. ////////////////////////////////////////////////////////////////////////
  150. public virtual string EnglishName
  151. {
  152. get
  153. {
  154. return (_cultureData.SENGCOUNTRY);
  155. }
  156. }
  157. ////////////////////////////////////////////////////////////////////////
  158. //
  159. // GetDisplayName
  160. //
  161. // Returns the display name (localized) of the region. (ie: United States
  162. // if the current UI language is en-US)
  163. //
  164. ////////////////////////////////////////////////////////////////////////
  165. public virtual string DisplayName
  166. {
  167. get
  168. {
  169. return (_cultureData.SLOCALIZEDCOUNTRY);
  170. }
  171. }
  172. ////////////////////////////////////////////////////////////////////////
  173. //
  174. // GetNativeName
  175. //
  176. // Returns the native name of the region. (ie: Deutschland)
  177. // WARNING: You need a full locale name for this to make sense.
  178. //
  179. ////////////////////////////////////////////////////////////////////////
  180. public virtual string NativeName
  181. {
  182. get
  183. {
  184. return (_cultureData.SNATIVECOUNTRY);
  185. }
  186. }
  187. ////////////////////////////////////////////////////////////////////////
  188. //
  189. // TwoLetterISORegionName
  190. //
  191. // Returns the two letter ISO region name (ie: US)
  192. //
  193. ////////////////////////////////////////////////////////////////////////
  194. public virtual string TwoLetterISORegionName
  195. {
  196. get
  197. {
  198. return (_cultureData.SISO3166CTRYNAME);
  199. }
  200. }
  201. ////////////////////////////////////////////////////////////////////////
  202. //
  203. // ThreeLetterISORegionName
  204. //
  205. // Returns the three letter ISO region name (ie: USA)
  206. //
  207. ////////////////////////////////////////////////////////////////////////
  208. public virtual string ThreeLetterISORegionName
  209. {
  210. get
  211. {
  212. return (_cultureData.SISO3166CTRYNAME2);
  213. }
  214. }
  215. ////////////////////////////////////////////////////////////////////////
  216. //
  217. // ThreeLetterWindowsRegionName
  218. //
  219. // Returns the three letter windows region name (ie: USA)
  220. //
  221. ////////////////////////////////////////////////////////////////////////
  222. public virtual string ThreeLetterWindowsRegionName
  223. {
  224. get
  225. {
  226. // ThreeLetterWindowsRegionName is really same as ThreeLetterISORegionName
  227. return ThreeLetterISORegionName;
  228. }
  229. }
  230. ////////////////////////////////////////////////////////////////////////
  231. //
  232. // IsMetric
  233. //
  234. // Returns true if this region uses the metric measurement system
  235. //
  236. ////////////////////////////////////////////////////////////////////////
  237. public virtual bool IsMetric
  238. {
  239. get
  240. {
  241. int value = _cultureData.IMEASURE;
  242. return (value == 0);
  243. }
  244. }
  245. public virtual int GeoId
  246. {
  247. get
  248. {
  249. return (_cultureData.IGEOID);
  250. }
  251. }
  252. ////////////////////////////////////////////////////////////////////////
  253. //
  254. // CurrencyEnglishName
  255. //
  256. // English name for this region's currency, ie: Swiss Franc
  257. //
  258. ////////////////////////////////////////////////////////////////////////
  259. public virtual string CurrencyEnglishName
  260. {
  261. get
  262. {
  263. return (_cultureData.SENGLISHCURRENCY);
  264. }
  265. }
  266. ////////////////////////////////////////////////////////////////////////
  267. //
  268. // CurrencyNativeName
  269. //
  270. // Native name for this region's currency, ie: Schweizer Franken
  271. // WARNING: You need a full locale name for this to make sense.
  272. //
  273. ////////////////////////////////////////////////////////////////////////
  274. public virtual string CurrencyNativeName
  275. {
  276. get
  277. {
  278. return (_cultureData.SNATIVECURRENCY);
  279. }
  280. }
  281. ////////////////////////////////////////////////////////////////////////
  282. //
  283. // CurrencySymbol
  284. //
  285. // Currency Symbol for this locale, ie: Fr. or $
  286. //
  287. ////////////////////////////////////////////////////////////////////////
  288. public virtual string CurrencySymbol
  289. {
  290. get
  291. {
  292. return (_cultureData.SCURRENCY);
  293. }
  294. }
  295. ////////////////////////////////////////////////////////////////////////
  296. //
  297. // ISOCurrencySymbol
  298. //
  299. // ISO Currency Symbol for this locale, ie: CHF
  300. //
  301. ////////////////////////////////////////////////////////////////////////
  302. public virtual string ISOCurrencySymbol
  303. {
  304. get
  305. {
  306. return (_cultureData.SINTLSYMBOL);
  307. }
  308. }
  309. ////////////////////////////////////////////////////////////////////////
  310. //
  311. // Equals
  312. //
  313. // Implements Object.Equals(). Returns a boolean indicating whether
  314. // or not object refers to the same RegionInfo as the current instance.
  315. //
  316. // RegionInfos are considered equal if and only if they have the same name
  317. // (ie: en-US)
  318. //
  319. ////////////////////////////////////////////////////////////////////////
  320. public override bool Equals(object value)
  321. {
  322. if (value is RegionInfo that)
  323. {
  324. return this.Name.Equals(that.Name);
  325. }
  326. return (false);
  327. }
  328. ////////////////////////////////////////////////////////////////////////
  329. //
  330. // GetHashCode
  331. //
  332. // Implements Object.GetHashCode(). Returns the hash code for the
  333. // CultureInfo. The hash code is guaranteed to be the same for RegionInfo
  334. // A and B where A.Equals(B) is true.
  335. //
  336. ////////////////////////////////////////////////////////////////////////
  337. public override int GetHashCode()
  338. {
  339. return (this.Name.GetHashCode());
  340. }
  341. ////////////////////////////////////////////////////////////////////////
  342. //
  343. // ToString
  344. //
  345. // Implements Object.ToString(). Returns the name of the Region, ie: es-US
  346. //
  347. ////////////////////////////////////////////////////////////////////////
  348. public override string ToString()
  349. {
  350. return (Name);
  351. }
  352. }
  353. }