Skip to main content

Find Cultureinfo and RegionInfo from browser in ASP.NET

Get language and country from a browser in ASP.NET

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.

Usage:

        RegionInfo ri = ResolveCountry();
        Response.Write("Display Name:" + ri.DisplayName );
        Response.Write("Native Name:" + ri.NativeName
);  

Comments

Popular posts from this blog

Android onTouch or onClick

  The decision to use either   onTouch   or   onClick   in an Android application depends on the specific requirements of the application and the behavior that the developer is trying to achieve. onClick  is called when the user taps a view, and is similar to a button press in a traditional desktop interface.  onClick  provides simple click detection, but it doesn't allow for more complex gestures like swipes and pinches. onTouch , on the other hand, provides more detailed information about the touch event and can detect complex gestures like swipes and pinches.  onTouch  gives you a  MotionEvent  which contains information about the touch event, including the position of the touch, the time of the touch, and the action that occurred. If you need to detect simple click events, then  onClick  is sufficient. However, if you need to detect more complex gestures, like swipes and pinches, then  onTouch is a better choice. Also, you can use both  onTouch  and  onClick  together if necess