Emil Stoichev's Blog

Just a few coding tips

Tip: How to get the client culture from an ASP.NET app

I fall in a couple of situations where I needed to get the client’s culture from the server side in an ASP.NET application. I googled this and found only client side solutions, but I knew there was some way to get this information because the ASP.NET framework supports client based culture (through the (UI)Culture = "Auto" in the page attributes and the globalization section in the web.config). The only way I thought of getting the client culture was from the Request object.

After examining the HTTP Headers collection I found the Accept-Language header. It contains information about the user's preferred languages. This is a sample Accept-Language header:

Accept-Language
bg-BG,en-US;q=0.7,ar-BH;q=0.3

The languages are explicitly defined in the browser and their order is determined. You are probably wondering what this q-thing means. According to the RFC 3282 (Content Language Headers) it specifies the language quality or in other words the language priority set in the client's browser. In the example above bg-BG (Bulgarian (Bulgaria) has highest priority then en-US (English (United States)) and the last preferred language is ar-BH (Arabic (Bahrain)).

The Accept-Language header lists all languages set in the browser in a comma separated list which makes it easy to extract each language.

From ASP.NET you can access this header using the Headers collection in the Request object - Request.Headers["Accept-Language"]. Then you can process it the way you like.

UPDATE:
Raj Kaimal gave a useful tip that instead of using Request.Headers["Accept-Language"] you can simply use the HttpRequest.UserLanguages to get a sorted string array of client language preferences. Thanks Raj!

I hope you find this tip useful.

Posted: Jan 21 2008, 03:44 PM by Emil Stoychev | with 9 comment(s)
Filed under:

Comments

rajbk said:

Consider using HttpRequest.UserLanguages to get a sorted string array of client language preferences.

msdn2.microsoft.com/.../system.web.httprequest.userlanguages.aspx

# January 21, 2008 1:19 PM

Emil Stoychev said:

Oh, I didn't know that exists. Thanks rajbk! I'll update the blog with your tip.

# January 21, 2008 4:46 PM

Tech Guru said:

Detect Client Culture with ASP.NET

# January 21, 2008 11:03 PM

Trumpi's blog said:

jQuery MooTools 1.2 Beta 2 Released, Adds New Element Storage jQuery, Facebox and AJAX Agile The magic

# January 23, 2008 2:03 PM

Ali said:

Great post. very helpful

# January 2, 2009 12:24 AM

Josh said:

You can get the current UI Culture in a much more direct way:

CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;

This will give you what is set in the browser as the current language.

# January 6, 2009 1:12 PM

Emil Stoychev said:

Hi Josh,

I've checked what you are saying and it seems that it is NOT true. On my machine I have culture set to en-US. On the browser I have first bg-BG and second en-US. Displaying System.Threading.Thread.CurrentThread.CurrentUICulturealways gives me en-US which is the server's culture (i.e. my machine's culture).

You can try it by yourself.

Also run an HTTP debugged and you will see that in the  request header all of your browser's cultures are listed, but System.Threading.Thread.CurrentThread.CurrentUICulturealways shows your machine's culture.

# January 7, 2009 2:25 AM

Josh said:

Emil:

It works fine for me, I set the browser's first language as Spanish(Traditional sort) which is es-ES, and I get the System.Threading.Thread.CurrentThread.CurrentUICultureas es-ES. If I choose Bulgarian (which for my browser is just bg) I get that.

Josh

# January 7, 2009 11:24 AM

Emil Stoychev said:

Josh, what we were missing is that you need to set UICulture="Auto" on page or global level before using System.Threading.Thread.CurrentThread.CurrentUICulture. Otherwise it will not give you the expected result.

Thanks for that tip! It is really a more direct way to get the client's culture.

# January 8, 2009 1:22 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)