0

I try to retrieve the "accept-language" HTTP header value with jQuery in this way:

$(document).ready(function () {
    //Ask ASP.NET what culture we prefer, because we stuck it in a meta tag
    var data = $("meta[name='accept-language']").attr("content")
});

Extracted from here.

The HTTP header is present because I can see it with the Chrome Network console tab "Accept-Language:en-US,en;q=0.8,de;q=0.6,es;q=0.4", but I always get undefined value.

Any idea?

2
  • Accept Headers can be overridden !! Commented Jan 22, 2013 at 10:06
  • I am sorry, I don't understand what do you pretend to say me, could you please explain it? Commented Jan 22, 2013 at 10:08

3 Answers 3

2

You need to notice, that this code is not retrieving the actual header, but value which has been put into META tag (JavaScript doesn't have access to the headers of original request). In the article you have linked you have this helper:

namespace System.Web.Mvc
{
    public static class LocalizationHelpers
    {
        public static IHtmlString MetaAcceptLanguage<T>(this HtmlHelper<T> html)
        {
            var acceptLanguage = HttpUtility.HtmlAttributeEncode(Threading.Thread.CurrentThread.CurrentUICulture.ToString());
            return new HtmlString(String.Format("<META name=\ content=\ {0}\?? accept-language\??>",acceptLanguage));
        }
    }
} 

Which you need to add to your project, and then use in view like this:

<HTML>
    <HEAD>
        ...
        @Html.MetaAcceptLanguage()
    </HEAD>
    ...
</HTML> 
Sign up to request clarification or add additional context in comments.

1 Comment

The code here doesn't actually get the Accept-Language header. To do that you need to access html.ViewContext.HttpContext.Request.Headers["Accept-Language"] and parse the output (which is something like en-US,en-GB;q=0.8,en;q=0.6.
2

You've misunderstood the code snippet slightly. In the Scott Hanselman article, he adds a custom meta element to the head of the HTML document. The jQuery selector $("meta[name='accept-language']") simply finds that meta-element so you can obtain its value.

There is no direct way to see the value sent from the client in the request for the returned page. It's not information exposed by the DOM. The closest you can get is navigator.language, but that's just the language set in the browser and will differ from the header.

This is why the custom meta tag is added to expose this information.

Comments

0

you can do with javascript.

  1. Accept headers can be changed in client side. so do server side check also. (though you are only concerned about the language header)

//navigator.userLanguage for ie

//navigator.language for other

var language = window.navigator.userLanguage || window.navigator.language;
alert(language);

1 Comment

These window.navigator properties are not the same value as passed on the Accept-Language request header

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.