In this Post I will Explain you following Points:-
<%@ 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> </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"> </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"> </td>
<td> </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 the selected culture in a session because I need this session value in the 'Registration.aspx' page to initialize the culture.
I will do the same thing for the registration page.
For changing the text of the submit button, we are using a global resource so that the registration page can also use the 'Submit' key from the global resource file. This way, we don’t have to include the 'Submit' key in every local resource; I've made this key a global key for all pages.
<%@ 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> </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 the text of the submit button is in Italian, which is coming from the global resource file. Therefore, if you want to use any key in multiple pages, it's advisable to put this key in the global resource file.