Welcome to the navigation

Officia ut tempor dolore sunt voluptate culpa non dolor minim et consequat, in deserunt ipsum in excepteur dolore eiusmod ea sint laborum, est nisi cupidatat. Ullamco amet, elit, est enim reprehenderit sunt ut in proident, ipsum culpa ea ut do incididunt qui sint mollit magna adipisicing in nisi anim laboris

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