The RegionInfo Class


As you know, the CultureInfo class can relate to a language by itself or a language in a country/region. However, it cannot relate to just a country/region alone. This is the purpose of the RegionInfo class. The RegionInfo class describes a country/region regardless of its language. This can be especially useful in Country combo boxes that allow you to select your country from a list of all countries, although it is worth pointing out that a region does not always have a one-to-one mapping with a country. Hong Kong, for example, is a region but not a country. RegionInfo supports two constructors: One accepts an LCID (locale ID), and the other accepts a region code or culture code. It should be noted, however, that an LCID refers to a specific language in a specific country; thus, there is often more than one LCID that refers to the same country/region. The CultureInfo class does not have a Region property that identifies the culture's region, but one can easily be created by either of the following lines:

 RegionInfo regionInfo =     new RegionInfo(Thread.CurrentThread.CurrentCulture.Name); RegionInfo regionInfo =     new RegionInfo(Thread.CurrentThread.CurrentCulture.LCID); 


Of the two choices, the constructor that accepts a name is the safer of the two. RegionInfo objects cannot be constructed from LCIDs of custom cultures (see Chapter 11) because supplementary custom cultures all have the same LCID.

The RegionInfo class does not have a GeTRegions method corresponding to the CultureInfo's GetCultures method, but the following Getregions method provides the same results. (If you are using the .NET Framework 1.1, replace culture Info.Name with cultureInfo.LCID.)

 public static RegionInfo[] GetRegions() {     Hashtable regionInfos = new Hashtable();     foreach (CultureInfo cultureInfo in         CultureInfo.GetCultures(CultureTypes.SpecificCultures))     {         RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);         if (regionInfos[regionInfo.ThreeLetterISORegionName] == null)             regionInfos.Add(                 regionInfo.ThreeLetterISORegionName, regionInfo);     }     RegionInfo[] regionInfoArray = new RegionInfo[regionInfos.Count];     regionInfos.Values.CopyTo(regionInfoArray, 0);     return regionInfoArray; } 


The GeTRegions method uses the CultureInfo.GetCultures method to get a list of specific cultures (because neutral cultures do not have a country/region) and creates a new RegionInfo from the culture's name (or the culture's LCID in the .NET Framework 1.1). As there can be many cultures that refer to the same country/region, we search the Hashtable to see if the country/region is already in the list by looking for a country/region with the same ThreeLetterISORegionName.

The RegionInfo properties are listed in Table 6.3. Notice that the static Current Region property refers to the value retrieved by the Win32 GetUserDefaultLCID API, which is the value set by the user in the Regional and Language Options dialog. This property is not affected by changes to the CurrentCulture or CurrentUICulture. Also note that the DisplayName property gets its resources from the .NET Framework Language Pack; whereas for Windows-only cultures, the NativeName property gets it resources from the operating system. If you do not have an appropriate .NET Framework Language Pack installed, you will get a mismatch if the language is not English. Finally, notice the inadequacies of the Boolean type in the IsMetric property. This property indicates whether the country/region uses the metric system. There are no shades of gray in the Boolean type, so the United States and the United Kingdom report False and TRue, respectively, where neither is entirely correct nor entirely incorrect.

Table 6.3. RegionInfo Properties

Property Name

Description

.NET 1.1

.NET 2.0

CurrencyEnglishName

The English name of the currency

No

Yes

CurrencyNativeName

The native name of the currency

No

Yes

CurrencySymbol

The currency symbol

Yes

Yes

CurrentRegion

The region set in Regional and Language Options when the application started

Yes

Yes

DisplayName

The localized region name (from the .NET Language Pack for framework cultures)

Yes

Yes

EnglishName

The English region name

Yes

Yes

GeoId

A unique number identifying the geographical region

No

Yes

IsMetric

Whether the region uses the metric system

Yes

Yes

ISOCurrencySymbol

The three-character ISO 4217 currency symbol

Yes

Yes

Name

The name of the region

Yes

Yes

NativeName

The localized region name (from the operating system for Windows-only cultures)

No

Yes

ThreeLetterISORegionName

The three-letter ISO 3166 code

Yes

Yes

THReeLetterISOWindowsName

The three-letter Windows code

Yes

Yes

TwoLetterISORegionName

The two-letter ISO 3166 code

Yes

Yes





.NET Internationalization(c) The Developer's Guide to Building Global Windows and Web Applications
.NET Internationalization: The Developers Guide to Building Global Windows and Web Applications
ISBN: 0321341384
EAN: 2147483647
Year: 2006
Pages: 213

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net