Welcome to the navigation

Adipisicing nostrud elit, laboris qui sed laborum, ad duis voluptate consequat, in mollit non exercitation fugiat minim deserunt eiusmod nulla veniam, sit commodo eu in. Nisi aliqua, magna qui ipsum quis reprehenderit eiusmod do cupidatat irure dolor esse elit, officia in laborum, dolore in consequat, aute tempor veniam, velit nulla

Yeah, this will be replaced... But please enjoy the search!

Getting the client locale using ASP.NET Web API

I think it is very neat to automatically help the user fill out some forms, adapt date and time formats and so on. Small things like this keeps visitors satisfied which is rather important! Using ASP.NET Web API for this is simple The query address will be something like http://yoursite/api/locale depending on your routing.

using System.Net.Http;
using System.Web;
using System.Web.Http;
 
namespace Trikks.Controllers.Api
{
    public class LocaleController : ApiController
    {
        public string[] GetLocale()
        {
            return GetClientLocale();
        }
 
        private string[] GetClientLocale(HttpRequestMessage request = null)
        {
            request = request ?? Request;
 
            if (request.Properties.ContainsKey("MS_HttpContext"))
            {
                return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserLanguages;
            }
            else if (HttpContext.Current != null)
            {
                return HttpContext.Current.Request.UserLanguages;
            }
            else
            {
                return null;
            }
        }
    }
}

The returns will look something like this

["sv-SE","sv;q=0.8","en-US;q=0.6","en;q=0.4"]

Store the response in a javascript variable

var locales = ["sv-SE","sv;q=0.8","en-US;q=0.6","en;q=0.4"];
var primaryLocale = locales[0];

primaryLocale will contain the first locale, in this case "sv-SE"

Enjoy

Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author