SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software Kodyaz Development Resources
Development resources, articles, tutorials, samples, codes and tools for .Net, SQL Server, Windows, Windows Phone, SAP and ABAP, like SAP UI5, Screen Personas, etc.




Localization Sample ASP.NET Web Application


Localization is one of the important success factors for a Web application or a Winform application if it is desired to run as a multi-language application. ASP.NET and VB.NET applications has the advantage of Globalization namespace and Resources namespace. If you are using Microsoft Visual Studio .Net 2003 or Visual Studio 2005 for developing your dotnet applications, you might have used the System.Globalization namespace with its CultureInfo class and System.Resources namespace with its  ResourceManager class.

In this article, I will try to summarize the steps to create a web site which uses resource files for different cultures using Microsoft Visual Studio 2005 (code name Whidbey). We will create the web site project, form the resource files for a few different cultures. And then we will prepare the code behind for sample aspx web pages which will use the localization resources.


Creating Sample Asp.NET Web Site LocalizationSample

In order to create a web site using Visual Studio 2005, you can follow the steps as shown in the below picture. From the main menu of the Visual Studio development environment (VS 2005 IDE), select File > New > Web Site... menu items. In Visual Studio .NET 2003, there is not the Web Site menu item. You can create web sites using the ASP.NET Web Application project template which you can specify after selecting the Project... menu item.

asp.net localization sample

Web Site... menu item is new with the Visual Studio 2005. After the web site is selected, installed web site templates are shown as a list shown as below:

asp.net localization sample

For the sample web application, I selected ASP.NET Web Site template and set the localization as C:\LocalizationSample. Here the location we set for our sample web site project is the place where the source codes of the project will be placed. Note that the place of the web site and its source codes are different places. Here in this step where we are creating the sample application, the place of the source codes of the project are defined. We will specify the location of the web site during publishing the project to a web server.

I will develop the sample application using Visual Basic. But if you want you can change the development language for this sample application by selecting Visual C# or Visual J# from the Language dropdownlist.

 Click OK button to finish creating the web site project. Here is the initial view of the new web site project LocalizationSample.

Solution Explorer

This ASP.NET 2.0 project contains a Default.aspx file, a code behind file Default.aspx.vb for the page, and the web.config configuration file.





How to code for Localization and Globalization

First switch to the design view of the default.aspx web page.

We will design a web page where the user can set the default language of the application. When the default language is selected the text on labels will be replaced according to the selected culture and the resources of that language. Also the user will be available to select a resource string from a dropdownlist and a language or culture from an other list and get the resource's translated string. asp.net localization sample

I created a table and placed the standart controls from the toolbox in the VS 2005 IDE into the table cells. I'm adding the html codes of the table for you to imagine the purpose of this web page design. You can focus on the ID's of the controls in order to see what they are be used for.

<table>
        <tr>
                <td style="width: 100px">
                        <asp:Label ID="lblCountryCulture" runat="server" Text="Label" Width="143px"></asp:Label></td>
                <td style="width: 100px">
                        <asp:DropDownList ID="DropDownListDefaultCulture" runat="server" Width="145px"></asp:DropDownList></td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
        </tr>
        <tr>
                <td style="width: 100px">
                        <asp:Label ID="LabelResource" runat="server" Text="Label" Width="143px"></asp:Label></td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
                <td style="width: 100px">
                </td>
        </tr>
        <tr>
                <td style="width: 100px">
                        <asp:DropDownList ID="DropDownListResource" runat="server" Width="145px"></asp:DropDownList></td>
                <td style="width: 100px">
                        <asp:DropDownList ID="DropDownListCulture" runat="server" Width="143px"></asp:DropDownList></td>
                <td style="width: 100px">
                        <asp:Button ID="btnDisplay" runat="server" Text="Button" Width="105px" /></td>
                <td style="width: 100px">
                        <asp:Label ID="lblSampleText" runat="server" Text="Label" Width="115px"></asp:Label></td>
        </tr>
</table>

The code behind page of the Default.aspx contains only the code declarations below.

Partial Class _Default
Inherits System.Web.UI.Page

End Class

