Monday, May 3, 2010

How to get client's languages using Asp.net

Recently I have fall in a situation that I need the clients language for fulfill the business requirement. I have found lots of resources about it. Today I want to share with you how to get client's language using asp.net.

You can get the language from Browser using
HttpRequest.Request.ServerVariables("HTTP_ACCEPT_LANGUAGE").

or, You can also use this
HttpRequest.UserLanguages();

Here HttpRequest.UserLanguages return a array of languages,so to retrieve the language you can use index of array like,
HttpRequest.UserLanguages(0);

But there are some differents between them.HttpRequest.UserLanguages() returns a sorted array of language preferences, whereas the Request.ServerVariables("HTTP_ACCEPT_LANGUAGE") returns a (comma-separated) string. In other words, HttpRequest.UserLanguages appears to take the value of Request.ServerVariables("HTTP_ACCEPT_LANGUAGE").

There are another way you can get it from CulturalInfo class.
CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentUICulture;

This will give you what is set in the browser as the current language.But 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. It is really a more direct way to get the client's culture.

No comments:

Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'

Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...