Creating Multi-Language “Hello World!” Kiosk Page using C# and ASP.NET
Apr 7, 2010
Today’s blog is a continuation of my last blog on globalizing “Hello World!” in ASP.NET. In this post I want to add functionality to the Web page that allows the user to change the displayed language of the page using a drop-down list. By default the page selects which language to display based on the language setting of the browser making the request. This post’s example is something you might do if you had a public kiosk and wanted to allow the user to choose the display language of the page without having to configure the browser or the operating system. Another use would be to test the local resource files of the page and make sure they are being displayed properly.
In my last post I had created the following aspx page:
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Greeting.aspx.cs"
2 Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>
4 <!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN"
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head runat="server">
10 <form id="form1" runat="server">
13 <asp:Label ID="lblTitle" runat="server" Text="Greeting Page"
14 meta:resourcekey="lblTitleResource1" /></h1>
16 <asp:Label ID="lblPara1" runat="server" Text="Hello World!"
17 meta:resourcekey="lblPara1Resource1" /></p>
Along with the aspx page I had created local resource files for different language interpretations of the label control’s Text properties, specifically Dutch and Japanese. The local resource files were stored in a folder named “App_LocalResources” off of the Web application’s root. The following screenshot shows the layout and file names:
To allow selection of the displayed language I will add the following drop-down box to the top of the page and add the languages that I am supporting as items and the culture abbreviations as the values.
12 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
13 <asp:ListItem Value="en">English</asp:ListItem>
14 <asp:ListItem Value="nl">Dutch</asp:ListItem>
15 <asp:ListItem Value="ja">Japanese</asp:ListItem>
To change the language displayed to be something different that the browser’s culture you need to set the UICulture property of the Thread building the page. The trick is you have to do this during the early stages of the page’s life cycle. I.e. you can’t just change it in the page’s load event. Microsoft makes this easy though by exposing the virtual method “InitializeCulture” that you can override. The following code is an example that overrides “InitializeCulture” and reads from the Request object to get the selected language and set the UICulture appropriately.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.UI;
6 using System.Web.UI.WebControls;
7 using System.Threading;
8 using System.Globalization;
10 public partial class Greeting : System.Web.UI.Page
12 protected void Page_Load(object sender, EventArgs e)
16 protected override void InitializeCulture()
18 if (Request.Form["DropDownList1"] != null)
20 String selectedLanguage = Request.Form["DropDownList1"];
21 Thread.CurrentThread.CurrentUICulture =
Note that you cannot access the dropdown list control directly because at this point in the page’s life cycle the controls have not been initialized. You have to get the selected value of the dropdown list from the raw Request object.
That’s it! You now have a page that can be rendered on the fly in any language you are willing to translate. I suppose the listbox might be a problem since it only displays choices in English. I’ll leave that up to your own creativity to fix…
If you need to build multi-language ASP.NET applications, we can customize our ASP.NET class to cover this content for you.