Meditation and Mindfulness for Software / IT Professionals. Conducted by Bipin Joshi in Thane. Read more...

Developing multilingual applications in ASP.NET (Part - I)

Introduction

Developing a multilingual applications always requires careful thought on the method used to implement multilingual support. .NET provides some easy to implement features that help in the overall process. In this part of the article we will see the commonly used terms in this context and how ASP.NET provides out of the box support for developing multilingual applications.

The classic way

Let us assume that you want to develop a web site in two different languages - English and Marathi. Naturally, all the content of these sites will be in the respective language. The content includes static text, form labels, prompts, error messages and similar things. In addition you would also like to take care of things such as date formats and number formats. Typically you will choose one of the following approaches to develop such web sites:

You will develop all together separate pages for English version and Marathi version. The advantage of this method is that you have two distinct sets of pages. You can host them easily in two separate virtual roots or folders. The biggest drawback of this method is that keeping both the sets in sync even after a minor change is very difficult. Maintaining or extending such sites is a difficult task.

You can store all the content in different languages in database tables. At run time you will retrieve the content corresponding to a language from this table. Similarly, based on the language chosen you will format date/time and numbers. This approach solves the problem to a large extent but still requires more coding and database storage.

You store all the content in resource files. A resource is a non-executable data that is required by the application and is deployed along with the application. e.g. Bitmaps, icons, cursors, screen labels. However, for traditional VB/ASP developer creating and consuming resource files was not an easy task.

Before going in details of how to create resources, however, we need to understand what is meant by "globalization", "localization" and "culture" in this context.

Globalization

Globalization is a process of identifying all the parts of your application that need to be different for respective languages and separate them from the core application.

Localization

Localization is process of creating and configuring your application for a specific language.

Culture

A culture is the combination of the language that you speak and the geographical location you belong to. It also includes the way you represent dates, times and currencies.  A culture is represented as shown in the following example:

en-US
en-GB
fr-BE

The first culture represents English language being spoken in US where as the second culture represents English culture being spoken in Great Briton.

Relevant Namespaces

To work with culture information .NET provides a class called CultureInfo that resides in System.Globalization namespace. The culture is set at thread level so you also need to work with Thread class from System.Threading namespace. Finally, for working with resources you need to use classes from System.Resources.

Get or set information about user culture

The current culture of a user can be retrieved by using CultureInfo.CurrentCulture property of the class.

The culture can be set for a thread using Thread.CurrentThread.CurrentCulture property.

Dim ci as CultureInfo
ci=CultureInfo.CurrentCulture
Response.Write(ci.Name)

Following example shows how to set culture information:

Dim ci as CultureInfo
ci=new CultureInfo("en-GB")
Thread.CurrentThread.CurrentCulture = ci
Response.Write(DateTime.Now)

In the above example since we set the culture to "en-GB" and date format used in Great Briton is dd/MM/yyyy you will get the out in that format.

Setting culture for a page

You can very easily set culture for a single ASP.NET page by setting the Culture attribute of the @Page directive.

<%@ Page language="c#" Culture="mr-IN"%>

Here, we set the culture for the page to mr-In (Marathi - India). If you now put a calendar web control on the web form then at run time it will look like this:

Setting this property at page level automatically set the culture for current thread. So, all the dates and number formats are displayed according to that culture. You can check that by outputting current date via Response.Write statement (Response.Write(DateTime.Now)).

Setting culture at web application level

You can also set the culture for entire web application using web.config file. The web.config <globalization> section allows you to do that. Following example shows the markup:

<globalization culture="en-GB" />

Summary

In the Part - I of the series we saw how .NET provides easy to use classes and configuration settings that you can use to develop multilingual applications. In the next part we will see how to create and consume resource files in .NET.

 




Bipin Joshi is a software consultant, an author and a yoga mentor having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 30 Aug 2003



Tags : ASP.NET Web Forms Visual Studio Components