You are on page 1of 59

.

NET for Indian Languages


Dr U B Pavanaja CEO, Vishva Kannada Softech
Microsoft MVP - Localization/Globalization http://www.vishvakannada.com/ pavanaja@vishvakannada.com

Introduction
Why should my application be worldready? Definitions
Globalization Localizability Localization Resources

.NET International Support


.NET Framework International Support
Provides information and APIs for over 200 cultures

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;

Console.WriteLine(String.Format(Specific culture is {0} ({1}), sc, sc.EnglishName));


Console.WriteLine(String.Format(Neutral culture is {0} ({1}), nc, nc.EnglishName)); } } }

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

Useful for a web application querying User Accept languages

CultureInfo and LCID


Preferred way of creating instances of CultureInfo is to provide string identifiers You can also use LCID to create instances of CultureInfo Some specific cultures can only be created using LCID
0x0C0A Spanish Spain (same as es-ES and uses default international sort) 0x040A Also Spanish Spain (uses traditional sort)

Using the CultureInfo class


Enumerating Cultures
CultureTypes.AllCultures all cultures that are actually supported by .NET Framework CultureTypes.NeutralCultures all the neutral cultures (and also the Invariant culture) that are supported by .NET Framework CultureTypes.SpecificCultures all the specific cultures that are supported by .NET CultureTypes.InstalledWin32Cultures all the cultures that are currently installed and suppored on your windows system

Hard Code Enumerating Cultures

The two roles of CultureInfo


CurrentUICulture Localization of user interface CurrentCulture - Handling international data (date, time, currencies, characters) Need not have same value
Only Specific cultures can be used to set CurrentCulture

Both Neutral and Specific cultures can be used to set CurrentUICulture

Spanish Application running in US American application running in Japan

Setting CurrentCulture and CurrentUICulture


Implicit Values
CurrentCulture
Picked up from the GetUserDefaultLCID API Affected by change to Control Panel Regional Options Set Locale

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

Setting CurrentCulture and CurrentUICulture


Explicitly setting the values
VB.NET
Imports System.Globalization Imports System.Threading Thread.CurrentThread.CurrentUICulture = new CultureInfo(en) Thread.CurrentThread.CurrentCulture = new CultureInfo(ja-JP)

Setting CurrentCulture and CurrentUICulture


Explicitly setting the values
C#
using System.Globalization; using System.Threading; Thread.CurrentThread.CurrentUICulture = new CultureInfo(en); Thread.CurrentThread.CurrentCulture = new CultureInfo(ja-JP);

Overriding User Settings


User can change some of the values that are associated with current culture
Can change date time settings to dd/mm/yyyy while keeping rest of the settings as en-US Can change currency to Euro from DM Impacts instances of CultureInfo.DateTimeFormatInfo,CultureInfo.Numb erFormatInfo, CultureInfo.TextInfo etc.

Use CultureInfo.UseUserOverride
Set to true by default respects users overrides Use alternative CultureInfo constructor to set it to false

Using CultureInfo in an API


Cases where you dont want to change the Thread culture for instance
All you have to do is format a date using a culture other than current thread culture

Many functions accept the IFormatProvider interface as parmeter CultureInfo implements IFormatProvider you can use an instance of CultureInfo where IFormatProvider is specified

Hard Code Using CultureInfo in API

Other classes in System.Globalization


CultureInfo references several other classes
Calendar CompareInfo DateTimeFormatInfo NumberFormatInfo TextInfo

Other classes in System.Globalization


Classes not referenced by CultureInfo
DaylightTime RegionInfo SortKey StringInfo TextElementEnumerator

Other classes in System.Globalization


GregorianCalendar HebrewCalendar HijriCalendar JapaneseCalendar JulianCalendar KoreanCalendar TaiwanCalendar ThaiBuddhistCalendar

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

Hard Code RegionInfo

DateTimeFormatInfo class

Dealing with Date and Time


Defines how DateTime values are formatted and displayed depending on culture Contains information such as date patterns, time patterns and AM/PM designators

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

Dealing with Date and Time

DateTime data type implements IFormattable allowing it to be formatted as string


Use one or more overloads of DateTime.ToString to do so Standard format provider class used for formatting DateTime objects is DateTimeFormatInfo.

Formatting Date and Time

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

Tuesday, April 10, 2001


3:51:24 PM 15:51:24 Tuesday, April 10, 2001 3:51 PM mardi 10 avril 2001 15:51 Tue, 10 Apr 2001 15:51:24 GMT Tue, 10 Apr 2001 15:51:24 GMT 2001-04-10T15:51:24 2001-04-10T15:51:24

Hard Code Formatting Date and Time

Parse and ParseExact can be used to convert string expression to DateTime


Parse method converts any valid string representation ParseExact method converts strings that are of the form you specify Both method accept format provider allowing you to parse culture specific settings

Parsing Date and Time

Parsing a string with just time leads to date in the DateTime being set to current date. Use DateTimeStyles.NoCurrentDateDefault to override

Parsing Date and Time


C#
String MyString = Jan 1, 2002; DateTime MyDateTime = Date.Parse(MyString); Console.WriteLine(MyDateTime);

C#

Parsing Date and Time

using System.Globalization; CultureInfo MyCultureInfo = new CultureInfo(taIN"); string MyString = "12 2002"; DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo); Console.WriteLine(MyDateTime);

C#

Parsing Date and Time

using System.Globalization; CultureInfo MyCultureInfo = new CultureInfo(taIN"); string MyString = "12 2002"; DateTime MyDateTime = DateTime.Parse(MyString, MyCultureInfo); Console.WriteLine(MyDateTime);

C#

Parsing Date and Time

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

Dealing with Numbers

Hard Code Inventing NumberFormatInfo


Invent a number format that allows us to parse and display a number as 123|456*, where | is group separator and * is the currency symbol

.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

Text format (.txt)

Resource source formats


Simple name/value pairs Only suitable for string resources

ResX XML format (.resx)


Simple, (almost) human-readable XML format Can include arbitrary objects Can be created with Visual Studio Some samples in the .NET Framework SDK

Resource generation process


.resx file
XML-based file Describes resources
.resx file
resgen

.resources file
Binary compiled file

.resources file al/compiler


assembly with resources

Assembly
Executable with default resources Resource-only satellite assembly (.resources.dll)

Naming pattern conventions .resources files: Satellite assemblies

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

\fr\myproject.resources.dll (neutral) \de-DE\myproject.resources.dll (specific)

Naming convention
Continued

Installing satellite assemblies into the global assembly cache (GAC)


Assemblies need to be strong-named (SN tool) A strong name consists of a name, version number and culture combined with a digital signature

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

Resource manager can be used to load both strings and objects:


RM.GetString(mystring, new CultureInfo(en-NZ) ) RM.GetString (mystring) RM.GetObject(Button1.Cursor)

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

External localization process


Any XML localization tool can be used WinRes: tool for visual editing of Windows Forms
Contained in .NET Framework SDK Does not require access to source

3rd party localization tools enabled for the new resource format:
ForeignDesk http://www.foreigndesk.net (Lionbridge) Alchemy CATALYST http://www.alchemysoftware.ie/

Localizing a Web-based UI - Step by Step


Create a folder for your resources say resources Create plain text version of the string resources Use resgen.exe to convert them to CLR binary .resources file Load these resources at runtime using ResourceManager

Hard Code Making it all make sense a localized ASP.NET application

Localizing Winforms UI Step by Step

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

Demo: Localizing a windows form

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

You might also like