| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- // See the LICENSE file in the project root for more information.
- ////////////////////////////////////////////////////////////////////////////
- //
- //
- // Purpose: This class represents settings specified by de jure or
- // de facto standards for a particular country/region. In
- // contrast to CultureInfo, the RegionInfo does not represent
- // preferences of the user and does not depend on the user's
- // language or culture.
- //
- //
- ////////////////////////////////////////////////////////////////////////////
- using System.Diagnostics;
- namespace System.Globalization
- {
- public class RegionInfo
- {
- //--------------------------------------------------------------------//
- // Internal Information //
- //--------------------------------------------------------------------//
- //
- // Variables.
- //
- //
- // Name of this region (ie: es-US): serialized, the field used for deserialization
- //
- internal string _name;
- //
- // The CultureData instance that we are going to read data from.
- //
- internal CultureData _cultureData;
- //
- // The RegionInfo for our current region
- //
- internal static volatile RegionInfo s_currentRegionInfo;
- ////////////////////////////////////////////////////////////////////////
- //
- // RegionInfo Constructors
- //
- // Note: We prefer that a region be created with a full culture name (ie: en-US)
- // because otherwise the native strings won't be right.
- //
- // In Silverlight we enforce that RegionInfos must be created with a full culture name
- //
- ////////////////////////////////////////////////////////////////////////
- public RegionInfo(string name)
- {
- if (name == null)
- throw new ArgumentNullException(nameof(name));
- if (name.Length == 0) //The InvariantCulture has no matching region
- {
- throw new ArgumentException(SR.Argument_NoRegionInvariantCulture, nameof(name));
- }
- //
- // For CoreCLR we only want the region names that are full culture names
- //
- _cultureData = CultureData.GetCultureDataForRegion(name, true);
- if (_cultureData == null)
- throw new ArgumentException(
- string.Format(
- CultureInfo.CurrentCulture,
- SR.Argument_InvalidCultureName, name), nameof(name));
- // Not supposed to be neutral
- if (_cultureData.IsNeutralCulture)
- throw new ArgumentException(SR.Format(SR.Argument_InvalidNeutralRegionName, name), nameof(name));
- SetName(name);
- }
- public RegionInfo(int culture)
- {
- if (culture == CultureInfo.LOCALE_INVARIANT) //The InvariantCulture has no matching region
- {
- throw new ArgumentException(SR.Argument_NoRegionInvariantCulture);
- }
- if (culture == CultureInfo.LOCALE_NEUTRAL)
- {
- // Not supposed to be neutral
- throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), nameof(culture));
- }
- if (culture == CultureInfo.LOCALE_CUSTOM_DEFAULT)
- {
- // Not supposed to be neutral
- throw new ArgumentException(SR.Format(SR.Argument_CustomCultureCannotBePassedByNumber, culture), nameof(culture));
- }
-
- _cultureData = CultureData.GetCultureData(culture, true);
- _name = _cultureData.SREGIONNAME;
- if (_cultureData.IsNeutralCulture)
- {
- // Not supposed to be neutral
- throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), nameof(culture));
- }
- }
- internal RegionInfo(CultureData cultureData)
- {
- _cultureData = cultureData;
- _name = _cultureData.SREGIONNAME;
- }
- private void SetName(string name)
- {
- // Use the name of the region we found
- _name = _cultureData.SREGIONNAME;
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // GetCurrentRegion
- //
- // This instance provides methods based on the current user settings.
- // These settings are volatile and may change over the lifetime of the
- // thread.
- //
- ////////////////////////////////////////////////////////////////////////
- public static RegionInfo CurrentRegion
- {
- get
- {
- RegionInfo temp = s_currentRegionInfo;
- if (temp == null)
- {
- temp = new RegionInfo(CultureInfo.CurrentCulture._cultureData);
- // Need full name for custom cultures
- temp._name = temp._cultureData.SREGIONNAME;
- s_currentRegionInfo = temp;
- }
- return temp;
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // GetName
- //
- // Returns the name of the region (ie: en-US)
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string Name
- {
- get
- {
- Debug.Assert(_name != null, "Expected RegionInfo._name to be populated already");
- return (_name);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // GetEnglishName
- //
- // Returns the name of the region in English. (ie: United States)
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string EnglishName
- {
- get
- {
- return (_cultureData.SENGCOUNTRY);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // GetDisplayName
- //
- // Returns the display name (localized) of the region. (ie: United States
- // if the current UI language is en-US)
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string DisplayName
- {
- get
- {
- return (_cultureData.SLOCALIZEDCOUNTRY);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // GetNativeName
- //
- // Returns the native name of the region. (ie: Deutschland)
- // WARNING: You need a full locale name for this to make sense.
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string NativeName
- {
- get
- {
- return (_cultureData.SNATIVECOUNTRY);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // TwoLetterISORegionName
- //
- // Returns the two letter ISO region name (ie: US)
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string TwoLetterISORegionName
- {
- get
- {
- return (_cultureData.SISO3166CTRYNAME);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // ThreeLetterISORegionName
- //
- // Returns the three letter ISO region name (ie: USA)
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string ThreeLetterISORegionName
- {
- get
- {
- return (_cultureData.SISO3166CTRYNAME2);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // ThreeLetterWindowsRegionName
- //
- // Returns the three letter windows region name (ie: USA)
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string ThreeLetterWindowsRegionName
- {
- get
- {
- // ThreeLetterWindowsRegionName is really same as ThreeLetterISORegionName
- return ThreeLetterISORegionName;
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // IsMetric
- //
- // Returns true if this region uses the metric measurement system
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual bool IsMetric
- {
- get
- {
- int value = _cultureData.IMEASURE;
- return (value == 0);
- }
- }
- public virtual int GeoId
- {
- get
- {
- return (_cultureData.IGEOID);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // CurrencyEnglishName
- //
- // English name for this region's currency, ie: Swiss Franc
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string CurrencyEnglishName
- {
- get
- {
- return (_cultureData.SENGLISHCURRENCY);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // CurrencyNativeName
- //
- // Native name for this region's currency, ie: Schweizer Franken
- // WARNING: You need a full locale name for this to make sense.
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string CurrencyNativeName
- {
- get
- {
- return (_cultureData.SNATIVECURRENCY);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // CurrencySymbol
- //
- // Currency Symbol for this locale, ie: Fr. or $
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string CurrencySymbol
- {
- get
- {
- return (_cultureData.SCURRENCY);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // ISOCurrencySymbol
- //
- // ISO Currency Symbol for this locale, ie: CHF
- //
- ////////////////////////////////////////////////////////////////////////
- public virtual string ISOCurrencySymbol
- {
- get
- {
- return (_cultureData.SINTLSYMBOL);
- }
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // Equals
- //
- // Implements Object.Equals(). Returns a boolean indicating whether
- // or not object refers to the same RegionInfo as the current instance.
- //
- // RegionInfos are considered equal if and only if they have the same name
- // (ie: en-US)
- //
- ////////////////////////////////////////////////////////////////////////
- public override bool Equals(object value)
- {
- if (value is RegionInfo that)
- {
- return this.Name.Equals(that.Name);
- }
- return (false);
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // GetHashCode
- //
- // Implements Object.GetHashCode(). Returns the hash code for the
- // CultureInfo. The hash code is guaranteed to be the same for RegionInfo
- // A and B where A.Equals(B) is true.
- //
- ////////////////////////////////////////////////////////////////////////
- public override int GetHashCode()
- {
- return (this.Name.GetHashCode());
- }
- ////////////////////////////////////////////////////////////////////////
- //
- // ToString
- //
- // Implements Object.ToString(). Returns the name of the Region, ie: es-US
- //
- ////////////////////////////////////////////////////////////////////////
- public override string ToString()
- {
- return (Name);
- }
- }
- }
|