Arrao4u

…a blog by Rama Rao

Archive for the ‘Localization & Globalization’ Category

Localization & Globalization

Posted by arrao4u on December 27, 2009

Aplications must be designed to accommodate users from various cultures. Users in different parts of the world use different languages and different formatting standards for numbers, currency, and dates. International applications should be customizable according to the preferences of users belonging to different nations, cultures, or regions

An application uses the locale to identify the preference of a user. The local is the combination of a language and a country. For example, the locale of a user speaking English and belonging to the United States is “en-US”. The locale includes information about the formats used for representing time and date, symbols and conventions used for representing currency, and the character encoding scheme being used.

The structure of an internationalized application is divided into two blocks:

  • Code Block: Contains the application code or the executable part of an application. This block will remain the same for all locales.
  • Data Block: Contains all the resources, such as text and images used by the user interface (UI). This block is locale-specific, and each locale will have one data block.

The process of making  an application ready for customers is called internationalization. It includes three phases:

  • Globalization: Involves writing the executable code for an application in such a way that makes it culture-neutral and language-neutral. While incorporating globalization, you must ensure that the culture-specific details are not hard-coded into the executable code. The primary task in this phase is to identify the various locale-sensitive resources and to isolate these resources from the executable code.
  • Localizability: An application that has been globalized must be tested to ensure that its executable code is independent of the culture and language-specific data. This is called localizability testing. The focus of localizability testing is to check how the application adapts itself to different locales.
  • Localization: Involves the customization of an application for a specific locale. One major task in this phase is the translation of resources identified during the globalization phase. During this phase, various resources, such as images and text, for the designated locale are created.

When designing an international application, you should ensure the following factors:

  • User Interface (UI): The language used to design different UI components, such as menu, message box, static-text holder (label), is not fixed during design phase of the application.
  • Information Formats: The formats of different elements, such as currency, number, and date, are not fixed during design phase of the application.

The different aspects of an application that should be taken into account while incorporating localization are:

  • Formats: The formats used for displaying numbers, time, dates and currency should be considered while incorporating localization.
  • Graphics: The locale-sensitive graphics, such as maps, traffic signs, and calendar should be considered while incorporating localization.

Implementing Globalization

The System.Globalization namespace provides different classes that enable you to determine locale-specific information, such as te language and country of an application at run time. The System.Globalization namespace includes classes that enable you to localize your application. For example, the Calendar class contains different calendars, such as JulianCalendar and GrgorianCalendar.

Using the CultureInfo Class

//
Every locale is identified by a culture. The general format of the culture is as follows:

<Language code>-<Country/Region code>

  • Language code: Represents a two-letter representation of a language. It is always written in lowercase. For example, the language code for Arabic is “ar” and that for Russian is “ru”.
  • Country/Region code: Represents a two-letter representation of a country or a region. It is always written in uppercase. For example, the country code for Russia is “RU” and that for France is “FR”.

Cultures are of two types:

  • Neutral culture: Signifies a culture that is linked only to a language and not to a country. For example, “ru” is a neutral code.
  • Specific culture: Signifies a culture that is linked to both a country and a language. For example, “ru-RU” is a specific code.

The following table describes some of the commonly used properties of the CultureInfo class:

PROPERTY DESCRIPTION
CurrentCulture Returns an instance of the CultureInfo class that represents the culture for the current thread
CurrentUICulture Returns an instance of CultureInfo class that represents the culture for culture-specific resources.
DisplayName Returns the name of the culture in format: <Language code>-<country/region code>
EnglishName Returns the English name of the culture in f<Language code>-<country/region code> format
Name Returns the name of the culture in <Language code>-<country/region code> format
LCID Returns the culture identifier for the instance of the CultureInfo class. Every culture has an identifier. For e.g., 0×0407 is the identifier for “de-DE” culture

The following table describes some of the commonly used methods of the CultureInfo class:

METHOD DESCRIPTION
GetCultures Returns a list of supported cultures.
GetFormat Returns an object that represents the format of a specified type. For e.g., this method will return an object representing the format of the date when the specified type is date/time.

The following code snippet shows how to use the CultureInfo class:

CultureInfo my_CI = new CultureInfo (“ru-RU”);

In the preceding code snippet, a new CultureInfo class instance by the name, my_CI, is created that sets the culture of the instance to “ru-RU”.

Working with Resources

A resource is any non-executable data that is logically deployed with an application. A resource might be displayed in an application as an error message or as a part of the UI. Examples of resources include the strings displayed in the UI based on the culture settings of the system or a set of images. A separate resource file should be created for each culture for which the application is to be customized. Information is stored in a resource file in the key/value format. For example, to store the welcome message you must assign a the message to a key. Like this:

WelcomeText = “Welcome to my homepage”;

