In this Post I will Explain you following Points:-
  • Globalization and Localization in ASP.NET,
  • Globalization and Localization in ASP.Net C# web applications  with example.
If your website is designed and developed to support a single language and culture,but you want make your website or web application to support different languages and cultures then you can use the concept of Globalization and Localization in ASP.Net Web Applications.
Your  website supports globalization,that  means your web application can show it contents based on the language preference set by a user in his browser.Using ASP .net you can achieve  Globalization and Localization very easily through the use of resource file.Resource Files:-
A resource file is a XML file that contains Key (or you can say strings) that you want to translate into different languages.Basically you can say that resource file contain key/value pair like dictionary in c#.Resource files have an .resx extension.

In ASP .net resource files are two type:-
a.global resource
You can  create a global resource file by placed it in the reserved folder App_GlobalResources at the root of the web application.
Any .resx file  in the App_GlobalResources folder has global scope means you can use this resource on any page in the web application.
b.local resource 
Localization  translate a  particular pages into different languages and cultures.local resource is specific to a particular page only.You can say that an ASP. NET file that has a file-name extension of .aspx, .ascx, or .master.
Local resource is use for the  particular page means In your web application each and every page can have local resource.Global resource placed in App_GlobalResources folder in a website and  a local resource placed in App_LocalResources folder.
  • Open visual studio add new empty website
  • File>New>Web Site then select “C#”(left side in visual studio)  “Empty Web Site”
name it as you want.
now add a aspx page in your website.
 I have designed the aspx page.I have a drop down which contain different languages which my website support.
 I have created a simple login page and which contains link for the registration page.
 
 now I have added one more aspx page in my  website and name it Registration.
 
 Now I want design my web application to support different languages and cultures
 Now right click on your website and add resource file.visual studio  will create App_GlobalResources Folder which contains these resource file.
 
I have added three resource file
Format For global resource files:
name.language.resx
name.language-culture.resx
 Resource.resx
 Resource.da-dk.resx(for denis language)
 Resource.it-IT.resx (for italian language support)
 
these are global resource files Now add local resource for Login and registration page.
 1.add a new folder and name it App_LocalResources
 2.right click on this and  add  resource file
Format For local resource files:
pageOrControlName.extension.language.resx
pageOrControlName.extension.language-culture.resx
Now put common strings in global resource file so that you can use this key any where in your application.
open global resource file and add key/value pair
I want to use “submit” keyword in login and registration both page that’s why I have put this in global resource file.
Now open local resource file and add key/value pair for particular page
now add key,value pair for the login page
12,13
also add key/value for the Registration page
How add resource key  to the html element.
Syntax For local resource
Add key two Labels
meta:resourcekey=”keyname”
Syntax For global resource
Text=”<%$Resources:ResourceName,keyname%>”

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="auto-style1">
            <tr>
                <td class="auto-style2">Language</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                        <asp:ListItem>Select Language</asp:ListItem>
                        <asp:ListItem Value="da-dk">Denis</asp:ListItem>
                        <asp:ListItem Value="it-IT">Italian</asp:ListItem>
                        <asp:ListItem Value="en-us">English</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label1" runat="server" Font-Size="Larger" ForeColor="#003399" Text="Login Panel" meta:resourcekey="LoginPanel"></asp:Label>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label2" runat="server" Text="Username" meta:resourcekey="password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">
                    <asp:Label ID="Label3" runat="server" Text="Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="<%$Resources:Resource,submit%>" />
                    <asp:HyperLink ID="HyperLink1" runat="server">Registration</asp:HyperLink>
                </td>
            </tr>
            <tr>
                <td class="auto-style2">&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Now we need to override the InitializeCulture function and set the UICulture to the user selected language.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeCulture();
    }

    protected override void InitializeCulture()
    {
        if (Request.Form["DropDownList1"] != null)
        {
            UICulture = Request.Form["DropDownList1"];
            Session["culture"] = Request.Form["DropDownList1"];  //set the UICulture to the user selected language.
        }
        base.InitializeCulture();
       
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        InitializeCulture();
      
    }
}

I have stored selected culture in session because I want this session value in registration.aspx page for initializing the culture.
Do same thing for registration page
for changing submit button text we are using global resource so that registration page can also use submit key from global resource file.
so that we don’t have to put submit key in every local resource I make this key global key for all page.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Registration1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Font-Size="Larger" ForeColor="#0099FF" Text="Register"  ></asp:Label>
                </td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="name" meta:resourcekey="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label3" runat="server" Text="lastname" meta:resourcekey="lastname"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4" runat="server" Text="password" meta:resourcekey="Password"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Submit" />
                </td>
                </tr>
        </table>
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Registration1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeCulture();
    }
    protected override void InitializeCulture()
    {
        
        if (Session["culture"].ToString()!= null)
        {
            UICulture = Session["culture"].ToString();  //set the UICulture to the user selected language from session
        }
        base.InitializeCulture();
    }

}

Now run your application and choose language from drop down


Now click on Registration link

you can see that text of submit is in Italian which is coming from global resource file.
so if you want use any key in multiple pages then put this key in global resource file.

The post How to Create Multilingual Website in Asp .Net appeared first on Software Development | Programming Tutorials.



Read More Articles