We Help You To Renovate Your Blogger Blog With Awesomeness!

Showing posts with label Get User Language. Show all posts
Showing posts with label Get User Language. Show all posts

Saturday, December 6, 2014

  • Get language and country from a browser in ASP.NET

    Resolve the culture

    Splitted 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;
    }





  • Copyright @ 2013 Code Snippets.