articles

Home / DeveloperSection / Articles / Displaying User Culture Language in ASP.Net

Displaying User Culture Language in ASP.Net

mohan kumar7568 21-Nov-2011

Hey Guys!! In this tutorial, I am going to explain you, how to read user’s cultural language and redirect the user’s page according to that regional/country language.

My Concept: - Both the request object’s User Languages and the CulturalInfo class’s Name property return culture information into two parts. The first two letters are the language code. Depending upon the language code, I will be redirecting the user according to his/her regional/Country language.

Default.aspx:-
<form id="form1" runat="server">
    <div>
    <asp:Label ID="lblCulture" runat="server" ></asp:Label>
    </div>
</form>

(However,I am not going to use this label..Let it be there J J)

Default.aspx.cs:-

 

Don’t forget to add the namespace System.Globalization. Use VB Or C# use this lines to add the namespace.

 

Using System.Globalization; (C#)

Import System.Globalization (VB)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization;
 
public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Get the user's preferred language
        string sLang =Request.UserLanguages[0];
        //Get the first two characters of the language
        sLang= sLang.Substring(0, 2);   
 
 
 
 //Redirect user based culture
        switch (sLang)
        {
            case "en":
                //use US Site
                Response.Redirect("http://www.mindstick.com/lang/eng");
                break;
            case "es":
                //use Spanish Site
                Response.Redirect("http://www.mindstick.com/lang/es");
                break;
            case "de":
                //use German Site
                Response.Redirect("http://www.mindstick.com/lang/de");
                break;
            case "zh":
                //use Chinese Site
                Response.Redirect("http://www.mindstick.com/lang/zh");
                break;
            default:
                Response.Redirect("http://www.mindstick.com/lang/eng");
                break;
        }
    }
}

  

On running this sample code,you will be directly redirected to the corresponding language.Just post back a reply if this article was useful for you. Thanks for reading my article. Happy Coding.Explore more and more as much as possible.



Updated 30-Nov-2017
Having around 5 Years experience in .NET domain.

Leave Comment

Comments

Liked By