Get language and country from a browser in ASP.NET

by Mads Kristensen 30. July 2008 03:21

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.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: ,

ASP.NET

Comments

7/30/2008 1:56:03 PM #

Jesper Blad Jensen

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

Jesper Blad Jensen Denmark |

7/30/2008 9:38:58 PM #

trackback

Trackback from DotNetKicks.com

Get country from a browser in ASP.NET

DotNetKicks.com |

7/31/2008 11:40:13 PM #

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.

Pedro Santos Portugal |

8/1/2008 12:30:51 PM #

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

Prashant India |

8/1/2008 9:59:33 PM #

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.

Brian Lowry United States |

8/1/2008 10:23:13 PM #

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.

Mike Borozdin Russia |

8/7/2008 1:40:34 PM #

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

Prashant India |

8/13/2008 8:13:11 PM #

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.

Alan Mendelevich Lithuania |

11/11/2008 10:23:03 AM #

Alojaweb

  
is that it is simple!.

thanks Mads.

Alojaweb Peru |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008