| | | 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 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace System.Globalization |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// This class represents settings specified by de jure or de facto |
| | | 11 | | /// standards for a particular country/region. In contrast to |
| | | 12 | | /// CultureInfo, the RegionInfo does not represent preferences of the |
| | | 13 | | /// user and does not depend on the user's language or culture. |
| | | 14 | | /// </summary> |
| | | 15 | | public class RegionInfo |
| | | 16 | | { |
| | | 17 | | // Name of this region (ie: es-US): serialized, the field used for deserialization |
| | | 18 | | private string _name; |
| | | 19 | | |
| | | 20 | | // The CultureData instance that we are going to read data from. |
| | | 21 | | private readonly CultureData _cultureData; |
| | | 22 | | |
| | | 23 | | // The RegionInfo for our current region |
| | | 24 | | internal static RegionInfo? s_currentRegionInfo; |
| | | 25 | | |
| | 0 | 26 | | public RegionInfo(string name) |
| | | 27 | | { |
| | 0 | 28 | | ArgumentNullException.ThrowIfNull(name); |
| | | 29 | | |
| | | 30 | | // The InvariantCulture has no matching region |
| | 0 | 31 | | if (name.Length == 0) |
| | | 32 | | { |
| | 0 | 33 | | throw new ArgumentException(SR.Argument_NoRegionInvariantCulture, nameof(name)); |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | // For CoreCLR we only want the region names that are full culture names |
| | 0 | 37 | | _cultureData = CultureData.GetCultureDataForRegion(name, true) ?? |
| | 0 | 38 | | throw new ArgumentException(SR.Format(SR.Argument_InvalidCultureName, name), nameof(name)); |
| | | 39 | | |
| | | 40 | | // Not supposed to be neutral |
| | 0 | 41 | | if (_cultureData.IsNeutralCulture) |
| | | 42 | | { |
| | 0 | 43 | | throw new ArgumentException(SR.Format(SR.Argument_InvalidNeutralRegionName, name), nameof(name)); |
| | | 44 | | } |
| | | 45 | | |
| | 0 | 46 | | _name = _cultureData.RegionName; |
| | 0 | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | public RegionInfo(int culture) |
| | | 50 | | { |
| | | 51 | | // The InvariantCulture has no matching region |
| | 0 | 52 | | if (culture == CultureInfo.LOCALE_INVARIANT) |
| | | 53 | | { |
| | 0 | 54 | | throw new ArgumentException(SR.Argument_NoRegionInvariantCulture); |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | if (culture == CultureInfo.LOCALE_NEUTRAL) |
| | | 58 | | { |
| | | 59 | | // Not supposed to be neutral |
| | 0 | 60 | | throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), nameof(culture)); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | if (culture == CultureInfo.LOCALE_CUSTOM_DEFAULT || culture == CultureInfo.LOCALE_CUSTOM_UNSPECIFIED) |
| | | 64 | | { |
| | | 65 | | // Not supposed to be neutral |
| | 0 | 66 | | throw new ArgumentException(SR.Argument_CustomCultureCannotBePassedByNumber, nameof(culture)); |
| | | 67 | | } |
| | | 68 | | |
| | 0 | 69 | | _cultureData = CultureData.GetCultureData(culture, true); |
| | 0 | 70 | | _name = _cultureData.RegionName; |
| | | 71 | | |
| | 0 | 72 | | if (_cultureData.IsNeutralCulture) |
| | | 73 | | { |
| | | 74 | | // Not supposed to be neutral |
| | 0 | 75 | | throw new ArgumentException(SR.Format(SR.Argument_CultureIsNeutral, culture), nameof(culture)); |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | internal RegionInfo(CultureData cultureData) |
| | | 80 | | { |
| | 0 | 81 | | _cultureData = cultureData; |
| | 0 | 82 | | _name = _cultureData.RegionName; |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// This instance provides methods based on the current user settings. |
| | | 87 | | /// </summary> |
| | | 88 | | public static RegionInfo CurrentRegion |
| | | 89 | | { |
| | | 90 | | get |
| | | 91 | | { |
| | 0 | 92 | | RegionInfo? temp = s_currentRegionInfo; |
| | 0 | 93 | | if (temp == null) |
| | | 94 | | { |
| | 0 | 95 | | temp = new RegionInfo(CultureData.GetCurrentRegionData()); |
| | | 96 | | |
| | | 97 | | // Need full name for custom cultures |
| | 0 | 98 | | temp._name = temp._cultureData.RegionName; |
| | 0 | 99 | | s_currentRegionInfo = temp; |
| | | 100 | | } |
| | 0 | 101 | | return temp; |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Returns the name of the region (ie: en-US) |
| | | 107 | | /// </summary> |
| | | 108 | | public virtual string Name |
| | | 109 | | { |
| | | 110 | | get |
| | | 111 | | { |
| | 0 | 112 | | Debug.Assert(_name != null, "Expected RegionInfo._name to be populated already"); |
| | 0 | 113 | | return _name; |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Returns the name of the region in English. (ie: United States) |
| | | 119 | | /// </summary> |
| | 0 | 120 | | public virtual string EnglishName => _cultureData.EnglishCountryName; |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Returns the display name (localized) of the region. (ie: United States |
| | | 124 | | /// if the current UI language is en-US) |
| | | 125 | | /// </summary> |
| | 0 | 126 | | public virtual string DisplayName => _cultureData.LocalizedCountryName; |
| | | 127 | | |
| | | 128 | | /// <summary> |
| | | 129 | | /// Returns the native name of the region. (ie: Deutschland) |
| | | 130 | | /// WARNING: You need a full locale name for this to make sense. |
| | | 131 | | /// </summary> |
| | 0 | 132 | | public virtual string NativeName => _cultureData.NativeCountryName; |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Returns the two letter ISO region name (ie: US) |
| | | 136 | | /// </summary> |
| | 0 | 137 | | public virtual string TwoLetterISORegionName => _cultureData.TwoLetterISOCountryName; |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Returns the three letter ISO region name (ie: USA) |
| | | 141 | | /// </summary> |
| | 0 | 142 | | public virtual string ThreeLetterISORegionName => _cultureData.ThreeLetterISOCountryName; |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Returns the three letter windows region name (ie: USA) |
| | | 146 | | /// </summary> |
| | 0 | 147 | | public virtual string ThreeLetterWindowsRegionName => ThreeLetterISORegionName; |
| | | 148 | | |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Returns true if this region uses the metric measurement system |
| | | 152 | | /// </summary> |
| | 0 | 153 | | public virtual bool IsMetric => _cultureData.MeasurementSystem == 0; |
| | | 154 | | |
| | 0 | 155 | | public virtual int GeoId => _cultureData.GeoId; |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// English name for this region's currency, ie: Swiss Franc |
| | | 159 | | /// </summary> |
| | 0 | 160 | | public virtual string CurrencyEnglishName => _cultureData.CurrencyEnglishName; |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Native name for this region's currency, ie: Schweizer Franken |
| | | 164 | | /// WARNING: You need a full locale name for this to make sense. |
| | | 165 | | /// </summary> |
| | 0 | 166 | | public virtual string CurrencyNativeName => _cultureData.CurrencyNativeName; |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Currency Symbol for this locale, ie: Fr. or $ |
| | | 170 | | /// </summary> |
| | 0 | 171 | | public virtual string CurrencySymbol => _cultureData.CurrencySymbol; |
| | | 172 | | |
| | | 173 | | /// <summary> |
| | | 174 | | /// ISO Currency Symbol for this locale, ie: CHF |
| | | 175 | | /// </summary> |
| | 0 | 176 | | public virtual string ISOCurrencySymbol => _cultureData.ISOCurrencySymbol; |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Implements Object.Equals(). Returns a boolean indicating whether |
| | | 180 | | /// or not object refers to the same RegionInfo as the current instance. |
| | | 181 | | /// RegionInfos are considered equal if and only if they have the same name |
| | | 182 | | /// (ie: en-US) |
| | | 183 | | /// </summary> |
| | | 184 | | public override bool Equals([NotNullWhen(true)] object? value) |
| | | 185 | | { |
| | 0 | 186 | | return value is RegionInfo otherRegion |
| | 0 | 187 | | && Name.Equals(otherRegion.Name); |
| | | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | public override int GetHashCode() => Name.GetHashCode(); |
| | | 191 | | |
| | 0 | 192 | | public override string ToString() => Name; |
| | | 193 | | } |
| | | 194 | | } |
| | | 195 | | |