Using the CultureInfo Class

The CultureInfo class provides culture-specific information about the locale of the operating system running your application. To give you an idea of what the CultureInfo class does, you can look at your own computer's regional settings.

In the Control Panel, the Regional and Language Settings applet is the place to view and change how regional information should be displayed on your computer. This includes date, time, currency, currency symbol information, as well as your keyboard layout.

Figure 15.1 demonstrates the Regional and Language Settings dialog box for my computer.

Figure 15.1. Regional settings for English (United States).


Because I installed Windows XP and I live in Florida, the locale for my computer is English (United States). I could have changed this to whatever I wanted during installation of Windows XP, or I could go to the Control Panel and modify it. Because the locale is en-US, the standard date, time, and currency formats are used throughout Windows.

If I want to see what the regional information would look like in another locale, I can change the settings to another locale in the Control Panel. Figure 15.2 shows I'm changing the locale to Tatar.

Figure 15.2. Modifying my locale to Tatar.


After the locale is changed, Windows takes on a new look for the regional settings of Tatar. If I look at my Outlook Calendar, it doesn't display the months and times as it would in the United States—it displays them as it would for Tatar. Figure 15.3 shows Outlook after changing my locale to Tatar.

Figure 15.3. Outlook Calendar after changing the locale to Tatar.


As you can see, Outlook is written with globalization in mind. The dates and months aren't specific to any region; they're determined by the locale settings in the Control Panel. This is how you should write applications that can span the globe. The CultureInfo class gives you the flexibility to do this.

In Windows and the .NET Framework's System.Globalization namespace, there are predefined culture names that comply with the RFC 1766 standard languagecode-Country/regioncode format for determining the culture you're working in. To make sense of this, look at the compact version of CultureInfo names and language-country/region combinations listed in Table 15.1.

Table 15.1. CultureInfo Names and Region Codes
Culture NameLanguage-Country/Region
“” (empty string)Invariant culture
afAfrikaans
af-ZAAfrikaans - South Africa
sqAlbanian
sq-ALAlbanian - Albania
arArabic
ar-DZArabic - Algeria
ar-BHArabic - Bahrain
ar-EGArabic - Egypt
ar-QAArabic - Qatar
ar-SAArabic - Saudi Arabia
ar-SYArabic - Syria
ar-TNArabic - Tunisia
ar-AEArabic - United Arab Emirates
ar-YEArabic - Yemen
hyArmenian
hy-AMArmenian - Armenia
azAzeri
Cy-az-AZAzeri (Cyrillic) - Azerbaijan
Lt-az-AZAzeri (Latin) - Azerbaijan
euBasque
eu-ESBasque - Basque
beBelarusian
be-BYBelarusian - Belarus
bgBulgarian
bg-BGBulgarian - Bulgaria
caCatalan
taTamil
ta-INTamil - India
ttTatar
tt-RUTatar - Russia
teTelugu
te-INTelugu - India
thThai
th-THThai - Thailand
trTurkish
tr-TRTurkish - Turkey
ukUkrainian
uk-UAUkrainian - Ukraine
urUrdu
ur-PKUrdu - Pakistan
uzUzbek
Cy-uz-UZUzbek (Cyrillic) - Uzbekistan
Lt-uz-UZUzbek (Latin) - Uzbekistan
viVietnamese
vi-VNVietnamese - Vietnam

Table 15.1 should give you a good idea of where this is going. Every possible culture code is available for you to use. Later, you're going to write code to actually list them all.

Understanding that you have access to specific cultures isn't enough to write global code. You must use the methods and properties of the CultureInfo class to make sure that you're displaying the correct information on your forms. Table 15.2 lists the common properties available to you in the CultureInfo class, and Table 15.3 lists the common methods that you can take advantage of when using the CultureInfo class.

Table 15.2. Common Properties of the CultureInfo Class
PropertyDescription
CalendarGets the default calendar used by the culture
CompareInfoGets the CompareInfo that defines how to compare strings for the culture
CurrentCultureGets the CultureInfo that represents the culture used by the current thread
CurrentUICultureGets the CultureInfo that represents the current culture used by the ResourceManager to look up culture-specific resources at runtime
DateTimeFormatGets or sets a DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times
DisplayNameGets the culture name in the format "<languagefull> (<country/regionfull>)" in the language of the localized version of .NET Framework
EnglishNameGets the culture name in the format "<languagefull> (<country/regionfull>)" in English
InstalledUICultureGets the CultureInfo that represents the culture installed with the operating system
InvariantCultureGets the CultureInfo that's culture-independent (invariant)
IsNeutralCultureGets a value indicating whether the current CultureInfo represents a neutral culture
IsReadOnlyGets a value indicating whether the current CultureInfo is read-only
LCIDGets the culture identifier for the current CultureInfo
NameGets the culture name in the format "<languagecode2>-<country/regioncode2>"
NativeNameGets the culture name in the format "<languagefull> (<country/regionfull>)" in the language that the culture is set to display
NumberFormatGets or sets a NumberFormatInfo that defines the culturally appropriate format of displaying numbers, currency, and percentages
OptionalCalendarsGets the list of optional calendars that can be used by the culture
ParentGets the CultureInfo that represents the parent culture of the current CultureInfo
TextInfoGets the TextInfo that defines the writing system associated with the culture
ThreeLetterISOLanguageNameGets the ISO 639-2 three-letter code for the language of the current CultureInfo
ThreeLetterWindowsLanguageNameGets the three-letter code for the language as defined in the Windows API
TwoLetterISOLanguageNameGets the ISO 639-1 two-letter code for the language of the current CultureInfo
UseUserOverrideGets a value indicating whether the current CultureInfo uses the user-selected culture settings

Table 15.3. Common Methods of the CultureInfo Class
MethodDescription
ClearCachedDataRefreshes cached culture-related information
CloneCreates a copy of the current CultureInfo
CreateSpecificCultureCreates a CultureInfo that represents the specific culture that's associated with the specified name
GetCulturesGets the list of supported cultures filtered by the specified CultureTypes
GetFormatGets an object that defines how to format the specified type
ReadOnlyReturns a read-only wrapper around the specified CultureInfo
ToStringOverridden; returns a string containing the name of the current CultureInfo in the format "<languagecode2>-<country/ regioncode2>"

As you can see, the whole idea of the CultureInfo class is to give you the ability write global applications. To see how you can use this in code, you're going to write a Culture Info Browser application that uses the methods and properties of the CultureInfo class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.157.142