Saying “Hello World!” in Your Language using C#

Microsoft and Visual Studio make it relatively easy to create Multi-language Web pages.  The first step is to create a Web page in Visual Studio.  The only catch is that for ASP.NET to handle your language translations you have to use Web Server controls and all of your text has to be through the properties of those controls.  I.e. be prepared to use a lot of label controls.  The following is a view of the source code of a simple page that we want to be multi-lingual.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Greeting.aspx.cs" Inherits="Greeting" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            <asp:Label ID="lblTitle" runat="server" Text="Hello World!" /></h1>
        <p>
            <asp:Label ID="lblPara1" runat="server" Text="HelloWorld!" /></p>
    </div>
    </form>
</body>
</html>

Notice the use of label controls instead of straight text inside the header 1 and paragraph elements.

The next step is to get Visual Studio to generate a local resource file.  This step has to be done in design view, the menu option is not available in source view.  From the Visual Studio menu select Tools-Generate Local Resource.  The result should be a new file created with the same name as the original file except with a .resx extension.  In this example it would be Greeting.aspx.resx.  This file is automatically placed into a folder named “App_LocalResources”.  The contents of the resx file for this simple web page is:

Default language resource file

Inside the original aspx page Visual Studio has made the following changes:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Greeting.aspx.cs" Inherits="Greeting"
    Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>
            <asp:Label ID="lblTitle" runat="server" Text="Greeting Page"
                       meta:resourcekey="lblTitleResource1" /></h1>
        <p>
            <asp:Label ID="lblPara1" runat="server" Text="Hello World!"
                       meta:resourcekey="lblPara1Resource1" /></p>
    </div>
    </form>
</body>
</html>

Note the new attributes added to the @Page directive and the meta:resourcekey attribute added to the Server controls.  These attributes are necessary for ASP.NET to perform the magic of automatically inserting language appropriate text into these controls.

The next step is to copy the rename the resx file to as many language files that you are prepared to translate.  For example,  Greeting.aspx.nl.resx would be the name of the Dutch translated resource file.  The contents would need to be edited to something like this:

Dutch language resource file

Here is another example for Japanese, make sure the resource file is named Greeting.aspx.ja.resx:

Japanese language resource file

Once this is completed ASP.NET will automatically use the proper resource file based on the default language of the browser visiting the page.  That is about all there is to creating a multi-lingual “Hello World” web page.

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.