Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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

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




Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

The application displays articles on the page using a server control. The server control has a single property that represents the article number. The control automatically selects the correct culture when it displays the article (more on this later). The server control class starts with the definition of a private member variable to hold the article identifier and a public property to manipulate that value.

using System.Globalization; using System.Threading; public class ArticleControl : System.Web.UI.Control { private int articleID; public int ArticleID { get { return articleID; } set { articleID = value; } }

The server control's CreateChildControls() method creates the HTML tags by adding controls to the page. First it extracts the culture in which the page is executing as a CurrentUICulture object using the Thread object in the System.Threading namespace. You will see how to manipulate this setting later in the article.

protected override void CreateChildControls() { CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

The CreateChildControls() method calls the Article class constructor and passes the culture value and the article identifier (from the ArticleID property) to create the Article object that will be displayed on the page.

Article myArticle = new Article(articleID,culture);

The Article object (myArticle) renders the page by creating a series of LiteralControl objects that squirt HTML out to the browser through the HTTPResponse object.

LiteralControl literal; literal = new LiteralControl("<h1>" + myArticle.Title + "</h1>"); Controls.Add(literal); literal = new LiteralControl("<b><i>" + myArticle.PublicationDate.ToLongDateString() + "</i></b><br /><br />"); Controls.Add(literal); literal = new LiteralControl(myArticle.Body + "<br />"); Controls.Add(literal); }

Using the server control, it's very easy to insert an article into a page in a Web application. For example:

<%@ Page language="c#" %> <%@ Register TagPrefix="NewsSite" Namespace="Multilingual.Controls" Assembly="Multilingual" %> <html> <body> <NewsSite:ArticleControl runat="Server" id="ArticleCtrl" ArticleID="1" /> </body> </html>

The three-tier design of this site keeps the program logic in the business objects and reserves the .aspx files for the interface and layout. The @Register directive in the Web Form specifies where .NET should look for controls. In this case the @Register directive means "When you come across any server controls, look in the Multilingual.Controls namespace to see if there is a matching class".

You insert the <NewsSite:ArticleControl> tag into the page with its ArticleID property set to an ID that matches one of the articles. If you set the ArticleID property to a value that does not match an article, the Article class throws an exception. Figure 2 shows the result of the preceding <NewsSite:ArticleControl> tag example.

Figure 2: A typical article displayed by adding a <NewsSite:ArticleControl> tag to a Web Form.

To view different articles you can change the article identifier on the query string. The culture is taken from the thread in which the page code is running. You can change the culture setting using the Culture property in the @Page directive. For example, you can change the first line of the page to include the Culture and UICulture attributes as follows.

<%@ Page language="c#" Culture="fr-FR" UICulture="fr-FR" %>

After making the change, the page displays the same article (see Figure 3), but in French ('fr-FR' is actually French as spoken in France). Notice that not only has the title and body text switched languages, but the page also displays the date formatted appropriately for French readers. Later you will see how we can dynamically change the culture setting.

Figure 3: The same article displayed in French.

Changing the Culture setting in the @Page directive affects the way the date is displayed, but changing the UICulture setting alters the language of the article itself. The former sets the culture for the display of information, and the latter specifies how the user interface should be shown. Notice also that if the culture is invalid the page throws an exception and that if you enter a culture code for which there is no matching article the page displays the English version.

Many Web applications using .NET hook the front-end directly to the database (rather than using business objects) because of the ease and speed with which it's possible to create interfaces using server controls like the DataGrid and DataList. This approach works well for most Web sites, however you must be aware that for larger Web sites this architecture is inappropriate and it is necessary to move the data access code into business objects and out of the code behind.

This method demonstrates how to use the @Page directive to specify that a page in the site should always be displayed in a certain language, whatever the culture settings. That's not always useful. You want to be able to change the language and culture dynamically. The next section discusses a means of providing site visitors with an interface to choose their own culture.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date