ASP.Net Multilingual website to display Multiple Languages within multiple pages in ASP.Net

Last Reply on Oct 04, 2016 05:23 AM By dharmendr

Posted on Oct 03, 2016 08:52 AM

ASP.Net Multilingual website: Support and display Multiple Languages (English French) in ASP.Net

i have use above article in my site

working fine

but my question is when i change to other page language selected default while i want it is not to change until i change from list .

i have master page and number of pages in my site

Posted on Oct 04, 2016 05:23 AM Modified on on Sep 26, 2017 11:16 PM

Hi yogeshc,

I have created sample that full-fill your requirement by refering the below article.

ASP.Net Multilingual website: Support and display Multiple Languages (English French) in ASP.Net

Note : Your each child page must inherit BasePage in order to use multilanguage support.

Resource.hi-in.resx

Name Value
Email ईमेल
Greetings हैलो, मैं मुदस्सर खान हूं
Introduction मैं इस साइट के संस्थापक हूँ, और यह भी एक स्वतंत्र सलाहकार के रूप में काम करते हैं। मैं कंप्यूटर इंजीनियरिंग में स्नातक की डिग्री के साथ मुंबई विश्वविद्यालय से अपनी स्नातक स्तर पूरा किया।
Language भाषा
Name नाम
Save संचय
SignUp पंजीकरण

Resource.resx

Name Value
Email Email
Greetings Hello, I am Mudassar Khan
Introduction I am the founder of this site, and also work as an Independent Consultant. I completed my graduation from Mumbai University with Bachelor’s Degree in Computer Engineering.
Language Language
Name Name
Save Save
SignUp SignUp

HTML

Master.aspx

<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">
    <%=Resources.Resource.Language %>:
    <asp:DropDownList ID="ddlLanguages" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ChangeLanguage">
        <asp:ListItem Text="English" Value="en-us" />
        <asp:ListItem Text="Hindi" Value="hi-in" />
    </asp:DropDownList>
    <hr />
    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
    </asp:ContentPlaceHolder>
    </form>
</body>
</html>

Default.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <asp:Label ID="lblGreeting" Text="<%$Resources:Resource, Greetings %>" runat="server"
        Font-Bold="true" />
    <br />
    <br />
    <span>
        <%=Resources.Resource.Introduction %></span>
    <br />
    <br />
    <asp:Button Text="<%$Resources:Resource, SignUp %>" runat="server" OnClick="SignUp" />
</asp:Content>

Registration.aspx

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <table>
        <tr>
            <td>
                <asp:Label ID="lblName" Text="<%$Resources:Resource, Name %>" runat="server" />
            </td>
            <td>
                <asp:TextBox runat="server" ID="txtName" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lblEmail" Text="<%$Resources:Resource, Email %>" runat="server" />
            </td>
            <td>
                <asp:TextBox runat="server" ID="txtEmail" />
            </td>
        </tr>
        <tr>
            <td colspan="2" align="center">
                <asp:Button Text="<%$Resources:Resource, Save %>" runat="server" />
            </td>
        </tr>
    </table>
</asp:Content>

C#

BasePage.cs

public class BasePage : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string language = "en-us";

        if (HttpContext.Current.Request.Cookies["Language"] != null)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
            if (cookie != null)
            {
                language = cookie.Value.Split('=')[1];
            }
        }

        //Check if PostBack is caused by Language DropDownList.
        if (Request.Form["__EVENTTARGET"] != null && Request.Form["__EVENTTARGET"].Contains("ddlLanguages"))
        {
            //Set the Language.
            language = Request.Form[Request.Form["__EVENTTARGET"]];
        }

        //Set the Culture.
        Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
    }
}

MasterPage.master.cs

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            string language = "en-us";
            if (HttpContext.Current.Request.Cookies["Language"] != null)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies["Language"];
                if (cookie != null)
                {
                    language = cookie.Value.Split('=')[1];
                }
            }
            this.ddlLanguages.Items.FindByValue(language.ToLower()).Selected = true;
        }
    }

    protected void ChangeLanguage(object sender, EventArgs e)
    {
        HttpCookie languageCookie = new HttpCookie("Language");
        languageCookie.Values["LanguageCode"] = this.ddlLanguages.SelectedValue;
        languageCookie.Expires = DateTime.Now.AddDays(30);
        HttpContext.Current.Response.Cookies.Add(languageCookie);
        this.Response.Redirect(Request.Url.AbsoluteUri);
    }
}

Default.aspx.cs

public partial class _Default : BasePage
{
    protected void SignUp(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }
}

Registration.aspx.cs

public partial class Registration : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
}

VB.Net

BasePage.vb

Public Class BasePage
	Inherits System.Web.UI.Page
	Protected Overrides Sub InitializeCulture()
		Dim language As String = "en-us"

		If HttpContext.Current.Request.Cookies("Language") IsNot Nothing Then
			Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies("Language")
			If cookie IsNot Nothing Then
				language = cookie.Value.Split("="C)(1)
			End If
		End If

		'Check if PostBack is caused by Language DropDownList.
		If Request.Form("__EVENTTARGET") IsNot Nothing AndAlso Request.Form("__EVENTTARGET").Contains("ddlLanguages") Then
			'Set the Language.
			language = Request.Form(Request.Form("__EVENTTARGET"))
		End If

		'Set the Culture.
		Thread.CurrentThread.CurrentCulture = New CultureInfo(language)
		Thread.CurrentThread.CurrentUICulture = New CultureInfo(language)
	End Sub
End Class

MasterPage.master.vb

Public Partial Class MasterPage
	Inherits System.Web.UI.MasterPage
	Protected Sub Page_Load(sender As Object, e As EventArgs)
		If Not Me.IsPostBack Then
			Dim language As String = "en-us"
			If HttpContext.Current.Request.Cookies("Language") IsNot Nothing Then
				Dim cookie As HttpCookie = HttpContext.Current.Request.Cookies("Language")
				If cookie IsNot Nothing Then
					language = cookie.Value.Split("="C)(1)
				End If
			End If
			Me.ddlLanguages.Items.FindByValue(language.ToLower()).Selected = True
		End If
	End Sub

	Protected Sub ChangeLanguage(sender As Object, e As EventArgs)
		Dim languageCookie As New HttpCookie("Language")
		languageCookie.Values("LanguageCode") = Me.ddlLanguages.SelectedValue
		languageCookie.Expires = DateTime.Now.AddDays(30)
		HttpContext.Current.Response.Cookies.Add(languageCookie)
		Me.Response.Redirect(Request.Url.AbsoluteUri)
	End Sub
End Class

Default.aspx.vb

Public Partial Class _Default
	Inherits BasePage
	Protected Sub SignUp(sender As Object, e As EventArgs)
		Response.Redirect("Registration.aspx")
	End Sub
End Class

Registration.aspx.vb

Public Partial Class Registration
	Inherits BasePage
	Protected Sub Page_Load(sender As Object, e As EventArgs)
	End Sub
End Class

Screenshot