I have a webform which has localization. I have added spanish as the second language of the webform and its perfectly working. But i found a problem with this, i can only start the webform in one language either in english or spanish, but i want to change it at run time.
I want to further improve this by adding the following features to the webform
1) Select language through drop down box (either english or spanish) at runtime.
2) Display the default language english as first and if the user wants to change it the user should select from drop down box
Are the above 2 mentioned features possible to add? if so please show how.
Here is my webform code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="About_Company.aspx.cs" Inherits="AutoMobileWebsite.AboutUs" UICulture="es-ES"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Company</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label id="lblAboutCM" runat="server" meta:resourcekey="lblAboutCM"><h1>About Company :</h1></asp:Label>
</div>
<div>
<asp:Label id="lblContent" runat="server" meta:resourcekey="lblContent"> <h3> The company was established in 2014 by the founder Rex Chris.
The intention of this website is provide our customers and online virtual car sale which
anytype of customer can buy their dream vehicle or sell the existing vehicle
</h3>
</asp:Label>
</div>
</form>
</body>
How do i achieve the above mentioned features ?
Thank you for your time
ASPX:
<form id="form1" runat="server">
Language:<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="English">English</asp:ListItem>
<asp:ListItem>Spanish</asp:ListItem>
</asp:DropDownList>
<div>
<asp:Label id="eng_lblAboutCM" runat="server" meta:resourcekey="lblAboutCM"><h1>english about :</h1></asp:Label>
<asp:Label id="sp_lblAboutCM" runat="server" meta:resourcekey="lblAboutCM"> <h1>spanish about :</h1></asp:Label>
</div>
<div>
<asp:Label id="eng_lblContent" runat="server" meta:resourcekey="lblContent"> <h3> english content
</h3>
</asp:Label>
<asp:Label id="sp_lblContent" runat="server" meta:resourcekey="lblContent"> <h3> spanish content
</h3>
</asp:Label>
</div>
</form>
Code behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ChangeLanguage();
}
private void ChangeLanguage()
{
foreach (var item in form1.Controls)
{
if (item is Label)
{
Label lbl = (Label)item;
lbl.Visible = false;
if (lbl.ID.StartsWith("eng") &&
DropDownList1.SelectedItem.Text == "English")
{
lbl.Visible = true;
}
else if (lbl.ID.StartsWith("sp") &&
DropDownList1.SelectedItem.Text == "Spanish")
{
lbl.Visible = true;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ChangeLanguage();
}
}