Before editing the code behind, we can begin forming our resource files for different cultures. To add a new resource file, click on the web site project location in the solution explorer window. Open the context menu item by a right click on the project name. And select the "Add New Item..." from the menu. This selection will open the "Add New Item" dialog screen. In the screen you will see a big number of item templates that can be added to the project. Search the list for the Resource File item template. You will find the below icon which is representing the Resource File template and click over the icon.

resource file
Leave the name Resource.resx as it is suggested by default by Visual Studio 2005. Click Add button to add this new .Net resource file. When you click the Add button, a message will be displayed for your confirmation.

App_GlobalResources

The message is:

You are attempting to add a resource to an ASP.NET application. For a resource to be generally consumable in your site, it should be placed inside the 'App_GlobalResources' folder. Would you like to place the resource in the 'App_GlobalResources' folder?

Since we want for our resources to be reachable within our site, or consumable generally in our web site, we will confirm the message for placing the resource in 'App_GlobalResources' folder. Note that although the folder App_GlobalResources is not existing currently in the web site project, it will be added to the project as well as the resource file Resource.resx

I also added a resource file for each culture named like Resource.en-US.resx, Resource.ru-RU.resx, Resource.tr-TR.resx and Resource.zh-CN.resx

And in every resource file I added four string type resources named

Partial Class _Default
        Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
                DropDownListDefaultCulture.AutoPostBack = True
                FillDefaultCultureDropDownList()
                FillResourceDropDownList()
                DropDownListDefaultCulture_SelectedIndexChanged(Nothing, Nothing)
        End If

End Sub

Private Sub FillDefaultCultureDropDownList()

        DropDownListDefaultCulture.Items.Add("en-US")
        DropDownListDefaultCulture.Items.Add("ru-RU")
        DropDownListDefaultCulture.Items.Add("tr-TR")
        DropDownListDefaultCulture.Items.Add("zh-CN")

        DropDownListCulture.Items.Add("en-US")
        DropDownListCulture.Items.Add("ru-RU")
        DropDownListCulture.Items.Add("tr-TR")
        DropDownListCulture.Items.Add("zh-CN")

End Sub

Protected Sub DropDownListDefaultCulture_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles     DropDownListDefaultCulture.SelectedIndexChanged

        ' Code for CultureInfo
        Dim cultureInfo As System.Globalization.CultureInfo
        cultureInfo = New System.Globalization.CultureInfo(DropDownListDefaultCulture.SelectedValue)

        ' Code for Setting the CurrentCulture
        System.Threading.Thread.CurrentThread.CurrentCulture = cultureInfo
        System.Threading.Thread.CurrentThread.CurrentUICulture = cultureInfo

        SetLabels()

End Sub

Private Sub SetLabels()

        lblCountryCulture.Text = Resources.Resource.Country
        LabelResource.Text = Resources.Resource.ResourceString
        btnDisplay.Text = Resources.Resource.Display
        lblSampleText.Text = ""

        btnDisplay_Click(Nothing, Nothing)

End Sub

Private Sub FillResourceDropDownList()

        DropDownListResource.Items.Add("Country")
        DropDownListResource.Items.Add("CultureCode")
        DropDownListResource.Items.Add("Display")
        DropDownListResource.Items.Add("ResourceString")

End Sub

Protected Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

        ' Code for creating Resource Manager
        Dim rm As System.Resources.ResourceManager
        rm = Resources.Resource.ResourceManager

        ' Code for Selecting Resource
        Dim tempCultureInfo As System.Globalization.CultureInfo
        tempCultureInfo = New System.Globalization.CultureInfo(DropDownListCulture.SelectedValue)

        lblSampleText.Text = rm.GetString(DropDownListResource.SelectedValue, tempCultureInfo)

End Sub

End Class
 

Since because of a file corruption in downloaded files section, the sample project download of this ASP.NET localization tutorial is moved from http://www.kodyaz.com/files/21/sample_localization_projects/entry382.aspx to Localization Sample Web Site project for Visual Studio 2005.




download SQL Server 2012 for free evaluation






Copyright © 2004 - 2017 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems