I recently had the challenge of retrieving a country based on the browser language. It was used to pre-select a country in a drop down list so the user didn’t have to. I knew it wasn’t going to be 100% accurate but probably more like 80-90%.

That’s because some people change the browser language instead of their native language and others use a non-ISO standard language. And last, some clients just don’t send language information.

It wasn’t an option to use a database that mapped IP addresses to countries, so the country had to be resolved from the browser alone.

Resolve the culture

I decided to split the functionality up into two methods. The first one resolves the CultureInfo based on the browsers language.

public static CultureInfo ResolveCulture()

{

  string[] languages = HttpContext.Current.Request.UserLanguages;

 

  if (languages == null || languages.Length == 0)

    return null;

 

  try

  {

    string language = languages[0].ToLowerInvariant().Trim();

    return CultureInfo.CreateSpecificCulture(language);

  }

  catch (ArgumentException)

  {

    return null;

  }

}

Resolve the country

The next method uses the ResolveCulture() method above to create a RegionInfo object. The RegionInfo contains all the country information needed such as ISO code, EnglishName, NativeName and DisplayName.

public static RegionInfo ResolveCountry()

{

  CultureInfo culture = ResolveCulture();

  if (culture != null)

    return new RegionInfo(culture.LCID);

 

  return null;

}

Now I am able to get all the culture and country/region information I need based on the browser’s language, with a margin of inaccuracy I can live with.

Comments

Jesper Blad Jensen
Jesper Blad Jensen

You should soon make a utility DLL, with all that fancy methods you have constructed. And share that :)

DotNetKicks.com
DotNetKicks.com

Trackback from DotNetKicks.com Get country from a browser in ASP.NET

Pedro Santos
Pedro Santos

Instead of null, you could also return a "defaulte" locale/region if you can't resolve the request's locale. I have similar code, but I use en-US as default because of all the localization interface.

Prashant
Prashant

Hi, Its look good, we can resolve country. But i am having a different problem. I am having my server hosted in US, but most of my users are from india. When a user enters a record i am entering date and time in the database in "AddDate" column, But in C# coding when i am using Date.Now to get date and time then it gets the US time whereas i want it according to india time. Can u give me a code for this problem. Thanks, Prashant

Brian Lowry
Brian Lowry

@Prashant, You need to store the dates into the database using DateTime.UtcNow (universal time). You can either default your website to India's timezone and subtract/add the correct number of hours before displaying the data, or you can grab the user's timezone information from the browser and pass it back to the server.

Mike Borozdin
Mike Borozdin

@ Prashant, I think you should use JavaScript for that, because it gets the date and time according to the local time zone, so basically get them with JavaScript and send them to the server.

Prashant
Prashant

Hi @Brian and @Mike Thanks for replying. I think Brian's idea is good, we'll store date according universal time and will subtract the hours according to the user's timezone. Thanks, Prashant

Alan Mendelevich
Alan Mendelevich

One thing: please, don't make non-optional decisions based on that like some Microsoft's sites do. I had to change languages in my browser just to be able to download English version of WLW or something like that.

Add a comment

Used only for your avatar and replies; never published.