In the preceding code snippet, WelcomeText is the key and “Welcome to my homepage” is the value. There are specific naming conventions to follow when naming resource files. The resource file for default culture should be Strings.txt or Strings.ResX, and for any other culture Strings.culture-name.ResX. Where Strings represents the name of the file. For e.g., Strings.fr-CA.resx. After resource files are created in the text form, they need to be converted into a binary format recognized by the .NET Framework.

Localization is the process of adapting a software application for a specific locale. Globalization is the process of identifying the localizable resources of the application. You can provide support for Localization and Globalization to the application using System.Globalization, System.Resources and System.Threading namespaces.
The developer can define culture specific information using the System.Globalization namespace. The System.Resources namespace contains ResourceManager class that allows access to resources either from the main assembly or from the Satellite Assemblies. The System.Threading namespace supports for multithreaded programming.
A web form has two culture values, Culture and UICulture. Culture value is used for date and number formatting and UICulture values are used to determine culture specific resources.
You can set culture and UICulture values in the application as follows.

Using <globalization> element of Web.Config.

Using @Page directive in the Page.

In Code-Behind Page e.g.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (“en-GB”);
Thread.CurrentThread.CurrentUICulture=new CultureInfo(“en-GB”);

Ex:

Understanding concept with Example
Step1: Create a Web Project.

Step2: Design your form.
For this example : Take 5 label controls, 4 textbox controls and 1 drop-down list control.
Place the control on form as shown in figure.

Step3: Identifying Resources that needs to be localized (Globalization Phase).
We want the output as shown in figure:

So here Resource that needs localization are

Language Name Label
Long Date Label
currency Label
Amount Label
Welcome Label (It would be same for all language, except its formatting might change, Note: you can do this easily with style-sheet concept, but as an example here I am explaining it with globalization concept.)

Now, Applying Localization to resources which needs to be localized and globalization phase. (Localization Phase begin).

Step5: Creating Resource File.

In asp.net 2.0 –> go to design mode of aspx page–>Tools–> Generate local resoure click on this link in the menu–> It will create resource files for u.

For this example i am creating resource file for French and German culture and other culture would get value from default resource file. Right click the Project in Solution Explorer and add a “asp.net folder”

Add App_GlobalResources Folder Add App_LocalResources Folder

Adding Global Resource File under App_GlobalResources Folder

Adding Local Resource File under App_LocalResources Folder

Note: While creating resource file, you should name resource file with filename.aspx.cultercode.resx

Eg:
1 For Default.aspx file, Default resource file would be Default.aspx.resx

2 For German resource file for Default.aspx would be Default.aspx.de-DE.resx

3 For French resource file for Default.aspx would be Default.aspx.fr-FR.resx

Step6: Adding code in .CS file to make it work

Adding Namespace
using System.Globalization;
using System.Drawing;
using System.Threading;

Filling Dropdown with set of languages for user-choice.
Code in Page Load Event.

if(!Page.IsPostBack)
{
//Applying Globalization to Header Text
lblWelcome.Text = Resources.GlobalResource.HeaderTitleText; lblWelcome.ForeColor = Color.FromName(Resources.GlobalResource.HeaderTitleColor);
//CultureInfo Class GetCultures gives collection of language.
foreach(CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
{
//Filling the language collection into drop-downlistbox for user choice.
ddlLanguage.Items.Add(new ListItem(ci.EnglishName,ci.Name));
}
//Calling the Event when page loads for the first time.
ddlLanguage_SelectedIndexChanged(sender,e);
}

Assigning selected language value to current thread, to reflect changes.
private void ddlLanguage_SelectedIndexChanged(object sender, System.EventArgs e)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLanguage.SelectedValue);
Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlLanguage.SelectedValue);

//Applying Localization
ApplyLocalization();
ApplyUILocalization();
}

Code which requires localization value.
private void ApplyUILocalization()
{
lblLanguageName.Text = Convert.ToString( this.GetLocalResourceObject(“LanguageName”));
lblLongDate.Text = Convert.ToString( this.GetLocalResourceObject(“LongDate”));
lblCurrency.Text = Convert.ToString( this.GetLocalResourceObject(“Currency”));
lblAmount.Text = Convert.ToString( this.GetLocalResourceObject(“Amount”));
}

Code which does not requires localization.
private void ApplyLocalization()
{
//Lets Assume currency and amount to keep example simple.
double dblCurrency = 500.50;
double dblAmount = 9580000.75;

//No extra effort for localization as you see here.
txtLanguageName.Text = ddlLanguage.SelectedItem.Text;
txtLongDate.Text = DateTime.Now.ToLongDateString();
txtCurrency.Text = dblCurrency.ToString(“c”);
txtAmount.Text = dblAmount.ToString(“n”);
}

Now, run the application and select the different culture and based on selection you will get the localized display as shown in figure:

When associated resource file not available culture from Default Resource File would be shown.

how simplest syntax structure is used to perform globalization and localization.

//
//

ref:http://dotnetguts.blogspot.com/2007/02/ease-of-applying-globalization-and.html

Posted in ASPNET, Localization & Globalization | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.