3

I need to switch language in my masterpage file. the masterpage file contains the menue and there I also need to switch my language. Is there a workaround how I can also use Multi Language Support in the Master Page?

I have build the language switcher with this tutorial. My MLS.cs file (In the tutorial named BasePage.cs) MLS inherits from System.Web.UI.Page but my Master Page inherits from System.Web.UI.MasterPage.

I hope that there is a simple solution to switch language also in the masterpage without writing the menue in 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 somebody can help.

Hack4Life
  • 563
  • 1
  • 11
  • 34

1 Answers1

0

I achieved this by creating two headers, one in English and one in Welsh, then in the 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 every subsequent code behind page inherited 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();
        }
    }

edit

I render both the welsh/english headers to my master 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 being stored at session, then for each other page which inherits from my basepage it checks the current culture and picks up the translations from my resx files.

edit 2

In terms of the two headers all I have in them are my links and the language switch, the code behind 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");
        }
    }
JsonStatham
  • 9,770
  • 27
  • 100
  • 181
  • What do you mean with two headers? Whats inside them? – Hack4Life Jul 28 '15 at 09:12
  • Just the links to the other pages and the language switch link, but in one everythings in Welsh and in the other everything is in English, – JsonStatham Jul 28 '15 at 09:15
  • I have added some code of my Design.Master file. I need to translate all text inside the `#scrollspy`. How can I do this by changing header? It confuses me .. – Hack4Life Jul 28 '15 at 09:29
  • is your Footer1/Footer2 something like a placeholder and you switch between them? – Hack4Life Jul 28 '15 at 09:38
  • Ok, it's getting clearer and clearer @selectDistinct. Would it be possible that you also show me the content of the ascx file and how it is used inside the `.aspx` file? Is it a file that contains the definition for some usercontrols like `asp:literal`? – Hack4Life Jul 28 '15 at 11:36
  • You can see in my second edit, the code behind is setting what the language switch link for each version of the header will actually do, i.e. set the current culture to en-GB or cy-GB respectively, the aspx for headers is irrelevant as long as you have some sort of language switch button on it that you can wire up to like I have done on the page load. – JsonStatham Jul 28 '15 at 13:38
  • You have in your first code snippet `Header1` and `Header2` but you register `Header2` and `Header2.cy-GB`. Where is there the difference? `Header2.cy-GB.visible` does not work in the codebehind of my masterpage file. – Hack4Life Jul 29 '15 at 06:05