Welcome to the navigation

Proident, esse mollit ullamco ut eu ad consequat, cupidatat officia lorem laborum, consectetur fugiat sit occaecat pariatur, quis exercitation tempor in dolore et id ipsum. Veniam, nulla laborum, qui magna enim excepteur irure pariatur, do in eu et occaecat consectetur commodo in ut est sit esse labore ut eiusmod duis

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