Cultures


Cultures are the fundamental building block of internationalization in the .NET Framework. Every issue that you will encounter in this subject has a culture at its heart. As such, this is clearly where we must begin. A culture is a language and, optionally, a region. In the .NET Framework, it is represented by the System.Globalization.CultureInfo class. Typically, a culture is created from a culture string, although we look at other possibilities in Chapter 6. The culture string is specified in the RFC 1766 format:

 languagecode2[-country/regioncode2[-script]] 


languagecode2 is an ISO 639-1 or 639-2 code, country/regioncode2 is an ISO 3166 code, and script is the writing system used to represent text in the country/region (e.g., Latin, Cyrillic). For example, the following code creates a CultureInfo object for the English language:

 CultureInfo cultureInfo = new CultureInfo("en"); 


(CultureInfo is in the System.Globalization namespace, so you must add a "using System.Globalization;" directive to the source.) This object simply represents neutral English, not English in the U.S. or English in the U.K., so it does not represent a specific region. To identify a specific country or region, you extend the string:

 CultureInfo cultureInfo = new CultureInfo("en-GB"); 


This object represents English in the U.K. (written as "English (United Kingdom)" so that the region is in brackets after the language) and all that this includes. For example, the object includes information about the date patterns used in the U.K.:

 string shortDatePattern =   cultureInfo.DateTimeFormat.ShortDatePattern; 


The value assigned to shortDatePattern is "dd/MM/yyyy". Similarly, this next assignment assigns the U.K. pound sign (£) to currencySymbol:

 string currencySymbol = cultureInfo.NumberFormat.CurrencySymbol; 


Here are a few more examples of culture strings:

"en-US" (English in the U.S.)

"en-AU" (English in Australia)

"fr" (French, no specific country)

"fr-FR" (French in France)

"fr-CA" (French in Canada)

"es" (Spanish, no specific country)

"es-ES" (Spanish in Spain)

"es-MX" (Spanish in Mexico)

"sr-SP-Latn" (Serbian in Serbia and Montenegro using the Latin script)

"sr-SP-Cyrl" (Serbian in Serbia and Montenegro using the Cyrillic script)

Typically, the language code is 2 characters (but not always) and the country/region code is 2 characters.

CultureInfo objects are classified as either invariant, neutral, or specific, and operate in the simple hierarchy shown in Figure 3.3.

Figure 3.3. CultureInfo Hierarchy


The invariant culture, represented by an empty string, is intended to represent the absence of any given culture (in reality, the invariant culture has much in common with the English culture, but this is not the same as it being the English culture, and you should not write code that depends upon this). This quality of not being a particular culture makes the invariant culture ideal for streaming data in a culturally-agnostic format. A neutral culture is a representation of a language but is not specific to a particular regionfor example, the "en" culture represents English in general, not English in a specific region. In particular, neutral cultures do not include region-specific globalization information because they are not specific to a region. A specific culture represents a language in a specific regionfor example, the "en-US" culture represents English in the United States and includes region-specific information such as date/time and number formats.

CultureInfo objects have a Parent property so that the "en-US" specific culture knows that its parent is the "en" neutral culturewhich, in turn, knows that its Parent is the invariant culture. This hierarchy is essential for the fallback process described later in this chapter.




.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