0% found this document useful (0 votes)
62 views3 pages

Chap 16 Globalization

This document introduces globalization in .NET Framework 2.0 applications. It discusses using the CultureInfo class to get information about the current culture like formatting for numbers, dates, and strings. The CultureInfo class represents a specific culture and provides properties for the current culture, display name, and UI culture. It also describes culture categories like invariant, neutral, and specific cultures.

Uploaded by

elwa3er
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views3 pages

Chap 16 Globalization

This document introduces globalization in .NET Framework 2.0 applications. It discusses using the CultureInfo class to get information about the current culture like formatting for numbers, dates, and strings. The CultureInfo class represents a specific culture and provides properties for the current culture, display name, and UI culture. It also describes culture categories like invariant, neutral, and specific cultures.

Uploaded by

elwa3er
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapitre 1- Introduction aux technologies J2EE

70-536 Chapitre 16

Plan

„ Lesson 1 : Using Culture Information


.NET Framework 2.0
Application Development
Foundation
„ Lesson 2 : Creating a Custom Culture
Chapitre 16

Globalization

Mohamed Romdhani
INSAT, Octobre 2007 2
M. Romdhani, Décembre 2007

70-536 Chapitre 16 70-536 Chapitre 16

Introduction to globalization

„ The geographical scope of applications is steadily increasing. The


costs of not knowing who will be using the applications are increasing
as well.
„ Dealing with issues as an afterthought is always more costly than
designing for them, and building applications for different locales is no
different in this respect !

Lesson 1 : Using Culture Information „ The .NET Framework 2.0 has an incredibly feature rich set of tools that
can be used to create applications that will run in dispersed
geographical regions.
„ An application can be written that will function equally well in Japan as it
will in Great Britain, with virtually no changes needed to the code. To
accomplish such tasks, the System.Globalization namespace is
provided.

M. Romdhani, Décembre 2007 3 M. Romdhani, Décembre 2007 4

70-536 Chapitre 16 70-536 Chapitre 16

CultureInfo Class CultureInfo Class


