Documente Academic
Documente Profesional
Documente Cultură
Introduction
Why should my application be worldready? Definitions
Globalization Localizability Localization Resources
Important Namespace
System.Globalization System.Resources System.Text
CultureInfo Class
System.Globalization.CultureInfo Provides culture specific information such as culture name, language, country region, calendar Provides access to culture specific instances of other classes such as DateTimeFormatInfo, NumberFormatInfo etc.
Culture Names
Culture Names
RFC 1766 standard
<languagecode2>[-<country/regioncode2>] languagecode2 two letter lowercase derived from ISO-639-1 Country/regioncode two/three letter uppercase derived from ISO 3166
Types of Cultures
Different Types Of Cultures
Invariant Culture Neutral Cultures Specific Cultures
Culture Tree
Invariant Culture de (German Neutral Culture) de-AT (German-Austria) de-CH (German-Switzerland) de-DE (German-Germany) de-LI (German-Liechtenstein) de-LU (German-Luxembourg) en (English Neutral Culture) en-US (English-United States) en-GB (English-United Kingdom)
Invariant Culture
Culture Neutral Associated with English language but no Country/Region Should be used for storing data in a culture independent way Should not be used for User Interface elements
Invariant Culture
VB.NET
Imports System.Globalization Dim ci As CultureInfo = new CultureInfo()
C#
using System.Globalization; CultureInfo ci = new CultureInfo();
Invariant Culture
VB.NET
Dim ci As CultureInfo = CultureInfo.InvariantCulture
C#
CultureInfo ci = CultureInfo.InvariantCulture
Neutral Culture
Associated with a language but not with country/region Can be used for language-only related options Cannot be used for retrieving information such as date/time formatting Specified using <languagecode2> format
For instance Arabic ar Exceptions zh-CHT, zh-CHS
Neutral Culture
VB.NET
Imports System.Globalization Dim ci As CultureInfo = New CultureInfo(fr)
C#
using System.Globalization; CultureInfo ci = new CultureInfo(fr);
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, the opposite is not true
Specific Culture
VB.NET
Imports System.Globalization Imports System Module Module1 Sub Main() Dim sc As CultureInfo = New CultureInfo(es-AR) Spanish-Argentina Dim nc As CultureInfo = sc.Parent Console.WriteLine(String.Format(Specific culture is {0} ({1}),sc,sc.EnglishName)) Console.WriteLine(String.Format(Neutral culture is {0} ({1}),nc,nc.EnglishName)) End Sub End Module
Specific Culture
C#
using System; using System.Globalization; namespace ConsoleApplication2 { class Class1 { public static void Main() { CultureInfo sc = new CultureInfo(esAR); //Spanish Argentina CultureInfo nc = sc.Parent;
Neutral Specific
Use CultureInfo.CreateSpecificCulture to create Specific culture when you know the Neutral culture
Not always accurate e.g. you might get es-ES (Spanish Spain) for es where as you wanted es-AR (Spanish Argentina) Doesnt work for zh
CurrentUICulture
Set by GetUserDefaultUILanguage on Windows 2000 and Windows XP MUI products where the end users can set their UI language If UI language is not set, set by system default installed language which is the language of OSs resources
Use CultureInfo.UseUserOverride
Set to true by default respects users overrides Use alternative CultureInfo constructor to set it to false
Many functions accept the IFormatProvider interface as parmeter CultureInfo implements IFormatProvider you can use an instance of CultureInfo where IFormatProvider is specified
Finally the specific calendar classes that all derive from the base Calendar class
Provides information about Country/Region Does not represent preferences of user Does not depend on users language or culture Use LCID of a specific culture or twoletter ISO 3166 region code to create an instance of RegionInfo Provides information about ISO currency symbol and metric usage
RegionInfo class
DateTimeFormatInfo class
Use CultureInfo.DateTimeFormat property to create DateTimeFormatInfo for a specific culture Use DateTimeFormatInfo.InvariantInfo property to get it for invariant culture Cannot create DateTimeFormatInfo for a neutral culture
DateTime value types represent dates and times between 12:00:00 Midnight January 1 0001 to 11:59:59 P.M. December 31 9999. Time values are measured in 100 nanosecond units called Ticks DateTime and TimeSpan
DateTime represents an instant in time TimeSpan represents a time interval You can subtract one instance of DateTime from another to get TimeSpan
Interpreted as standard format specifiers if they are single character and contain only one of the single format specifiers d, D, t, T, f, F, g, G, M or m, R or r, s, u, U, Y or y A character other than one listed above causes exception Format string longer than one character is treated as custom format Patterns produced by these format specifiers are culture dependent DateSeparator and TimeSeparator characters associated with DateTimeFormat property of current culture do not change value for r, s and u specifiers
Format Strings
Format Strings
Format specifier d Current culture en-US 4/10/2001 Output
d
d D
en-NZ
de-DE hi-IN
10/04/2001
10.04.2001 10 2001
D
T T f f r r s s
en-US
en-US es-ES en-US fr-FR en-US zh-SG en-US pt-BR
Parsing a string with just time leads to date in the DateTime being set to current date. Use DateTimeStyles.NoCurrentDateDefault to override
C#
using System.Globalization; CultureInfo MyCultureInfo = new CultureInfo(taIN"); string MyString = "12 2002"; DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo); Console.WriteLine(MyDateTime);
C#
using System.Globalization; CultureInfo MyCultureInfo = new CultureInfo(taIN"); string MyString = "12 2002"; DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo); Console.WriteLine(MyDateTime);
C#
using System.Globalization; CultureInfo MyCultureInfo = new CultureInfo("enUS"); string MyString = " Tuesday, April 10, 2001"; DateTime MyDateTime = DateTime.ParseExact(MyString, "D", MyCultureInfo); Console.WriteLine(MyDateTime);
NumberFormatInfo class defines how numeric values are formatted and displayed depending on culture Contains information such as currency, decimal separator and other numeric symbols To create an instance of this class for:
Specific Culture: create an instance of CultureInfo for that culture and use its NumberFormat property Invariant Culture: use InvariantInfo static member of NumberFormatInfo or use NumberFormatInfo constructor Neutral Culture: This class cannot be created for a Neutral culture
.NET Framework uses a new resource model Any serializable object can be a resource (e.g. also sound, images) Resource model is extensible to new formats Localization focuses on text, Windows Forms formats Fully supported in the .NET Compact Framework for smart devices
Resource Model
.resources file
Binary compiled file
Assembly
Executable with default resources Resource-only satellite assembly (.resources.dll)
Naming convention
<myproject>.<xx-XX>.resources
<myproject>.resources.dll
Directory locations for satellites: A subdirectory per culture Either neutral or specific cultures can be used Myproject.dll
Naming convention
Continued
Servicing the main assembly without revising the satellite assemblies SatelliteContractVersionAttribute
Loading Resources
Creating a Resource Manager
The RM constructor indicates the file from which resources are to be loaded. Several alternatives
Load from a loose .resources file Load from this assembly Load from another assembly Load from a custom resource format if you write your own resource manager
Loading Resources,
Continued
Loads requested resources based on Thread.CurrentThread.CurrentUICulture Fallback hierarchy derived from RFC 1766
Resource Fallback
Main Assembly Code Default resources(fallback) Greeting=Hello Farewell=Goodbye Logo=<graphic data> French No code fr resources Greeting = Bonjour Farewell = Au revoir French (France) (fr-FR) No code fr-FR resources Greeting=Salut
3rd party localization tools enabled for the new resource format:
ForeignDesk http://www.foreigndesk.net (Lionbridge) Alchemy CATALYST http://www.alchemysoftware.ie/
Finish your core application, ideally try to freeze on code. If not, at least finalize form design Set the Localizable property of your form to true Select the Language property of your form to a language you want to localize Change labels, menus etc to the language in which you are localizing Set the CurrentUICulture in the InitializeComponent method of your form to the language you want your UI to appear in
http://www.microsoft.com/globaldev/ do checkout the Ask Dr. International feature http://www.microsoft.com/india/msdn/ - ask questions, attend weekly expert chats and more! http://www.bhashaindia.com/
Technical Resources