Globalisation in ASP.Net

Currently, when surfing websites, is there one common thing that you notice? Many websites are bilingual or multilingual. The reason behind this is that when we upload a website to the internet world, all over the world people can surf our website content. And people would appreciate if content is their own regional language. For implementing multilingual functionality in your website, you need to use globalization in your website.



So let’s start implementation of globalization in our website.

  1. Open Visual Studio
  2. "File" >> "New">> "WebSite".
  3. Select "Empty WebSite", Click "Ok".
  4. Right-click on the solution in the Solution Explorer then select "Add" >> "Add ASP.NET Folder" >> "App_GlobalResources".
  5. Select App_GlobalResource >> Add >> Resource File

Resource Files

A resource file is an XML file that contains the strings that you want to translate into various languages or paths to images. The resource file contains key/value pairs. Each pair is an individual resource. Key names are not case sensitive. For example, a resource file might contain a resource with the key Button1 and the value Submit.

In this example, I will create the following 3 resource files:

  1. For English: Resource.resx
  2. For Hindi: Resource.hi.resx
  3. For Spanish: Resource.es.resx

So first create 3 resource files, and please make all keys names the same and change the values.

  1. Resource.resx (for English):



  2. Resource.hi.resx (for Hindi):



  3. Resource.es.resx (for Spenish):


Add a new MasterPage, right-click on the solution in the Solution Explorer then select "Add" >> "MasterPage".

MasterPage.master

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" 
Inherits="MasterPage" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

    <asp:ContentPlaceHolder ID="head" runat="server">

    </asp:ContentPlaceHolder>

</head>

<body>

    <form id="form1" runat="server">

        <div>

            <table>

                <tr>

                    <td>

                        <asp:Label ID="lblChageLang" runat="server" Text="Change Languages">
</
asp:Label>

                    </td>

                    <td>

                        <asp:DropDownList ID="ddlLanguage" runat="server" 
OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged" AutoPostBack="True">

                            <asp:ListItem Text="English" Value="en-Us"></asp:ListItem>

                            <asp:ListItem Text="Hindi" Value="hi-IN"></asp:ListItem>

                            <asp:ListItem Text="Spenish" Value="es-MX"></asp:ListItem>

                        </asp:DropDownList>

                    </td>

                </tr>

            <br/>

            </table>

            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

            </asp:ContentPlaceHolder>

        </div>

    </form>

</body>

</html>

MasterPage.master.cs

using System;

using System.Collections.Generic;

using System.Globalization;

using System.Linq;

using System.Threading;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

public partial class MasterPage : System.Web.UI.MasterPage

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            if (Session["SelectedLang"] != null)

            {

                ddlLanguage.SelectedValue = Session["SelectedLang"].ToString();

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture
(ddlLanguage.SelectedValue);

                Thread.CurrentThread.CurrentUICulture = new CultureInfo
(ddlLanguage.SelectedValue);

            }

            else

            {

                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture
(ddlLanguage.SelectedValue);

                Thread.CurrentThread.CurrentUICulture = new CultureInfo
(ddlLanguage.SelectedValue);

            }

        }

    }

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)

    {

        if (ddlLanguage.SelectedValue == "hi-IN")

        {

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("hi-IN");

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("hi-IN");

            Session["SelectedLang"] = ddlLanguage.SelectedValue;

            Server.Transfer(Request.Path);

            

        }

        else if (ddlLanguage.SelectedValue == "es-MX")

        {

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("es-MX");

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("es-MX");

            Session["SelectedLang"] = ddlLanguage.SelectedValue;

            Server.Transfer(Request.Path);

        }

        else

        {

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");

            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

            Session["SelectedLang"] = ddlLanguage.SelectedValue;

            Server.Transfer(Request.Path);

        }

    }

}

In MasterPage, you can see there is a dropdownlist , where there are 3 list items for the English, Hindi and Spanish languages. I put a dropdownlist in the masterpage so  we can see this dropdown in all pages.

Default2.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" 
CodeFile="Default2.aspx.cs" Inherits="Default2" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

    <table>

        <tr>

            <td>

                <asp:Label ID="lblName" runat="server" Text="<% $Resources:Resource,Name %>"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="lblFname" runat="server" Text="<% $Resources:Resource,FName %>"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="lblMname" runat="server" Text="<% $Resources:Resource,MomName %>"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtMName" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="lblAddress" runat="server" Text="<% $Resources:Resource,Address %>"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="lblPhone" runat="server" Text="<% $Resources:Resource,Phone %>"></asp:Label>

            </td>

            <td>

                <asp:TextBox ID="txtPhone" runat="server"></asp:TextBox>

            </td>

        </tr>

 

    </table>

</asp:Content>

In the preceding page, look at the label Text . Text=”<% $Resources.Resource,Address %>”

<asp:Label ID="lblAddress" runat="server" Text="<% $Resources:Resource,Address %>"></asp:Label>

How it works

As you already know, a resource file is a collection of key and value pairs. In each resource file, we define a culture. For example for the Hindi resource file I usee the name Resource.hi.resx. When you define a key in a resource file, all keys would be the same except the value will change based on the language.

Like in the all 3 resource files above, the address key is common but the value is different in all the files.

When you create a label text file <%Resources:Resource,Address%> Here the address is the key, based on the selected dropdown value, you see the changes.

Press F5, and run your code.


Up Next
    Ebook Download
    View all
    Learn
    View all