Switch language in MasterPage C # .NET

I need to switch the language in my main page file. the main page file contains the menu and there I need to switch the language too. Is there a workaround how can I also use the multi-language support on the homepage?

I have built a language switcher using this tutorial. My MLS.cs

file (in the tutorial called BasePage.cs) MLS

inherits from System.Web.UI.Page

, but my main page inherits from System.Web.UI.MasterPage

.

I hope there is a simple solution to switch the language also on the homepage without writing menus on all content pages.

Here is the content of my Design.Master (MasterPge for the user):

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Design.Master.cs" Inherits="ProjectName.Site1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>...</head>
<body class="skin-blue">
    <form id="form1" runat="server">
    <div class="wrapper">
    <aside class="main-sidebar">
            <div class="slimScrollDiv" style="width: auto; height: 422px; overflow: hidden; position: relative;">
                <div class="sidebar" id="scrollspy" style="width: auto; height: 422px; overflow: hidden; -ms-touch-action: none;">
                    <ul class="nav sidebar-menu">
                        <li class="header">data lookup</li>
                        <li><a href="~/datalookup.aspx"><i class="fa fa-arrow-right"></i>to data file</a></li>
                    </ul>
                    <!-- sidebar menu: : style can be found in sidebar.less -->
                    <ul class="nav sidebar-menu">
                        <li class="header">quick selection menue</li>
                        <li class="active"><a href="#table1"><i class="fa fa-circle-o"></i>to table 1</a></li>
                        <li ><a href="#table2"><i class="fa fa-circle-o"></i>to table 2</a></li>
                        <li ><a href="#table3"><i class="fa fa-circle-o"></i>to table 3</a></li>
                        <li ><a href="#table4"><i class="fa fa-circle-o"></i>to table 4</a></li>
                    </ul>
                </div>
            </div>
            <!-- /.sidebar -->
        </aside>
    <!-- /.aside -->

      

Hope someone can help.

+3


source to share


1 answer


I achieved this by creating two titles, one in English and one in Welsh, then in MasterPage.master.cs

I did:

protected void Page_Load(object sender, EventArgs e)
{
    BreadCrumb();

    if (Thread.CurrentThread.CurrentCulture.ToString() == "cy-GB")
    {                
        Footer1.Visible = false;
        Footer2.Visible = true;
        Header1.Visible = false;
        Header2.Visible = true;
    }

    if (Thread.CurrentThread.CurrentCulture.ToString() == "en-GB")
    {                
        Footer2.Visible = false;
        Footer1.Visible = true;
        Header1.Visible = true;
        Header2.Visible = false;
    }

    Page.Header.DataBind();   
    //clear cache each time page loads
    Response.Expires = 0;
    Response.Cache.SetNoStore();
    Response.AppendHeader("Pragma", "no-cache");


private void BreadCrumb()
{
    string path = HttpContext.Current.Request.Url.AbsolutePath;

    if (path == "/LogIn.aspx" || path == "/LogIn.aspx?lang=cy-GB")
    {                
        breadcrumb.Visible = false;                
    }
}

      

I also created a BasePage class which each subsequent code after page inherits from:

public partial class BasePage : System.Web.UI.Page
    {
        protected override void InitializeCulture()
        {
            if (Session["language"] == null)
            {
                Session["language"] = "en-GB";
            }

            else
            {
                if (Request.QueryString["lang"] == null)
                {
                    SetSessionCulture();
                }

                if (Request.QueryString["lang"] != null)
                {
                    string qs = Request.QueryString["lang"];
                    Session["language"] = qs;
                }

                SetSessionCulture();
            }

            SetSessionCulture();           
        }

        private void SetSessionCulture()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["language"].ToString());
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["language"].ToString());
            base.InitializeCulture();
        }
    }

      

change

I present both welsh / english headers to my host like this:

<%@ Register Src="Components/Header2.ascx" TagName="Header" TagPrefix="uc1" %>
<%@ Register Src="Components/Header2.cy-GB.ascx" TagName="Header" TagPrefix="uc4" %>

      



Then disable / enable them based on the current language which is stored in the session and then for every other page that inherits from my base page, it checks the current culture and takes translations from my resx files.

change 2

In terms of the two headers, all I have are my links and a language switcher, the code looks like this:

English version

public partial class header : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           string currentPage = Request.Url.AbsoluteUri.ToString();

            NameValueCollection qsexisting = HttpUtility.ParseQueryString(Request.QueryString.ToString());

            //find anyting called lang in the array and remove
            qsexisting.Remove("lang");

            //The culture is English, set stuff to Welsh
            if (Thread.CurrentThread.CurrentCulture.ToString() == "en-GB")
            {
                Uri uri = new Uri(currentPage);
                languagelink.HRef = String.Format(uri.GetLeftPart(UriPartial.Path) + "?lang=cy-GB" + (qsexisting.ToString() == "" ? "" : "&" + qsexisting.ToString()));                
            }
        }

        protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
        {
            Response.Redirect("~/LogIn.aspx");
        }
    }

      

Welsh version

 public partial class header_cy_GB : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string currentPage = Request.Url.AbsoluteUri.ToString();

            NameValueCollection qsexisting = HttpUtility.ParseQueryString(Request.QueryString.ToString());
            //find anyting called lang in the array and remove
            qsexisting.Remove("lang");

            var qs = Request.QueryString;

            //The culture is welsh, set stuff to English
            if (Thread.CurrentThread.CurrentCulture.ToString() == "cy-GB")
            {
                Uri uri = new Uri(currentPage);
                languagelink.HRef = String.Format(uri.GetLeftPart(UriPartial.Path) + "?lang=en-GB" + (qsexisting.ToString() == "" ? "" : "&" + qsexisting.ToString()));
            }
        }

        protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
        {
            Response.Redirect("~/LogIn.aspx");
        }
    }

      

0


source







All Articles