开发者

Set culture using cookie in asp.net, not updated

开发者 https://www.devze.com 2023-01-31 05:11 出处:网络
I\'m using asp.net and want to make it possible for the user to set the culture to use in the website by himself. In MasterPage I have the following code to set a language cookie:

I'm using asp.net and want to make it possible for the user to set the culture to use in the website by himself. In MasterPage I have the following code to set a language 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);
    }
}

In Global.asax I use the cookie like this:

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);
    }
}

The problem is that after I set the cookie with Response.SetCookie I need to reload the page to get the new language. How can I make my code so when the user set a new language the page is reloaded with the new language directly?


You can do

Response.Redirect(Request.PathAndQuery);

But why not just set the language after setting the Cookie? You can even use the BeginRequest event to check for specific input being posted and use it as an alternative condition for setting the language.


I had the same issue with the language being selected by the user. In order for it to work you have to do it on

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);

}

In order for it to work on every page of the site, I created a class that inherited from System.Web.UI.Page and implemented there

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();
  }
}

from then on I had all my pages inherit from myBasePage.

This way, I used a Server (Postback) control to set the language and the page would get reloaded, and the language would be set.


If you are using 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!

        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消