Creating Multi-Language “Hello World!” Kiosk Page using C# and ASP.NET

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" 
                                Inherits="Greeting"
    2     Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>
    3
    4 <!DOCTYPE html PUBLIC "-//W3C//DTD
                    XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    5 <html xmlns="http://www.w3.org/1999/xhtml">
    6 <head runat="server">
    7     <title></title>
    8 </head>
    9 <body>
   10     <form id="form1" runat="server">
   11     <div>
   12         <h1>
   13             <asp:Label ID="lblTitle" runat="server" Text="Greeting Page"
   14                        meta:resourcekey="lblTitleResource1" /></h1>
   15         <p>
   16             <asp:Label ID="lblPara1" runat="server" Text="Hello World!"
   17                        meta:resourcekey="lblPara1Resource1" /></p>
   18     </div>
   19     </form>
   20 </body>
   21 </html>

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:
Local Resource Folder Contents
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>
   16 </asp:DropDownList>

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;
    9
   10 public partial class Greeting : System.Web.UI.Page
   11 {
   12     protected void Page_Load(object sender, EventArgs e)
   13     {
   14
   15     }
   16     protected override void InitializeCulture()
   17     {
   18         if (Request.Form["DropDownList1"] != null)
   19         {
   20             String selectedLanguage = Request.Form["DropDownList1"];
   21             Thread.CurrentThread.CurrentUICulture =
   22                 CultureInfo.CreateSpecificCulture(selectedLanguage);
   23             base.InitializeCulture();
   24         }
   25     }
   26 }

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.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.