Tuesday, September 4, 2012

How to detect browsers language preference?

By Javascript:
var language = window.navigator.userLanguage || window.navigator.language;

By check request header on server side:
Request.Headers["Accept-Language"]
Accept-Language:en-US;q=0.8,en;q=0.6

Scrum in 5 Minutes


ASP.NET MVC 4, Browser-Specific Views



private void registerDisplayMode(string agent, int index = 0)
{
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode(agent)
{
ContextCondition = (context => context.GetOverriddenUserAgent().IndexOf
(agent, StringComparison.OrdinalIgnoreCase) >= 0)
});
}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);

registerDisplayMode("iPhone", 0);
registerDisplayMode("iPad", 1);
}