15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

namespace system.Globalization ❘ 571<br />

Possibly the most important class in the System.Globalization namespace is CultureInfo. CultureInfo<br />

represents a culture <strong>and</strong> defines calendars, formatting of numbers <strong>and</strong> dates, <strong>and</strong> sorting strings used with<br />

the culture.<br />

The class RegionInfo represents regional settings (such as the currency) <strong>and</strong> shows whether the region<br />

is using the metric system. Some regions can use multiple languages. One example is the region of Spain,<br />

which has Basque (eu-ES), Catalan (ca-ES), Spanish (es-ES), <strong>and</strong> Galician (gl-ES) cultures. Similar to one<br />

region having multiple languages, one language can be spoken in different regions; for example, Spanish is<br />

spoken in Mexico, Spain, Guatemala, Argentina, <strong>and</strong> Peru, to name only a few countries.<br />

Later in this chapter, you see a sample application that demonstrates these characteristics of cultures <strong>and</strong> regions.<br />

specific, neutral, <strong>and</strong> invariant Cultures<br />

When using cultures in the .<strong>NET</strong> Framework, you have to differentiate between three types: specific,<br />

neutral, <strong>and</strong> invariant cultures.<br />

A specific culture is associated with a real, existing culture defined with<br />

RFC 1766, as you saw in the preceding section. A specific culture can be<br />

mapped to a neutral culture. For example, de is the neutral culture of the<br />

specific cultures de-AT, de-DE, de-CH, <strong>and</strong> others. de is shorth<strong>and</strong> for<br />

the German language (Deutsch); AT, DE, <strong>and</strong> CH are shorth<strong>and</strong> for the<br />

countries Austria, Germany, <strong>and</strong> Switzerl<strong>and</strong>, respectively.<br />

When translating applications, it is typically not necessary to do<br />

translations for every region; not much difference exists between the<br />

German language in the countries Austria <strong>and</strong> Germany. Instead of using<br />

specific cultures, you can use a neutral culture to localize applications.<br />

The invariant culture is independent of a real culture. When storing<br />

formatted numbers or dates in files, or sending them across a network to<br />

a server, using a culture that is independent of any user settings is the best<br />

option.<br />

Figure 22-2 shows how the culture types relate to each other.<br />

CurrentCulture <strong>and</strong> CurrentUiCulture<br />

When you set cultures, you need to differentiate between a culture for the user interface <strong>and</strong> a culture<br />

for the number <strong>and</strong> date formats. Cultures are associated with a thread, <strong>and</strong> with these two culture types,<br />

two culture settings can be applied to a thread. The Thread class has the properties CurrentCulture <strong>and</strong><br />

CurrentUICulture. The property CurrentCulture is for setting the culture that is used with formatting<br />

<strong>and</strong> sort options, whereas the property CurrentUICulture is used for the language of the user interface.<br />

Users can change the default setting of the CurrentCulture by using the Regional <strong>and</strong> Language options<br />

in the Windows Control Panel (see Figure 22-3). With this configuration, it is also possible to change the<br />

default number, the time, <strong>and</strong> the date format for the culture.<br />

The CurrentUICulture does not depend on this configuration. The CurrentUICulture setting depends<br />

on the language of the operating system. There is one exception, though: If a multi-language user interface<br />

(MUI) is installed with Windows 7, Windows Vista or Windows XP, it is possible to change the language of<br />

the user interface with the regional configuration, <strong>and</strong> this influences the property CurrentUICulture.<br />

These settings make a very good default, <strong>and</strong> in many cases, there is no need to change the default behavior.<br />

If the culture should be changed, you can easily do this by changing both cultures of the thread to, say, the<br />

Spanish culture, as shown in this code snippet:<br />

System.Globalization.CultureInfo ci = new<br />

System.Globalization.CultureInfo("es-ES");<br />

System.Threading.Thread.CurrentThread.CurrentCulture = ci;<br />

System.Threading.Thread.CurrentThread.CurrentUICulture = ci;<br />

Invariant<br />

figure 22-2<br />

de<br />

en<br />

de-AT<br />

de-DE<br />

de-CH<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!