C#: Globalization and localization

download C#: Globalization and localization

of 24

  • date post

  • Category


  • view

  • download


Embed Size (px)


This hel

Transcript of C#: Globalization and localization

  • 1. Introduction Globalization is about internationalizing applications:the application supports number and date formatsdepending on the culture. Localization is about translating applications forspecific cultures.

2. Globalization (Internationalization) Its is a process of creating an application thatsupports localized user interfaces.Localization = translation It is the process of translating the applicationsinto different languages. 3. If you plan on distributing your application to aninternational audience, there are a number of things youllneed to keep in mind during the design and developmentphases. Even if you dont have such plans, a small effort upfront can make things considerably easier should yourplans change in future versions of your application.Services built into the .NET Framework make it easy todevelop a single application that can adapt to differentlocales using managed development with Visual Basic orVisual C#. Visual Studio .NET was designed from the start to makedeveloping for an international audience easy by takingadvantage of services built into the .NET Framework. Thefollowing topics will help introduce you to theinternationalization features built into Visual Studio .NET. 4. One Code Base 5. Cultures and Regions The world is divided into multiple cultures and regions, and applications have to beaware of these cultural and regional differences. A culture is a set of preferences basedon a users language and cultural habits. Some examples are en-AU, en-CA, en-GB, and en-US for the English language in Australia, Canada, United Kingdom, and the United States. While one region has multiple languages, one language can be spoken in differentregions; for example, Spanish is spoken in Mexico, Spain, Guatemala, Argentina, andPeru, to name but a few. 6. The most important class in the System.Globalizationnamespace is the class CultureInfo. CultureInfo represents a culture and defines calendars,formatting of numbers and dates, and sorting strings thatare used with the culture. The class RegionInfo represents regional settings (suchas the currency) and shows if the region is using themetric system. In the same region, you can use multiplelanguages. One example is the region of Spain with its Basque (eu-ES), Catalan (ca-ES), Spanish (es-ES), Galician (gl-ES)cultures. 7. Types of Culture Invarient Neutral Specific 8. Invariant Culture Should be used for storing data in a cultureindependent way Should not be used for User Interface elements Has no country/regionEg:CultureInfo ci = CultureInfo.InvariantCulture 9. Neutral Culture Associated with a language not with country/region Can be used for UI related options Cannot be used for retrieving information such asdate/time formatting Specified using format: Arabic ar Exceptions zh-CHT, zh-CHEg.using System.Globalization;CultureInfo ci = new CultureInfo(fr); 10. Specific Culture Associated with Language and a Country/Region fr Neutral Culture fr-FR Specific Culture Provides additional information about the date, time,currency and number formatting options Can be used wherever a Neutral culture is used, theopposite is not trueEg.using System.Globalization;CultureInfo ci = new CultureInfo(fr-FR); 11. Formatting and Parsing Formatting Standard formatting characters Custom formatting characters Parsing Configurable with styles TryParse no conversion exceptions ParseExact for date/time valuesFormatting Culture-appropriate Type Object ParsingString 12. Numeric Formats Store as binary data type if possible Integer, decimal, floating-point Invariant storage as text Format using CultureInfo.InvariantCulture Use standard format character R (reversible) for floating pointnumbers 13. Currenciesdecimal dec = decimal.Parse("$1000000.23",NumberStyles.Currency,CultureInfo.CurrentCulture);System.Console.WriteLine("{0:C}", dec);Output: $1,000,000.23 Preferably store as decimal with meta data Culture DateTime Use 3rd party servicefor conversion When storing as text use invariant culture Reversible text floating-point format Currency text format: 1,000,000.23 14. System.GlobalizationNamespace Includes classes for functionality such as: Culture-aware string comparison Cot vs. Cte (culture dependent) Date & Time formatting yy/mm/dd vs. dd/mm/yy Numeric formatting 12,000.00 vs. 12.000,00 Calendars Gregorian and non-Gregorian 15. CultureInfo class provides culture specific information Controls date comparisonstring comparison number comparison etc CultureInfo userculture=Thread.CurrentThread.CurrentCulture (Used for calculation and Internal Manipulation) CultureInfo userculture=Thread.CurrentThread.CurrentUICulture (Used for DisplayPurpose) 16. CurrentCulture Thread Date and number formatting String comparison and casing It determines the results of culture dependant functions. You can define the CurrentCulture object with specificcultures and not with neutral cultures. 17. CurrentUICulture Thread It determines which resources are loaded by the ResourceManager if you have provided resources in multiplelanguages.Because this controls only which language is used you can define CurrentUICulture with either neutral or specific cultures. 18. Changing the CultureprogrammaticallyCultureInfo ci = new CultureInfo(culture); // set culture for formattingThread.CurrentThread.CurrentCulture = ci;// set culture for resourcesThread.CurrentThread.CurrentUICulture = ci; 19. Implementing localization When the Localizable property is set to true, theresource file resX is generated form the form.BookOfTheDay.resX change the Language property of the form and theproperties of some form elements, a new resource fileis generated for the specified language.BookOfTheDayForm.de.resX 20. Culture-aware Classes Any API which takes a culture, or anIFormatProvider Culture-sensitive by default Examples: System.Globalization.CompareInfo System.Globalization.StringInfo System.Globalization.Calendar System.Resources System.DateTime System.String 21. Calendar Classes Includes support for: Gregorian Calendar Hebrew Calendar Hijiri Calendar Japanese Calendar Julian Calendar Korean Calendar Taiwan Calendar Thai Buddhist Calendar Base Calendar class from which custom calendars can bederived 22. DateTime Provides methods that enable culture-sensitiveoperations on a DateTime. Use the DateTimeFormatInfo Class to format anddisplay a DateTime based on culture. DateTimeFormatInfo Defines how DateTime values are formatted anddisplayed, depending on the culture. 23. CompareInfo Provides a set of methods that can be used to perfomr culture-sensitive string comparisons The CultureInfo class has a CompareInfo property that is an instance of the CompareInfo class The String.Compare method uses the information in the CultureInfo.CompareInfo property to compare strings NumberFormatInfo Defines how currency, decimal separator and other numeric symbols are formatted and displayed based on culture.