locked
Howto detect browser language RRS feed

  • Question

  • User1652557896 posted

    Hi, 

    How can I detect the language of the browser, i want to select the language for the browser.


     

    Thursday, December 7, 2006 3:02 AM

Answers

  • User1622957740 posted

     

    ASP.NET 2.0 has a couple of facilities to auto-detect locales and switch the thread's locale to the browser locale. Look for the <globalization> section in web.config (which is global obviously) or the Culture and UICulture attributes on the @Page directive.

    You can also do this manually in your code by looking at Browser.UserLanguages which returns all the user's browser languages that are configured in her browser. Here's a routine I use in my most of applications to switch the culture and UICulture:

    /// <summary>
    /// Sets a user's Locale based on the browser's Locale setting. If no setting
    /// is provided the default Locale is used.
    /// </summary>
    public static void SetUserLocale(string CurrencySymbol, bool SetUiCulture)
    {
        HttpRequest Request = HttpContext.Current.Request;
        if (Request.UserLanguages == null)
            return;
    
        string Lang = Request.UserLanguages[0];
        if (Lang != null)
        {
            // *** Problems with Turkish Locale and upper/lower case
            // *** DataRow/DataTable indexes
            if (Lang.StartsWith("tr"))
                return;
    
            if (Lang.Length < 3)
                Lang = Lang + "-" + Lang.ToUpper();
            try
            {
                System.Globalization.CultureInfo Culture = new System.Globalization.CultureInfo(Lang);
                if (CurrencySymbol != null && CurrencySymbol != "")
                    Culture.NumberFormat.CurrencySymbol = CurrencySymbol;
                
                System.Threading.Thread.CurrentThread.CurrentCulture = Culture;
    
                if (SetUiCulture)
                    System.Threading.Thread.CurrentThread.CurrentUICulture = Culture;
            }
            catch
            { ;}
        }
    }
    

    I prefer doing this manually rather than letting ASP.NET's Auto features do this because typically you'll need to configure additional local settings like the currency symbol explicitly. If you do you need to create a new culture info anyway and so this is a more consistent way to do it... You should call this soemwhere early in the ASP.NET lifecycle like Application_BeginRequest...

    Hope this helps,

    +++ Rick ---

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 9, 2006 8:48 PM

All replies

  • User1415983342 posted

    HttpBrowserCapabilities bc = Request.Browser;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    Response.Write("Supports VB Script = " + bc.VBScript + "<br>");<o:p></o:p>

    Response.Write("Supports JavaScript = " + bc.JavaScript + "<br>");<o:p></o:p>

    Response.Write("Supports Java Applets = " + bc.JavaApplets + "<br>");<o:p></o:p>

    Response.Write("Supports ActiveX Controls = " + bc.ActiveXControls + "<br>");
    Thursday, December 7, 2006 5:56 AM
  • User2076344368 posted

    You could look at using the System.Globalization and CultureInfo, there is a write up here and some example code

    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/culture.aspx#autoculture

     Hope it helps
     

     

     

    Thursday, December 7, 2006 6:03 AM
  • User1347187933 posted

    Hi,

    ASP.NET 2.0 Support Auto Detection of Browser Language - you can determine the language of the requesting browser from user's culture.

    http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/default.aspx

    http://samples.gotdotnet.com/quickstart/aspplus/doc/localizingapps.aspx

     

    Thursday, December 7, 2006 6:24 AM
  • User1622957740 posted

     

    ASP.NET 2.0 has a couple of facilities to auto-detect locales and switch the thread's locale to the browser locale. Look for the <globalization> section in web.config (which is global obviously) or the Culture and UICulture attributes on the @Page directive.

    You can also do this manually in your code by looking at Browser.UserLanguages which returns all the user's browser languages that are configured in her browser. Here's a routine I use in my most of applications to switch the culture and UICulture:

    /// <summary>
    /// Sets a user's Locale based on the browser's Locale setting. If no setting
    /// is provided the default Locale is used.
    /// </summary>
    public static void SetUserLocale(string CurrencySymbol, bool SetUiCulture)
    {
        HttpRequest Request = HttpContext.Current.Request;
        if (Request.UserLanguages == null)
            return;
    
        string Lang = Request.UserLanguages[0];
        if (Lang != null)
        {
            // *** Problems with Turkish Locale and upper/lower case
            // *** DataRow/DataTable indexes
            if (Lang.StartsWith("tr"))
                return;
    
            if (Lang.Length < 3)
                Lang = Lang + "-" + Lang.ToUpper();
            try
            {
                System.Globalization.CultureInfo Culture = new System.Globalization.CultureInfo(Lang);
                if (CurrencySymbol != null && CurrencySymbol != "")
                    Culture.NumberFormat.CurrencySymbol = CurrencySymbol;
                
                System.Threading.Thread.CurrentThread.CurrentCulture = Culture;
    
                if (SetUiCulture)
                    System.Threading.Thread.CurrentThread.CurrentUICulture = Culture;
            }
            catch
            { ;}
        }
    }
    

    I prefer doing this manually rather than letting ASP.NET's Auto features do this because typically you'll need to configure additional local settings like the currency symbol explicitly. If you do you need to create a new culture info anyway and so this is a more consistent way to do it... You should call this soemwhere early in the ASP.NET lifecycle like Application_BeginRequest...

    Hope this helps,

    +++ Rick ---

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 9, 2006 8:48 PM