在asp.net中使用cookie设置文化,而不是更新

时间:2010-12-20 09:15:26

标签: asp.net cookies globalization

我正在使用asp.net,并希望用户可以自己设置文化在网站中使用。在MasterPage中,我有以下代码来设置语言cookie:

protected void Page_Load(object sender, EventArgs e) {

    if (Request.QueryString["setLanguage"] != null)
    {
        HttpCookie languageCookie = new HttpCookie("language");
        languageCookie.Value = Request.QueryString["setLanguage"];
        languageCookie.Expires = DateTime.Now.AddDays(10);
        Response.SetCookie(languageCookie);
    }
}

在Global.asax中,我使用这样的cookie:

protected void Application_BeginRequest(object sender, EventArgs e) {
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];
    if (languageCookie.Value != null)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
    }
}

问题是在我使用Response.SetCookie设置cookie后,我需要重新加载页面以获取新语言。如何在用户设置新语言时直接使用新语言重新加载我的代码?

3 个答案:

答案 0 :(得分:1)

你可以做到

Response.Redirect(Request.PathAndQuery);

但为什么不在设置Cookie后设置语言?您甚至可以使用BeginRequest事件来检查发布的特定输入,并将其用作设置语言的替代条件。

答案 1 :(得分:1)

我对用户选择的语言有同样的问题。为了使它能够工作,你必须在

上进行
protected override void InitializeCulture()
{
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);

}

为了使它能够在网站的每个页面上工作,我创建了一个继承自System.Web.UI.Page并在那里实现的类

public class myBasePage : System.Web.UI.Page
{
  protected override void InitializeCulture()
  {
    HttpCookie languageCookie = System.Web.HttpContext.Current.Request.Cookies["language"];

    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);

    base.InitializeCulture();
  }
}

从那时起,我的所有页面都继承自myBasePage。

这样,我使用Server(Postback)控件来设置语言,页面将被重新加载,并且语言将被设置。

答案 2 :(得分:0)

如果您使用的是Asp.Net MVC

//A foreigner, has possibly brew a cookie for me
public class SpeakNativeTongueAttribute : ActionFilterAttribute, IActionFilter
{
    const string cookieName = "culture";

     void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
    {
        var cookieKeys = filterContext.RequestContext.HttpContext.Request.Cookies.AllKeys;

        if (cookieKeys.Contains(cookieName))
        {
            //eat the cookie
            var theCultureCookie = filterContext.RequestContext.HttpContext.Request.Cookies[cookieName];
            var theCulture = theCultureCookie.Value;

            //say thanks in native tongue
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(theCulture);
        }
        else
        {
            //Didn't receive a cookie, don't speak their language, those bastards!

        }
    }
}