Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems updating Google Translate cookie in Chrome

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);
    }
}
like image 333
dmr Avatar asked Nov 17 '14 14:11

dmr


People also ask

Why is Google Translate not working on Chrome?

Try refreshing the webpage. If it's still not working, right-click anywhere on the page. Then, click Translate to [Language].

Why has my Google Translate stopped working?

Google translate not working properly on Chrome might happen due to inappropriate browser settings configuration, language settings, third party extensions, or so. Well, whatever be the case, we will look into some reliable fixes that might help you deal with this problem easily.

What has happened to Google Translate?

Google Translate is still available for very narrow purposes and use cases. Specifically, to quote Google's official resource: [The website widget can be used by] government, non-profit, and/or non-commercial websites (for example, academic institutions) that focus on COVID-19 response.


1 Answers

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

}
like image 73
James Ralston Avatar answered Oct 14 '22 16:10

James Ralston