Dresel / RouteLocalization

RouteLocalization is an MVC and Web API package that allows localization of your attribute routes.
MIT License
67 stars 13 forks source link

Save user culture choice in Cookie to be used on the next visit. #52

Closed mdmoura closed 9 years ago

mdmoura commented 9 years ago

Hello,

I have the following on Route localization setup:

CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate = context => 
       new CultureResolver().GetCulture(context, acceptedCultres, defaultCulture);

Inside the GetCulture I have the following:

public CultureInfo GetCulture(
     HttpContext context, 
     ISet<String> acceptedCultures, 
     String defaultCulture) {

     return new CultureInfo(defaultCulture);
}

This works fine but when the user later accesses the site the culture is again the default one.

I would like to save a cookie with the user choice and the next time the user accesses the web site the previous chosen culture would be used. I tried the following:

public CultureInfo GetCulture(
     HttpContext context, 
     ISet<String> acceptedCultures, 
     String defaultCulture) {

     return new CultureInfo(defaultCulture);
}

public CultureInfo GetCulture(
     HttpContext context, 
     ISet<String> acceptedCultures, 
     String defaultCulture) {

    HttpCookie cookie = context.Request.Cookies["Culture"];
    if (cookie == null || !acceptedCultures.Contains(cookie.Value)) {                
      cookie = new HttpCookie("Culture", defaultCulture);
      context.Response.Cookies.Add(cookie);
    }     
    return new CultureInfo(cookie.Value);
 } // GetCulture

However, when I change the culture the Cookie value does not change.

I am not sure how to update the cookie value when the culture is changed.

What should be the best way to do this?

Thank You

Dresel commented 9 years ago

I would set the cookie value on the culture selected callback function from the filter.

We discussed something similiar here: https://github.com/Dresel/RouteLocalization/issues/19#issuecomment-55503337.

mdmoura commented 9 years ago

Thanks! That worked really fine.

I was searching for it on Docs, source and issues but wasn't able to find it.

Thank you once again, Miguel