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);
}
}
Try refreshing the webpage. If it's still not working, right-click anywhere on the page. Then, click Translate to [Language].
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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With