localization with

Upload: ramakrishna-munnaluri

Post on 05-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Localization With

    1/59

  • 7/31/2019 Localization With

    2/59

    Introduction

    Why should my application be world-ready?

    Definitions

    Globalization Localizability

    Localization

    Resources

  • 7/31/2019 Localization With

    3/59

    .NET International Support

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

    cultures

    Important NamespaceSystem.Globalization

    System.Resources

    System.Text

  • 7/31/2019 Localization With

    4/59

    CultureInfo Class

    System.Globalization.CultureInfo Provides culture specific information such as

    culture name, language, country region,

    calendar Provides access to culture specific instancesof other classes such as DateTimeFormatInfo,NumberFormatInfo etc.

  • 7/31/2019 Localization With

    5/59

    Culture Names

    Culture Names RFC 1766 standard [-] languagecode2 two letter lowercase derived

    from ISO-639-1 Country/regioncode two/three letter uppercase

    derived from ISO 3166

  • 7/31/2019 Localization With

    6/59

    Types of Cultures

    Different Types Of Cultures Invariant Culture Neutral Cultures Specific Cultures

  • 7/31/2019 Localization With

    7/59

    Culture Tree

    Invariant Culture

    de (German Neutral Culture)

    en (English Neutral Culture)

    de-AT (German-Austria)

    de-CH (German-Switzerland)

    de-DE (German-Germany)

    de-LI (German-Liechtenstein)

    de-LU (German-Luxembourg)

    en-US (English-United States)

    en-GB (English-United Kingdom)

  • 7/31/2019 Localization With

    8/59

  • 7/31/2019 Localization With

    9/59

    Invariant Culture

    VB.NETImports System.Globalization

    Dim ci As CultureInfo = new CultureInfo()

    C#using System.Globalization;

    CultureInfo ci = new CultureInfo();

  • 7/31/2019 Localization With

    10/59

    Invariant Culture

    VB.NETDim ci As CultureInfo =

    CultureInfo.InvariantCulture

    C#CultureInfo ci = CultureInfo.InvariantCulture

  • 7/31/2019 Localization With

    11/59

  • 7/31/2019 Localization With

    12/59

    Neutral Culture

    VB.NETImports System.Globalization

    Dim ci As CultureInfo = New CultureInfo(fr)

    C#

    using System.Globalization;

    CultureInfo ci = new CultureInfo(fr);

  • 7/31/2019 Localization With

    13/59

    Specific Culture

    Associated with Language and aCountry/Region

    fr Neutral Culture

    fr-FR Specific Culture Provides additional information about

    the date, time, currency and numberformatting options

    Can be used wherever a Neutral cultureis used, the opposite is not true

  • 7/31/2019 Localization With

    14/59

    Specific Culture

    VB.NETImports 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

  • 7/31/2019 Localization With

    15/59

  • 7/31/2019 Localization With

    16/59

    Neutral Specific

    Use CultureInfo.CreateSpecificCultureto create Specific culture when youknow the Neutral culture

    Not always accurate e.g. you might getes-ES (Spanish Spain) for es where asyou wanted es-AR (Spanish Argentina)

    Doesnt work for zh

    Useful for a web application queryingUser Accept languages

  • 7/31/2019 Localization With

    17/59

    CultureInfo and LCID

    Preferred way of creating instances ofCultureInfo is to provide stringidentifiers

    You can also use LCID to create

    instances of CultureInfo

    Some specific cultures can only becreated using LCID

    0x0C0A Spanish Spain (same as es-ESand uses default international sort)

    0x040A Also Spanish Spain (usestraditional sort)

  • 7/31/2019 Localization With

    18/59

    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 Invariantculture) that are supported by .NETFramework

    CultureTypes.SpecificCultures all thespecific cultures that are supported by .NET

    CultureTypes.InstalledWin32Cultures allthe cultures that are currently installed andsuppored on your windows system

  • 7/31/2019 Localization With

    19/59

    Hard CodeEnumerating Cultures

  • 7/31/2019 Localization With

    20/59

    The two roles of CultureInfo

    CurrentUICulture Localization of userinterface Both Neutral and Specific cultures can be

    used to set CurrentUICulture

    CurrentCulture - Handling internationaldata (date, time, currencies,characters) Only Specific cultures can be used to set

    CurrentCulture

    Need not have same value Spanish Application running in US American application running in Japan

  • 7/31/2019 Localization With

    21/59

    Setting CurrentCulture andCurrentUICulture 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 productswhere the end users can set their UI language

    If UI language is not set, set by systemdefault installed language which is the

    language of OSs resources

  • 7/31/2019 Localization With

    22/59

    Setting CurrentCulture andCurrentUICulture

    Explicitly setting the values

    VB.NET

    Imports System.GlobalizationImports System.Threading

    Thread.CurrentThread.CurrentUICulture = new

    CultureInfo(en)

    Thread.CurrentThread.CurrentCulture = new

    CultureInfo(ja-JP)

  • 7/31/2019 Localization With

    23/59

    Setting CurrentCulture andCurrentUICulture

    Explicitly setting the values

    C#using System.Globalization;

    using System.Threading;

    Thread.CurrentThread.CurrentUICulture = new

    CultureInfo(en);

    Thread.CurrentThread.CurrentCulture = new

    CultureInfo(ja-JP);

  • 7/31/2019 Localization With

    24/59

    Overriding User Settings

    User can change some of the values that areassociated with current culture

    Can change date time settings to dd/mm/yyyywhile keeping rest of the settings as en-US

    Can change currency to Euro from DM Impacts instances of

    CultureInfo.DateTimeFormatInfo,CultureInfo.NumberFormatInfo, CultureInfo.TextInfo etc.

    Use CultureInfo.UseUserOverride Set to true by default respects users overrides

    Use alternative CultureInfo constructor to set it tofalse

  • 7/31/2019 Localization With

    25/59

  • 7/31/2019 Localization With

    26/59

    Hard CodeUsing CultureInfo in API

  • 7/31/2019 Localization With

    27/59

    Other classes inSystem.Globalization CultureInfo references several other

    classes

    Calendar

    CompareInfo

    DateTimeFormatInfo

    NumberFormatInfo

    TextInfo

  • 7/31/2019 Localization With

    28/59

    Other classes inSystem.Globalization Classes not referenced by CultureInfo

    DaylightTime

    RegionInfo

    SortKey

    StringInfo

    TextElementEnumerator

  • 7/31/2019 Localization With

    29/59

    Other classes inSystem.Globalization Finally the specific calendar classes that

    all derive from the base Calendar class GregorianCalendar HebrewCalendar HijriCalendar JapaneseCalendar JulianCalendar KoreanCalendar

    TaiwanCalendar ThaiBuddhistCalendar

  • 7/31/2019 Localization With

    30/59

    RegionInfo class Provides information about

    Country/Region

    Does not represent preferences of user

    Does not depend on users language orculture

    Use LCID of a specific culture or two-

    letter ISO 3166 region code to createan instance of RegionInfo

    Provides information about ISOcurrency symbol and metric usage

  • 7/31/2019 Localization With

    31/59

  • 7/31/2019 Localization With

    32/59

    Dealing with Date and Time DateTimeFormatInfo class 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 specificculture

    Use DateTimeFormatInfo.InvariantInfoproperty to get it for invariant culture

    Cannot create DateTimeFormatInfo for aneutral culture

  • 7/31/2019 Localization With

    33/59

    Dealing with Date and Time DateTime value types represent dates

    and times between 12:00:00 MidnightJanuary 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

  • 7/31/2019 Localization With

    34/59

    Formatting Date and Time DateTime data type implements

    IFormattable allowing it to be formattedas string

    Use one or more overloads ofDateTime.ToString to do so

    Standard format provider class used forformatting DateTime objects is

    DateTimeFormatInfo.

  • 7/31/2019 Localization With

    35/59

    Format Strings Interpreted as standard format specifiers ifthey 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 ofcurrent culture do not change value for r, sand u specifiers

  • 7/31/2019 Localization With

    36/59

    Format StringsFormat specifier Current culture Outputd en-US 4/10/2001

    d en-NZ 10/04/2001

    d de-DE 10.04.2001

    D hi-IN 102001

    D en-US Tuesday, April 10, 2001T en-US 3:51:24 PM

    T es-ES 15:51:24

    f en-US Tuesday, April 10, 2001 3:51 PM

    f fr-FR mardi 10 avril 2001 15:51

    r en-US Tue, 10 Apr 2001 15:51:24 GMTr zh-SG Tue, 10 Apr 2001 15:51:24 GMT

    s en-US 2001-04-10T15:51:24

    s pt-BR 2001-04-10T15:51:24

  • 7/31/2019 Localization With

    37/59

  • 7/31/2019 Localization With

    38/59

    Parsing Date and Time Parse and ParseExact can be used to convert

    string expression to DateTime

    Parse method converts any valid stringrepresentation

    ParseExact method converts strings that are of theform you specify

    Both method accept format provider allowing youto parse culture specific settings

    Parsing a string with just time leads to date inthe DateTime being set to current date. UseDateTimeStyles.NoCurrentDateDefault tooverride

  • 7/31/2019 Localization With

    39/59

    Parsing Date and Time

    C#String MyString = Jan 1, 2002;

    DateTime MyDateTime = Date.Parse(MyString);

    Console.WriteLine(MyDateTime);

  • 7/31/2019 Localization With

    40/59

    Parsing Date and Time C#using System.Globalization;

    CultureInfo MyCultureInfo = new CultureInfo(ta-

    IN");string MyString = "12 2002";

    DateTime MyDateTime = DateTime.Parse(MyString,

    MyCultureInfo);

    Console.WriteLine(MyDateTime);

  • 7/31/2019 Localization With

    41/59

    Parsing Date and Time C#using System.Globalization;

    CultureInfo MyCultureInfo = new CultureInfo(ta-

    IN");string MyString = "12 2002";

    DateTime MyDateTime = DateTime.Parse(MyString,

    MyCultureInfo);

    Console.WriteLine(MyDateTime);

  • 7/31/2019 Localization With

    42/59

    Parsing Date and Time

    C#using System.Globalization;

    CultureInfo MyCultureInfo = new CultureInfo("en-

    US");string MyString = " Tuesday, April 10, 2001";

    DateTime MyDateTime = DateTime.ParseExact(MyString,

    "D", MyCultureInfo);

    Console.WriteLine(MyDateTime);

  • 7/31/2019 Localization With

    43/59

    Dealing with Numbers NumberFormatInfo class defines how numericvalues 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 NumberFormatproperty

    Invariant Culture: use InvariantInfo static member

    of NumberFormatInfo or use NumberFormatInfoconstructor Neutral Culture: This class cannot be created for a

    Neutral culture

  • 7/31/2019 Localization With

    44/59

    Hard CodeInventing

    NumberFormatInfo Invent a number format that allows us to parse

    and display a number as 123|456*, where | isgroup separator and * is the currency symbol

  • 7/31/2019 Localization With

    45/59

    Resource Model

    .NET Framework uses a new resourcemodel

    Any serializable object can be aresource (e.g. also sound, images)

    Resource model is extensible to newformats

    Localization focuses on text, Windows

    Forms formats

    Fully supported in the .NET CompactFramework for smart devices

  • 7/31/2019 Localization With

    46/59

    Resource source formats

    Text format (.txt) 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 FrameworkSDK

  • 7/31/2019 Localization With

    47/59

    Resource generation process

    .resx file XML-based file

    Describes resources

    .resources file

    Binary compiled file

    Assembly

    Executable withdefault resources

    Resource-only satelliteassembly(.resources.dll)

    .resx file

    .resources file

    assembly with resources

    resgen

    al/compiler

  • 7/31/2019 Localization With

    48/59

  • 7/31/2019 Localization With

    49/59

  • 7/31/2019 Localization With

    50/59

  • 7/31/2019 Localization With

    51/59

    Loading Resources,Continued

    Resource manager can be used to loadboth strings and objects: RM.GetString(mystring, new

    CultureInfo(en-NZ) ) RM.GetString (mystring)

    RM.GetObject(Button1.Cursor)

    Loads requested resources based onThread.CurrentThread.CurrentUICulture

    Fallback hierarchy derived from RFC1766

  • 7/31/2019 Localization With

    52/59

    Resource FallbackMain AssemblyCodeDefault resources(fallback)

    Greeting=HelloFarewell=GoodbyeLogo=

    FrenchNo codefr resources

    Greeting = BonjourFarewell = Au revoir

    French (France) (fr-FR)No codefr-FR resources

    Greeting=Salut

  • 7/31/2019 Localization With

    53/59

    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 thenew resource format:

    ForeignDesk http://www.foreigndesk.net(Lionbridge)

    Alchemy CATALYSThttp://www.alchemysoftware.ie/

    http://www.foreigndesk.net/http://www.alchemysoftware.ie/http://www.alchemysoftware.ie/http://www.foreigndesk.net/
  • 7/31/2019 Localization With

    54/59

    Localizing a Web-based UI - Stepby Step

    Create a folder for your resources say resources

    Create plain text version of the stringresources

    Use resgen.exe to convert them toCLR binary .resources file

    Load these resources at runtimeusing ResourceManager

  • 7/31/2019 Localization With

    55/59

    Hard CodeMaking it all make sense alocalized ASP.NET application

  • 7/31/2019 Localization With

    56/59

    Localizing Winforms UI Step byStep

    Finish your core application, ideally try tofreeze on code. If not, at least finalize formdesign

    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 inwhich you are localizing

    Set the CurrentUICulture in theInitializeComponent method of your form to

    the language you want your UI to appear in

  • 7/31/2019 Localization With

    57/59

    Demo: Localizing a windowsform

  • 7/31/2019 Localization With

    58/59

    Technical Resources

    http://www.microsoft.com/globaldev/ -do checkout the Ask Dr. Internationalfeature

    http://www.microsoft.com/india/msdn/- ask questions, attend weekly expertchats and more!

    http://www.bhashaindia.com/

  • 7/31/2019 Localization With

    59/59