我需要在母版页文件中切换语言。主页文件包含menue,我还需要切换我的语言。 有没有解决方法如何在母版页中使用多语言支持?
我使用this教程构建了语言切换器。我的MLS.cs
文件(在名为BasePage.cs的教程中)MLS
继承自System.Web.UI.Page
,但我的母版页继承自System.Web.UI.MasterPage
。
我希望有一个简单的解决方案,也可以在母版页中切换语言,而无需在所有内容页面中编写菜单。
以下是我的Design.Master(用户的MasterPge)的内容:
<%@ 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 -->
希望有人可以提供帮助。
答案 0 :(得分:0)
我通过创建两个标题来实现这一点,一个是英文,一个是威尔士,然后是MasterPage.master.cs
我做过:
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;
}
}
我还创建了一个BasePage类,后面的每个后续代码都继承自:
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();
}
}
修改强>
我将这两个威尔士语/英语标题呈现给我的主人:
<%@ Register Src="Components/Header2.ascx" TagName="Header" TagPrefix="uc1" %>
<%@ Register Src="Components/Header2.cy-GB.ascx" TagName="Header" TagPrefix="uc4" %>
然后根据会话中存储的当前语言禁用/启用它们,然后对于从我的基页继承的每个其他页面,它会检查当前的文化并从我的resx文件中获取翻译。
编辑2
就两个标题而言,我所拥有的是我的链接和语言切换,后面的代码如下所示:
英文版
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");
}
}
威尔士版
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");
}
}