Internationalize Your ASP.NET Applications (Part 1 of 3) : Page 3

Not only does ASP.NET simplify Web application development, it also provides comprehensive support for internationalization.

by Ollie Cornes

Oct 21, 2002

Page 3 of 6

Map Content to Classes
The sample site for this article series is a Web-based content viewer built using ASP.NET that provides news articles in several languages in parallel. The content is stored in several different languages and is displayed to the Web site visitor in the correct language depending on their preference. The site ensures that all article text, page text, dates and images are displayed for the specified culture.

The news site stores its articles in a database, which is common in sites of this type, because it simplifies building management interfaces for editors and journalists. The first step in putting the application together is to build a user interface to display the news stories. The problem lies in controlling which language version is displayed. Here are the simple requirements for the site:

The site displays a series of news articles in the selected language (if available).

Each article consists of a unique identifier, title, body, and publication date

As this is an example, there will be no interface to submit, edit, delete or otherwise manipulate articles. The Test data were inserted directly into the database (SQL Server).

A class called Article represents an instance of an article in a specific language. At the start of the Article.cs class file are some directives for the namespaces you'll need:

using System.Data;
using System.Globalization;

The System.Data namespace provides the DataSet class, and the System.Globalization namespace provides classes you use to manage the localization of information. The most important of these is the CultureInfo class, which represents a culture and which you use to specify the culture to be used to display a specific page.

The Article class has private members used to store the identifier, title, body, publication date and culture (each Article instance represents a piece of content in a specific culture).

Each row in the dataset represents a version of an article in a specific language, and each version includes the article identifier, date, title, body and a culture identification string.

Author Note: Although both Access and SQL Server support Unicode, you may need to configure your development machine to display characters in foreign languages by installing additional language support. To do this in Windows look in Control Panel at the Regional settings and you will find an area marked "Language settings for the system". You will need to select each language that you want to use. Clearly, which items you need will vary depending on the current configuration of your machine and the languages with which you intend to work.

The Article class has an overloaded constructor that accepts an integer identifying a specific article and a System.Globalization.CultureInfo object that represents the culture, and returns an Article object.

public Article(int articleID, CultureInfo culture)
{
}

A SQL query in the constructor uses the two parameters to retrieve the relevant translation of the article.

So, the Article class retrieves an article by formulating the SQL statement and passing that to the Database object, which calls the GetDataSet() method:

DataSet dataSet = Database.GetDataSet(Sql);

If the query returns no rows then there is no matching article in the specified culture. In this case, this next block of code runs another SQL query to search for an English version (the default language in the application).

The last line of the constructor uses the CreateSpecificCulture() method to create a new CultureInfo object that represents the culture of the article The CreateSpecificCulture method takes a culture string such as 'en-US" and returns a CultureInfo object representing the most appropriate culture.