Problems updating Google Translate cookie in Chrome
Asked Answered
V

2

17

I am trying to allow the user to set their default language. When a user picks a language from the dropdown and clicks "save", the page is reloaded and the Google Translate cookie is updated- and therefore the site is translated to the language the user picked. On FF and IE, users are able to set the language with the dropdown and then change it to another language. On Chrome, however, the users are able to set the language the first time, but then they cannot change it to a different language.

This issue only shows up on the test and beta site- I can update the language on my localhost.

I am testing with Chrome version 38.

This is the code that sets the Cookie the first time, and also that updates it when a user clicks Save.

public void ImplementUserPreferences(UserPreferences prefs)
{
    //examples of prefs.GoogleTranslateDefaultLanguage:
    //  af, sq, ar, sp, is....
    HttpCookie languageCookie = new HttpCookie("googtrans", "/en/" + prefs.GoogleTranslateDefaultLanguage);
    AddOrSetCookie(languageCookie, "googtrans");
}

private void AddOrSetCookie(HttpCookie cookie, String cookieName)
{
    if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null)
    {
        System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
    }
    else
    {
        System.Web.HttpContext.Current.Response.Cookies.Set(cookie);
    }
}
Vinegarette answered 17/11, 2014 at 14:20 Comment(3)
Have you tried doing System.Web.HttpContext.Current.Response.Cookies[cookieName] = cookie or ...Cookies[cookieName].Value = cookie.Value, or deleting the cookie and re-adding it with a new value?Pepin
Yes, tried it. Doesn't help.Vinegarette
If saving downloads a cookie they will have to clear em. Or you can set them to refresh quickly, but that would hurt users who get it right the first time.Jovi
M
1

Check the response headers, but I do not believe that the cookie will not be sent in the request when your address is localhost. So, with your logic the preference will update each time since the request has no cookie present.

An HttpResponse will not send back the cookies that came in the request, it only adds cookies that you mean to set in the response. So when you are testing in the beta site a request comes in with a cookie after it has been set, and will call the code in the else condition. The HttpCookieCollection.Set(...) method does not add a cookie, only updates one that exists in the collection already. I would change the code to read like this:

private void AddOrSetCookie(HttpCookie cookie, String cookieName)
{
    if (System.Web.HttpContext.Current.Request.Cookies[cookieName] == null
        || System.Web.HttpContext.Current.Request.Cookies[cookieName].Value != cookie.Value )
    {
        System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
    }

}
Mellifluent answered 20/11, 2014 at 2:39 Comment(2)
When I check the response Headers (Chrome developer tools) there is a google translate cookie in the list of cookies: googtrans=/en/ca; Vinegarette
The cookie shows up in the response header on localhost and on the test/beta siteVinegarette
N
0

please don't use cookies - that is why html5 includes local storage

more about local storage: http://www.html5rocks.com/en/tutorials/offline/storage

and for your scenario: http://www.codeguru.com/csharp/.net/two-ways-of-passing-html5-web-storage-data-to-asp.net.htm

by the way: these features are also supported for IE8!!! (as shown here: http://caniuse.com/#search=local%20storage)

Norvun answered 21/11, 2014 at 12:41 Comment(1)
I'm using cookies because google translate is implemented with a cookie, and I'm overriding that cookie.Vinegarette

© 2022 - 2024 — McMap. All rights reserved.