„ One of the core tools for manipulating and retrieving information about
the cultural context an application is running in is the CultureInfo „ To detect a user's current culture information, use the CurrentCulture property
class. of the executing thread's CurrentThread property, as shown in the following
code sample:
„ This class provides culture-specific information such as the format of CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
numbers and dates, and casing conventions. Console.WriteLine("The current culture of this application is : " +
„ More broadly, it represents the name of a culture, the culture's writing UsersCulture.Name);
Console.WriteLine("The Display Name of this application is : " +
system, the culture's calendar, the culture's language and UsersCulture.DisplayName);
sublanguages if applicable, and the country and region of the culture, Console.WriteLine("The Native Name of this application is : " +
and it provides methods to manipulate all these aspects. UsersCulture.NativeName);
„ The basic uses of the CultureInfo class are shown in the following list: Console.WriteLine("The ISO Abbreviation of this application is : " +
UsersCulture.TwoLetterISOLanguageName);
„ Control how string comparisons are performed.
„ Sample Result on an american workstation
„ Control how number comparisons and formats are performed.
„ Control how date comparisons and formats are performed.
„ Control how resources are retrieved and used.

„ As a rule, a culture will be grouped into one of three categories:


1. an invariant culture (culture category is culture-insensitive ),
2. a neutral culture (a neutral culture is associated with a language but has no „ Another important feature is the CurrentUICulture property of the CultureInfo
relationship to countries or regions ) class. Although this property is often the same as the CurrentCulture property of
3. or a specific culture (the most precise of the three categories and is the CultureInfo class, they can and very well might be different.
represented by a neutral culture, a hyphen, and then a specific culture „ Hence, one culture can be used for calculations and internal manipulation and another
abbreviation. For instance, in the designations "fr-FR") . can be used for display purposes. The CurrentUICulture of the application can be
accessed via the CurrentUICulture property of the CurrentThread class.
M. Romdhani, Décembre 2007 5 M. Romdhani, Décembre 2007 6

1
Chapitre 1- Introduction aux technologies J2EE

70-536 Chapitre 16 70-536 Chapitre 16

CultureInfo Class CultureTypes Enumeration


„ CultureTypes is an enumeration marked with the FlagsAttribute attribute, which
„ Best Practice : CurrentUICulture must be set at the application's startup ! allows users to specify multiple values.
„ The following example illustrates how to use the GetCultures method, specifying the
„ Setting the current culture is similar to retrieving it. SpecificCultures value of the CultureType enumeration
foreach (CultureInfo UsersCulture in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) {
„ CurrentCulture is a property of the CurrentThread property of the Thread Console.WriteLine("Culture: " + UsersCulture.Name);}
class.
„ Example „ The CultureTypes Enumeration
CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
Console.WriteLine("The current culture of this application is : " +
UsersCulture.Name);
//Change this to Spanish/Venezuela
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-VE");
Console.WriteLine("The current culture of this application is : " +
Thread.CurrentThread.CurrentCulture);

M. Romdhani, Décembre 2007 7 M. Romdhani, Décembre 2007 8

70-536 Chapitre 16 70-536 Chapitre 16


DateTimeFormatInfo and
RegionInfo Class NumberFormatInfo Classes
„ Although the CultureInfo class provides fairly detailed information „ When dealing with applications that cross cultural boundaries, uninitiated
about a given culture, more granular information is sometimes needed. developers might make a few incorrect assumptions concerning date
The RegionInfo is one mechanism to provide more detailed formats, number formats, currency values thinking that they will be the
information. same across boundaries.
„ In short, the RegionInfo class provides specific information about a
particular country or region. „ The DateTimeFormatInfo class and the NumberFormatInfo class provide
„ The CultureInfo has two types of property that will allow it to work in mechanisms for manipulating how these differences are handled. The
conjunction with the RegionInfo class: nominal identifiers (such as the CultureInfo class has properties that provide information about a specific
Name property) and numeric identifiers (such as LCID). Either culture.
approach will have the same ultimate result: „ The DateTimeFormatInfo class provides a comprehensive set of methods
CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture; and properties to handle and respond to the dates of different cultures.
RegionInfo DemoRegion = new RegionInfo(UsersCulture.LCID); Examine the following code:
Or:
RegionInfo DemoRegion = new RegionInfo(UsersCulture.Name); CultureInfo usersCulture = new CultureInfo("es-VE");
String[] Days = usersCulture.DateTimeFormat.DayNames;
„ Afterward, any of the desired properties can be accessed directly just foreach (String Day in Days) {
like any other class: Console.WriteLine("Day Name for Venezuelan Spanish : " + Day);
}
CultureInfo UsersCulture = Thread.CurrentThread.CurrentCulture;
RegionInfo DemoRegion = new RegionInfo(UsersCulture.LCID);
Console.WriteLine("English Name: " + DemoRegion.EnglishName);
Console.WriteLine("Display Name: " + DemoRegion.DisplayName);
Console.WriteLine("Currency Symbol: " + DemoRegion.CurrencySymbol);

M. Romdhani, Décembre 2007 9 M. Romdhani, Décembre 2007 10

70-536 Chapitre 16 70-536 Chapitre 16


DateTimeFormatInfo and Using the CompareInfo Class and CompareOptions
NumberFormatInfo Classes Enumeration for Culturally Aware Comparisons

„ One important reason to use the CultureInfo class is to allow for culturally
„ If one culture has a date format in mm/dd/yyyy format and the other stores aware string comparisons.
the values in dd/mm/yyyy format, what would happen if you tried to
compare them? „ To do this, the CultureInfo class has a CompareInfo property, which is an
instance of the CompareInfo class. You can create a new instance of the
„ Chances are that you'd end up with erroneous comparisons. However, by CompareInfo class or, more practically, you can set it to the CompareInfo
specifying that the dates are from different cultures, comparisons will work property of the CurrentCulture.
correctly. CompareInfo DemoInfo = Thread.CurrentThread.CurrentCulture.CompareInfo;
„ The same problem exists when dealing with numbers and currency, and „ The most basic approach to using the CompareInfo class is to call the
essentially the same solution is in place—using the NumberFormatInfo Compare method, passing in the values to be compared. The following code
class instead of the DateFormatInfo class. This can be easily illustrated by illustrates comparing two words for equivalency using the Compare method
using the same code we used previously, but with a slight modification: of the CompareInfo class:
CultureInfo UsersCulture = new CultureInfo("es-VE"); String FirstString = "Coté";
Console.WriteLine("Venezuelan Currency Symbol: " + String SecondString = "coté";
sersCulture.NumberFormat.CurrencySymbol); CompareInfo DemoInfo = new CultureInfo("fr-FR").CompareInfo;
Console.WriteLine("Number Decimal Symbol: " + DemoInfo.Compare(FirstString, SecondString);
UsersCulture.NumberFormat.NumberDecimalSeparator);
If you run this code, the two strings will not be considered equal.
However, you can use options from the "CompareOptions Enumeration" to
The output from this code, will show that Venezuelan currency ignore case for example, etc…
abbreviation is "Bs" and a comma is used as the decimal separator DemoInfo.Compare(FirstString, SecondString, CompareOptions.IgnoreCase);
when writing numbers.
If you run this code, the two strings will be considered equal.

M. Romdhani, Décembre 2007 11 M. Romdhani, Décembre 2007 12

2
Chapitre 1- Introduction aux technologies J2EE

70-536 Chapitre 16 70-536 Chapitre 16

Lesson 1 Summary
„ Cultures are represented by the CultureInfo class.

„ The current culture of an application can be retrieved by examining the


CurrentCulture property of the CultureInfo class. The current culture of
the application can be changed by simply setting the CurrentCulture
property to another culture.
„ The CurrentCulture property of the CurrentThread class is used to both
retrieve and set culture information for the current application.

„ The CultureInfo class is the primary vehicle used to manipulate cultural


information.

„ The CurrentUICulture property of the CurrentThread class is used to


retrieve and set culture information about visual aspects of the
application. Unlike the CurrentCulture property, this property should be
set only when the application starts or in an object's constructor.

„ The NumberFormatInfo class provides a mechanism for displaying how


numbers are represented and displayed.

„ The DateFormatInfo class provides a mechanism for displaying how


dates are represented and displayed.

M. Romdhani, Décembre 2007 13 M. Romdhani, Décembre 2007 14

70-536 Chapitre 16 70-536 Chapitre 16

Lab: Create Your Own Culture


„ In this lab, you'll create a culture of your own. If you encounter a
problem completing an exercise, the completed projects are available
on the companion CD in the Code folder.
1. Open Visual Studio 2005.
2. Create a new console application in Microsoft C# 2005 or in Microsoft
Visual Basic 2005. Visual Basic 2005 creates a module for you, along
with an empty Main procedure. Visual C# creates a Program.cs class
for you with an empty Main procedure.
Lesson 2 : Creating a Custom Culture 3. Make sure that the System and System.Globalization namespaces are
referenced by the project.
4. Add a reference to sysglobl.dll, as described earlier in this chapter.
5. Within the Main procedure, add the following code:
6. Add a NumberFormatInfo object, and set the properties as illustrated in
the following code:
7. Add a DateTimeFormatInfo object, and set the properties as illustrated
in the following code:
8. Build the program, fix any errors, and then run the application.
9. Use the CultureInfo class to interrogate the properties of your new
culture.

M. Romdhani, Décembre 2007 15 M. Romdhani, Décembre 2007 16

70-536 Chapitre 16 70-536 Chapitre 16

Lesson 2 Summary Chapter 16 Summary


„ The .NET Framework provides the System.Globalization namespace to give
„ Custom cultures can be created with the CultureAndInfoRegionBuilder applications the ability to run seamlessly in multiple locales.
class.
„ The NumberFormatInfo class provides a mechanism to retrieve and manipulate
„ The culture of a CultureAndRegionInfoBuilder class can be inherited number and currency formatting.
from a parent setting.
„ The DateTimeFormatInfo class provides a mechanism to retrieve and
manipulate date and time formatting.
„ The region of a CultureAndRegionInfoBuilder class can also be
inherited from a parent setting.
„ To effectively implement globalization, data must be formatted instead of being
hard coded. (The first method shown in the following code samples should be
„ The number formatting of custom classes can be manipulated through avoided, while the second method should be used.)
the NumberFormat property of the CultureAndRegionInfoBuilder class. „ String UsersCulture = "$20.00";
„ String DollarValue = (20).ToString("C");

„ Custom cultures can be created with the CultureAndInfoRegionBuilder class.

„ The number formatting of custom classes can be manipulated through the


NumberFormat property of the CultureAndRegionInfoBuilder class.

„ The date and time formatting of a custom class can be manipulated through the
GregorianDateTimeFormat property of the CultureAndRegionInfoBuilder class.

M. Romdhani, Décembre 2007 17 M. Romdhani, Décembre 2007 18

You might also like