Published on August 23, 2007
Globalization Support in Microsoft .NET Framework : Globalization Support in Microsoft .NET Framework François Liger Program Manager Microsoft Corporation What is .NET Framework?: What is .NET Framework? .NET is Microsoft's platform for building, deploying, operating and integrating XML Web services. Rich XML, standard protocols, stateless Cross-language development Common language runtime Common type system for all languages Rich runtime environment Rich class libraries (.NET Framework) Base class libraries, ADO.NET and XML Windows Forms for rich, Win32 applications Web application platform ASP.NET Easier to deploy, run, & maintain applications For components, versioning, availability Key Terms (as used in this talk): Key Terms (as used in this talk) Globalization (a.k.a. Internationalization) Core application handles international data Character encodings Date and time, numeric, currency formats … Market adaptation Additional functionality for a given market Localization = translation Localizable = ready for translation Resources Application elements (error messages, UI) to be translated in localized versions Why include international support in the Framework ?: Why include international support in the Framework ? Consistency .NET Framework is Unicode internally .NET Framework carries NLS+ classes, sorting tables Consistent results on Windows XP, Windows 2000, Windows NT 4, Windows Millennium, Windows 98 Note: there are a few limitations on down-level Unicode support: Underlying operating system limitations Windows Code pages File I/O .NET Framework Namespace: System System.Data System.Xml System.Web Globalization Diagnostics Configuration Collections Resources Reflection Net IO Threading Text ServiceProcess Security Design ADO SQLTypes SQL XPath XSLT Runtime InteropServices Remoting Serialization Serialization Configuration SessionState Caching Security Services Description Discovery Protocols UI HtmlControls WebControls System.Drawing Imaging Drawing2D Text Printing System.WinForms Design ComponentModel .NET Framework Namespace System.Globalization Namespace: System.Globalization Namespace Includes classes for functionality such as: Culture-aware string comparison AABC vs. ABCC (ordinal: codepoint values) Coté vs. Côte (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 Starting point : the CultureInfo class CultureInfo: CultureInfo Provider of cultural preferences Two roles CurrentCulture Date and number formatting String comparison and casing … CurrentUICulture Resource selection for user interface Can be controlled on a per thread basis RFC 1766 derived hierarchy CultureInfo.CreateSpecificCulture Optional mapping from neutral to specific CultureInfo: CultureInfo Cultural preferences provider Invariant culture culture-invariant default Neutral culture Based on language Resource only No formatting CurrentUICulture only Specific culture Based on language & region Resource & Formatting specifics CurrentCulture & CurrentUICulture CultureInfo and related classes Demo: CultureInfo and related classes Demo How to Set Culture and UICulture: How to Set Culture and UICulture Implicitly CurrentUICulture Picked up from GetUserDefaultUILanguage On Windows XP and Windows 2000 MUI SKUs, this setting can be set by end-user Otherwise, set from OS resources language CurrentCulture Picked up from GetUserDefaultLCID Affected by changes to Control Panel | Regional Options | Set Locale How to Set CurrentCulture, CurrentUICulture : How to Set CurrentCulture, CurrentUICulture Explicitly Thread.CurrentThread.CurrentUICulture = new CultureInfo(“ja”) Thread.CurrentThread.CurrentCulture = new CultureInfo(“ja-JP”) Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0)) Within an individual API that takes a culture Str = DateTime.Now.ToString(cultureInfo); What if I don’t want formatting to change by culture?: What if I don’t want formatting to change by culture? For UI, prefer culture-sensitive formatting However, for wire-transfer, database storage, you may want stable, culture-unrelated format, such as #,###.## dd MMMM yyyy HH:mm:ss GMT international currency symbol Use CultureInfo.InvariantCulture Culture-aware Classes: Culture-aware Classes Any API which takes a culture, or an IFormatProvider Culture-sensitive by default Examples System.Globalization.CompareInfo System.Globalization.StringInfo System.Globalization.Calendar System.Resources System.DateTime System.String Culture-aware Classes: Culture-aware Classes 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 be derived Culture-aware Classes: Culture-aware Classes DateTime Provides methods that enable culture-sensitive operations on a DateTime. Use the DateTimeFormatInfo Class to format and display a DateTime based on culture. DateTimeFormatInfo Defines how DateTime values are formatted and displayed, depending on the culture. Culture-aware Classes: Culture-aware Classes NumberFormatInfo Defines how currency, decimal separator and other numeric symbols are formatted and displayed based on culture Culture-aware Classes: Culture-aware Classes 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 Unicode support: Unicode support Unicode everywhere Unicode internally UTF-8 and UTF-16 encoding classes String operations Surrogates & Combining characters support Character type information Based on Unicode 3.0 tables Unicode category enumeration CultureInfo data Caveats OS dependent operations Controls support File System ASP.NET And International: ASP.NET And International ASP.NET delivers improved support for international application development: Can leverage .NET Framework base classes System.Globalization System.Text (encodings) System.Resources Can separate resources from source code Unicode-enabled More options for specifying encoding ASP.NET setting culture and encoding:: ASP.NET setting culture and encoding: Programmatically Thread.CurrentThread.CurrentCulture =………. In a Page directive <%@Page ResponseEncoding=“utf-8” %> In a Configuration file (web.config) Per application or per machine <globalization culture=“de-DE” fileEncoding=“utf-8” /> The most specific setting has precedence : Programmatic overrides Page Directive, Page Directive overrides Configuration file ASP.NET Demo: ASP.NET Demo Additional References: Additional References Documentation .NET Framework SDK: Developing World-Ready Applications Tutorials Samples Visual Studio : Visual Studio.NET\Visual Basic and VisualC#\Globalizing and Localizing\Walkthroughs General info on .NET Framework: http://msdn.microsoft.com/net http://www.GotDotNet.com General info on globalization: http://www.microsoft.com/globaldev Questions ?: Questions ?