我正在尝试使用会话根据母版页中选定的链接按钮来本地化我的母版页。
Global.asax中
void Session_Start(object sender, EventArgs e)
{
//set english as default startup language
Session[Udev.MasterPageWithLocalization.Classes.Global.SESSION_KEY_CULTURE] = Udev.MasterPageWithLocalization.Classes.Culture.EN;
}
包含链接按钮的母版页:HomeMaster.master
<asp:LinkButton ID="btnSetGerman" runat="server" Text="Nepali" CommandArgument="ne-NP" OnClick="RequestLanguageChange_Click"></asp:LinkButton>
<asp:LinkButton ID="btnSetEnglish" runat="server" Text="English" CommandArgument="en-GB" OnClick="RequestLanguageChange_Click"></asp:LinkButton>
HomeMaster.master.cs
public partial class Includes_MasterPage_HomeMaster : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void RequestLanguageChange_Click(object sender, EventArgs e)
{
LinkButton senderLink = sender as LinkButton;
//store requested language as new culture in the session
Session["MyCulture"] = senderLink.CommandArgument;
//reload last requested page with new culture
Server.Transfer(Request.Path);
}
}
BasePage.cs
namespace Udev.MasterPageWithLocalization.Classes
{
/// <summary>
/// Custom base page used for all web forms.
/// </summary>
public class BasePage : Page
{
protected override void InitializeCulture()
{
//retrieve culture information from session
string culture = Convert.ToString(Session[Global.SESSION_KEY_CULTURE]);
//check whether a culture is stored in the session
if (culture.Length > 0) Culture = culture;
//set culture to current thread
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
//call base class
base.InitializeCulture();
}
}
}
Culture.cs
namespace Udev.MasterPageWithLocalization.Classes
{
/// <summary>
/// This class provides ISO definitions for all cultures that are supported by this application.
/// </summary>
public struct Culture
{
//Nepali - Nepal definition
public const string NL = "ne-NP";
//English - Great Britain definition
public const string EN = "en-GB";
}
}
Global.cs
namespace Udev.MasterPageWithLocalization.Classes
{
/// <summary>
/// Summary description for Global
/// </summary>
public struct Global
{
public const string SESSION_KEY_CULTURE = "culture";
}
}
到目前为止,我的母版页没有初始化任何语言,而其他页面只加载全局会话中的默认语言集。