如何创建类来初始化区域性以进行本地化

本文关键字:区域性 初始化 本地化 何创建 创建 | 更新日期: 2023-09-27 17:56:52

我正在尝试本地化我的 asp.net 页面。 到目前为止,我只本地化了我的母版页。

全球

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpCookie cookie = Request.Cookies["CultureInfo"];
    if (cookie != null && cookie.Value != null)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(cookie.Value);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(cookie.Value);
    }
    else
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-CA");
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-CA");
    }
}

MasterPage.master.cs

protected void Page_Load(object sender, EventArgs e)
{
    //only does it on non-postback because otherwise 
    //the selected value will not reach event handler correctly
    if (!Page.IsPostBack)
    {
        ddlLanguage.SelectedValue = Thread.CurrentThread.CurrentCulture.Name;
    }
}
protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
{
    //Sets the cookie that is to be used by Global.asax
    HttpCookie cookie = new HttpCookie("CultureInfo");
    cookie.Value = ddlLanguage.SelectedValue;
    Response.Cookies.Add(cookie);
    //Set the culture and reload for immediate effect.
    //Future effects are handled by Global.asax
    Thread.CurrentThread.CurrentCulture = new CultureInfo(ddlLanguage.SelectedValue);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(ddlLanguage.SelectedValue);
    Server.Transfer(Request.Path);
}

MasterPage.master

<asp:DropDownList ID="ddlLanguage" runat="server"
        OnSelectedIndexChanged="ddlLanguage_SelectedIndexChanged"
        AutoPostBack="true">
        <asp:ListItem Text="<%$ Resources:Resource, users_English %>" Value="en-CA" />
        <asp:ListItem Text="<%$ Resources:Resource, users_French %>" Value="fr-CA" />
</asp:DropDownList>

如何为所有其他网页创建一个公共类以从公共类继承。

如何创建类来初始化区域性以进行本地化

您可以创建一个继承自所需的 System.Web.Ui.Page(即公共类 CommonPage:System.Web.Ui.Page)添加/覆盖方法的类,并且您的所有页面都可以从这个新创建的页面继承。