15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

576 ❘ ChaPTer 22 lOcAlizAtiOn<br />

Next you get the calendar information about the culture. The Calendar property of the CultureInfo class<br />

returns the default Calendar object for the specific culture. Because the Calendar class doesn’t have a<br />

property to tell its name, you use the ToString() method of the base class to get the name of the class, <strong>and</strong><br />

remove the namespace of this string to be displayed in the text field textCalendar.<br />

Because a single culture might support multiple calendars, the OptionalCalendars property returns an<br />

array of additional supported Calendar objects. These optional calendars are displayed in the list box<br />

listCalendars. The GregorianCalendar class that derives from Calendar has an additional property called<br />

CalendarType that lists the type of the Gregorian calendar. This type can be a value of the enumeration<br />

GregorianCalendarTypes: Arabic, MiddleEastFrench, TransliteratedFrench, USEnglish, or<br />

Localized, depending on the culture. With Gregorian calendars, the type is also displayed in the list box:<br />

// default calendar<br />

textCalendar.Text = ci.Calendar.ToString().<br />

Remove(0, 21).Replace("Calendar", "");<br />

// fill optional calendars<br />

listCalendars.Items.Clear();<br />

foreach (Calendar optCal in ci.OptionalCalendars)<br />

{<br />

StringBuilder calName = new StringBuilder(50);<br />

calName.Append(optCal.ToString());<br />

calName.Remove(0, 21);<br />

calName.Replace("Calendar", "");<br />

}<br />

// for GregorianCalendar add type information<br />

GregorianCalendar gregCal = optCal as GregorianCalendar;<br />

if (gregCal != null)<br />

{<br />

calName.AppendFormat(" {0}", gregCal.CalendarType.ToString());<br />

}<br />

listCalendars.Items.Add(calName.ToString());<br />

code snippet CultureDemo/MainWindow.xaml.cs<br />

Next, you check whether the culture is a specific culture (not a neutral culture) by using !ci<br />

.IsNeutralCulture in an if statement. The method ShowSamples() displays number <strong>and</strong> date samples.<br />

This method is implemented in the next code section. The method ShowRegionInformation() is used to<br />

display some information about the region. With the invariant culture, you can display only number <strong>and</strong><br />

date samples, but no region information. The invariant culture is not related to any real language,<br />

<strong>and</strong> therefore it is not associated with a region:<br />

// display number <strong>and</strong> date samples<br />

if (!ci.IsNeutralCulture)<br />

{<br />

groupSamples.IsEnabled = true;<br />

ShowSamples(ci);<br />

// invariant culture doesn't have a region<br />

if (String.Compare(ci.ThreeLetterISOLanguageName, "IVL", true) == 0)<br />

{<br />

groupRegion.IsEnabled = false;<br />

}<br />

else<br />

{<br />

groupRegion.IsEnabled = true;<br />

ShowRegionInformation(ci.Name);<br />

}<br />

}<br />

else // neutral culture: no region, no number/date formatting<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!