@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

September 1, 2013 comment

ASP.NET: Detecting User/Browser Languages

Multilingual application, provide a way, so that user can select preferable language and application adopt culture based on selected language. But intelligent web application should auto detect user browser language and accordingly adopt culture settings for application. In this quick & short post, we will see how to detect user or browser language in ASP.NET application.

Request.UserLanguages return a array of string consisting list of languages set in user browser. We can use Request.UserLanguages in ASP.NET application, to determine user's preferable language. Below is the code snippet for the same.

protected override void InitializeCulture()
{
    CultureInfo cul;
    try
    {
        cul = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);
    }
    catch
    {
        cul = CultureInfo.CreateSpecificCulture("en-US");
    }
    System.Threading.Thread.CurrentThread.CurrentCulture = cul;
    System.Threading.Thread.CurrentThread.CurrentUICulture = cul;
}

In above code snippet, we are using try catch to set en-US as fallback language in case culture initialization fail. Some browser client allow user to set custom language header. In such case culture initialization may fail.

How Request.UserLanguages Is Populated?

We have utilized Request.UserLanguages to determine user's preferable language but it is nice to also know that how Request.UserLanguages values is populated. To check it out, first let add one additional language in browser.

We have added Gujarati language and setting its preference higher than other two language. Now request any webpage and examine Accept-Language request header.

As we can see in above image browser language is sent to server with Accept-Language header. Each user selected language is sent with coma separated.

What Is Quality Value?

If we examine above image carefully, we can see that it is also appending some numeric value to some of language. For e.g. en-US;q=0.8 in above image. This value is called quality value. As described here, quality value represent user's preference of choice. Default value is 1 and generally it is not appended. As we can see in above image we have set Gujarati as a preferable language so in request header it is not appending quality value for it. Although this behavior can be different from browser to browser.

Below image show the value of Request.UserLanguages which is populated from Accept-Language request header.

Hope this quick & short post would be helpful!

You can follow me on twitter for latest link and update on ASP.NET & MVC.

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading