How to detect browser language

/// <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
        { ;}
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s