.net.net-coreglobalizationanglesharp

AngleSharp: properly setting Accept-Lang header


I'm using AngleSharp to query google.com. All results I get are crearly produced for an it-IT locale (since I execute the code from Italy). A test using a browser set to en-US proofs it.

From a discussion in GitHub issues of the project, I learned that I was setting the Accept-Language header in the proper way.

This code confirms it:

var formatter = new PrettyMarkupFormatter();
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.812");
var requester = new HttpClientRequester(client);
var config = Configuration.Default.WithRequester(requester).WithDefaultLoader();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync("http://www.reliply.org/tools/requestheaders.php");
var table = document.QuerySelector("table.standard");
Console.WriteLine(table.ToHtml(formatter));

How google.com (or a web server in general) can recognize the client locale in a different way? And how to properly set a given locale in AngleSharp?

Making light about it, will be very appreciated and more over I think it's an important thing to understand in general.


Solution

  • Google gets the locale from your location / region, which is given by the IP address.

    You can override this setting via the Google cookie (or by setting it on the page, which will trigger the cookie). The cookies with the NID values are the ones to be respected.

    Most Google users will have a preferences cookie called ‘NID’ in their browsers. A browser sends this cookie with requests to Google’s sites. The NID cookie contains a unique ID Google uses to remember your preferences and other information, such as your preferred language (e.g. English), how many search results you wish to have shown per page (e.g. 10 or 20), and whether or not you wish to have Google’s SafeSearch filter turned on.

    See https://policies.google.com/technologies/types?hl=en-US.

    Hope this helps!