Get language and country from a browser in ASP.NET

Jul 29, 2008

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

Comments (8) -

Jesper Blad Jensen
Jesper Blad Jensen Denmark
7/30/2008 4:56:03 AM #

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

Pedro Santos
Pedro Santos Portugal
7/31/2008 2:40:13 PM #

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 India
8/1/2008 3:30:51 AM #

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 United States
8/1/2008 12:59:33 PM #

@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 Russia
8/1/2008 1:23:13 PM #

@ 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 India
8/7/2008 4:40:34 AM #

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 Lithuania
8/13/2008 11:13:11 AM #

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.

Alojaweb
Alojaweb Peru
11/11/2008 1:23:03 AM #

  
is that it is simple!.

thanks Mads.

Pingbacks and trackbacks (1)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.