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...
inodes
Member
70 Points
15 Posts
Howto detect browser language
Dec 07, 2006 07:02 AM|LINK
Hi,
How can I detect the language of the browser, i want to select the language for the browser.
guenavan
Contributor
4306 Points
1695 Posts
Re: Howto detect browser language
Dec 07, 2006 09:56 AM|LINK
HttpBrowserCapabilities bc = Request.Browser;
Response.Write("Supports VB Script = " + bc.VBScript + "<br>");
Response.Write("Supports JavaScript = " + bc.JavaScript + "<br>");
Response.Write("Supports Java Applets = " + bc.JavaApplets + "<br>");
Response.Write("Supports ActiveX Controls = " + bc.ActiveXControls + "<br>");Hope4sun
Participant
1139 Points
201 Posts
Re: Howto detect browser language
Dec 07, 2006 10:03 AM|LINK
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
GeorgeZ
Contributor
2325 Points
552 Posts
Re: Howto detect browser language
Dec 07, 2006 10:24 AM|LINK
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
rstrahl
Contributor
2233 Points
375 Posts
ASPInsiders
MVP
Re: Howto detect browser language
Dec 10, 2006 12:48 AM|LINK
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 ---
West Wind Technologies
Making waves on the Web
www.west-wind.com/weblog