Monday, April 04, 2005

[itsdifferent] Localizing your ASP.NET Applications


Writing applications that has the output in any of the languages is the concern of this writeup. Cultureinfo class is used to build applications ready for use world-over.

With the ever-increasing Internet population in the country, writing a web application that delivers the output in an Indian Language (or for that matter in other Non-English languages) will help in reaching out to the masses. In this article I will introduce you to this exciting world of writing world-ready, yet local ASP.NET applications using the features of Dot Net Framework.
The first step is to understand that a localized version of application can never be an after-thought (or a step before deployment). It has to be an integral part of your project planning and development exercise, right from the word go. The next step is the familiarity with certain terms that are commonly used in literatures that you will come across while developing localized applications.
World-Ready: It is an application that has been properly globalized and is ready for localization.
Globalization: It is the process of developing an application that makes it language neutral. In other words, an application is set to be globalized if it has been designed to accept input, display and make any output using Unicode. 
Localization: It is the process of adapting an application for a specific language/country/region/market. For example, the upcoming Hindi edition of Office 2003 is set to be localized, because apart from other things, the entire User Interface of the product is in Hindi. 
Locale: This is the most important term from the developer's standpoint. Locale is a set of features that are dependent on language, country/region and other cultural conventions. For example, this includes settings for the sort order, currency used, date format and more. In the Dot Net framework locale is set using Cultureinfo, which is a class under the System.Globalization namespace that represents data about a specific culture. The CultureInfo IDs follow the RFC 1766 in the format <ISO Language code in Lowercase>-<ISO Country code in Uppercase>. Example: pa-IN for Punjabi language and India.

Diving into water
I will now show a simple ASP.NET calendar control page that is localized to Hindi language. Code 1

Code 1

<%@ Page Culture="hi-IN" uiculture="hi-IN" Language="vb" %>
 <html>
 <head>
       <title>Simple Sample - Hindi</title>
 </head>
 <body>
       <form runat="Server">
           <asp:Calendar id="C1" runat="server"   DayNameFormat="Full"></asp:Calendar>
 </form>
 </body>
 </html>

ASP.NET page that is localized to Hindi with the Culture Page Attribute

Fig 1 is the output from the ASP.NET page shown in Code 1.

Fig 1

As you would have noticed, all that I have done in this page for getting the day names and month name in Hindi is to set the Culture & UI Culture Page Attribute to hi-IN. In the background what this attribute does is to tell the framework to bind the respective Hindi Resources to this file when it is compiled. 
You can also set the culture for an entire web application using web.config as shown in Code 2

Code 2

 <configuration>
   <system.web>
      <globalization
          fileEncoding="utf-8"
          requestEncoding="utf-8"
          responseEncoding="utf-8"
          culture="kn-IN"
          uiCulture="kn-IN"
       />
  </system.web>
 </configuration>

Setting the culture as Kannada Language and India in Web.Config

Out of the box the standard ASP.NET Web Controls is localized to all languages that are supported by the underlying Windows OS. Instead of Hindi/Kannada, I could have very well used the culture from any of the other Indian Languages supported by Windows 2000/XP/2003 like Punjabi, Gujarati, Kannada, Tamil, Marathi, Telugu and more. It can also be Japanese language in Japan (ja-JP) or French language in Canada (fr-CA). 

Localize the thread
In most of the cases we won't be able to set the culture using the page attribute. The reason for this being any change in the culture would require a recompile or editing the source-code. Both of these are not desirable and we would like this to be dynamic, say DB driven. For doing this we need to understand threads and how they use CultureInfo class.
Typically every ASP.NET page runs in a thread called "Current Thread". You create a new CultureInfo object specific to the culture (Language-Country pair) and then assign it to the CurrentThread class. (Code 3)

Code 3

 <%@ Page Language="VB" %>
 <%@Import Namespace="System.Globalization"%>
 <%@Import Namespace="System.Threading"%>
 <html>
 <head><Title>Dynamic Culture sample - French Language in France 
