C#: Globalization and localization

24
Globalization and Localization

description

This hel

Transcript of C#: Globalization and localization

Page 1: C#: Globalization and localization

Globalization and Localization

Page 2: C#: Globalization and localization

IntroductionGlobalization is about internationalizing

applications: the application supports number and date formats depending on the culture.

Localization is about translating applications for specific cultures.

Page 3: C#: Globalization and localization

Globalization (Internationalization) Its is a process of creating an

application that supports localized user interfaces.

Localization = translation It is the process of translating the

applications into different languages.

Page 4: C#: Globalization and localization

If you plan on distributing your application to an international audience, there are a number of things you'll need to keep in mind during the design and development phases. Even if you don't have such plans, a small effort up front can make things considerably easier should your plans change in future versions of your application. Services built into the .NET Framework make it easy to develop a single application that can adapt to different locales using managed development with Visual Basic or Visual C#.

Visual Studio .NET was designed from the start to make developing for an international audience easy by taking advantage of services built into the .NET Framework. The following topics will help introduce you to the internationalization features built into Visual Studio .NET.

Page 5: C#: Globalization and localization

One Code Base

Page 6: C#: Globalization and localization

Cultures and RegionsThe world is divided into multiple cultures and regions, and applications have to be aware of these cultural and regional differences. A culture is a set of preferences based on a user’s 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 different regions; for example, Spanish is spoken in Mexico, Spain, Guatemala, Argentina, and Peru, to name but a few.

Page 7: C#: Globalization and localization

The most important class in the System.Globalization namespace is the class CultureInfo.

CultureInfo represents a culture and defines calendars, formatting of numbers and dates, and sorting strings that are used with the culture.

The class RegionInfo represents regional settings (such as the currency) and shows if the region is using the metric system. In the same region, you can use multiple languages.

One example is the region of Spain with its Basque (eu-ES), Catalan (ca-ES), Spanish (es-ES), Galician (gl-ES) cultures.

Page 8: C#: Globalization and localization

Types of Culture Invarient NeutralSpecific

Page 9: C#: Globalization and localization

Invariant CultureShould be used for storing data in a culture

independent wayShould not be used for User Interface

elementsHas no country/region

Eg:CultureInfo ci = CultureInfo.InvariantCulture

Page 10: C#: Globalization and localization

Neutral CultureAssociated with a language not with

country/regionCan be used for UI related optionsCannot be used for retrieving information such

as date/time formattingSpecified using <languagecode2> format:

Arabic – “ar”Exceptions – zh-CHT, zh-CH

Eg.using System.Globalization;…CultureInfo ci = new CultureInfo(“fr”);

Page 11: C#: Globalization and localization

Specific CultureAssociated with Language and a Country/Region

fr Neutral Culturefr-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

Eg.using System.Globalization;…CultureInfo ci = new CultureInfo(“fr-FR”);

Page 12: C#: Globalization and localization

Formatting and ParsingFormatting

Standard formatting charactersCustom formatting characters

ParsingConfigurable with stylesTryParse – no conversion exceptionsParseExact for date/time values

Type ObjectCulture-appropriate

String

Formatting

Parsing

Page 13: C#: Globalization and localization

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 point numbers

Page 14: C#: Globalization and localization

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 service for conversionWhen storing as text use invariant culture

Reversible text floating-point format Currency text format: ¤1,000,000.23

Page 15: C#: Globalization and localization

System.Globalization Namespace Includes classes for functionality such as:

Culture-aware string comparison 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

Page 16: C#: Globalization and localization

CultureInfo classprovides culture specific informationControls 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)

Page 17: C#: Globalization and localization

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

specific cultures and not with neutral cultures.

Page 18: C#: Globalization and localization

CurrentUICulture Thread It determines which resources are loaded by the

Resource Manager if you have provided resources in multiple languages.

Because this controls only which language is used you can define CurrentUICulture with either neutral or specific cultures.

Page 19: C#: Globalization and localization

Changing the Culture programmatically

CultureInfo ci = new CultureInfo(culture); // set culture for formattingThread.CurrentThread.CurrentCulture = ci; // set culture for resourcesThread.CurrentThread.CurrentUICulture = ci;

Page 20: C#: Globalization and localization

Implementing localizationWhen the Localizable property is set to true,

the resource file resX is generated form the form.

BookOfTheDay.resX change the Language property of the form

and the properties of some form elements, a new resource file is generated for the specified language.BookOfTheDayForm.de.resX

Page 21: C#: Globalization and localization

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

Page 22: C#: Globalization and localization

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

Page 23: C#: Globalization and localization

DateTimeProvides 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.

Page 24: C#: Globalization and localization

CompareInfoProvides a set of methods that can be used to

perfomr culture-sensitive string comparisonsThe 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

NumberFormatInfoDefines how currency, decimal separator and

other numeric symbols are formatted and displayed based on culture.