and in Canada.</Title></head>
 <body>
     <h1> French in France </h1>
     <% Dim i as Integer = 10000.00 
       Thread.CurrentThread.CurrentCulture = new      
       CultureInfo("fr-FR")
     %>
   <% Response.Write ("Currency: " & i.toString("C")) %> <BR/>
   <% Response.Write ("Month: " & Now.ToString("MMMM")) %>
 </body>
 </html>

Page that is localized to French language and region as France

The output for the page will be something similar to Fig 2.

Fig 2

Output showing French in France

Notice in Fig 2, that the currency value is automatically set to Euro, the decimal separator is a comma (,) against a dot (.) that we normally use in India. If you land in Paris and see a coffee which would cost you 1,35 Euros, don't panic; it is only 1.35 Euros (1 followed by 35 after the decimal separator) and not 135 Euros! The month is javier (the word for January in French).
Continuing with the same code in Code 3, change the CultureInfo line to something like this 
 
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-CA")
Code to set French Language and Country as Canada
Now you will notice that the currency value changes to $ (Representing the currency used in Canada), but still the name of the month continues to be javier as the language is French.

Customizing for the User
To improve the user experience our web, application should automatically be able to identify the user's preferred culture and render accordingly. To identify the user's preferred culture, you can use Request.UserLanguages() collection, that returns a set of culture from the browser in the order of priority. Your application should always try to render the content for the language that is there on the top, or the next or the next until it finds a culture that it can understand. If no match is found, then it can settle for the default culture set in the web.config or OS. 

Output showing French in Canada

To use the browser setting we need to use CultureInfo.CreateSpecificCulture instead of the new CultureInfo that we used in Code 3.
 
<%Thread.CurrentThread.CurrentCulture = 
 CultureInfo.CreateSpecificCulture(Request.UserLanguages(0))%>

Code to read the browser preferred language and set CultureInfo accordingly

Resource Files
So far we have seen how to use the CultureInfo to work with Date, Numbers & Currency formatting. The important part in a web application is the content that is used in web pages. Ideally for your application to be truly localized, these should also be in the native language. 

Instead of keeping the localized content in ASPX files, it is better you keep them in Resource Files (.RESX). RESX files can be created either with RESGEN utility that ships with Dot Net framework or with VS.NET. Once you have created these files for each language you should place them in individual folders with names as in CultureInfo names. For example, to put a Marathi Resource file, create a folder mr-IN and place the resource files in that. 

Setup

Adding support for Indic and East Asian Languages in Windows XP

The code samples in this article are tested with ASP.NET 1.1 running on a Windows XP SP1 machine and it is compatible with ASP.NET 1.0. For the samples to work fine, you should ensure that the Supplemental Language support for Indic and East Asian Languages in Language Tab located in the Regional Settings applet in Control Panel is selected. This installs the support files for rendering Indian Languages like Hindi and other Asian Languages like Japan. To add a culture as your preferred language, in Internet Explorer, go to Tools->Internet Options, General Tab and click on the Languages button. 

Settings of the preferred language in Internet Explorer


Summary
With this, we have seen how to start using the CultureInfo Class and use the localization support built in Dot Net framework to build truly World-Ready ASP.NET applications. To learn more on this subject, you may use the references given. 

References
1. www.bhashaindia.com/knowledge/localaizability/ index.aspx 1
2. Developing International Software (2nd Edition) - MS Press
3. http://www.asp.net/tutorials/quickstart.aspx 

 

 

 

Regards,

Deven Goratela

 



Note: This Group is not a Job Searching Group, so please co-operate and dont transfer any kind of job related material across this Group.AnyOne doing so can be banned from the Group
Thanx , Group Co-Ordinators




Yahoo! Groups Links


--
--
Regards...
/*******************************
itsDifferent
http://its-Different.blogspot.com
http://groups.yahoo.com/group/itsdifferent/join
http://groups.msn.com/its-Different/join
http://groups-beta.google.com/group/itsdifferent
*******************************/

1 comment:

job opportunitya said...

Irresistible blog. I look for blogs like this one
when time allows me to. I enjoyed the site and I'll
check it next time!
Please examine my plastic surgery nj blog as soon as you can.