In this article I will explain with an example, how to automatically detect Browser
Language and
Culture in
ASP.Net using C# and VB.Net.
Once the Browser Language and Culture is detected, the website will display content in the User’s language using Globalization and Localization.
Adding a Global Resource file
The very first thing is to add two Global Resource files one for English and one for French with the following names.
English - Resource.resx
French – Resource.fr.resx
As soon as you add the first Resource file you will get the following prompt from
Visual Studio asking to create a folder named
App_GlobalResources. You need to click Yes.
Once both the Resource files are added, the App_GlobalResources folder in the Solution Explorer should look as shown below.
Next you will need to add sets of Name and Values to the Resource files of both English and French.
Note: The Name will be same for all entries in the Resource files of both English and French only the Value will change.
English Resource file
French Resource file
HTML Markup
The
HTML Markup consists of following controls:
Label – For displaying the text.
The
Label has been assigned the
Greetings Resource.
Then
HTML SPAN element has been assigned the
Introduction Resource.
There’s also another Label lblCulture which displays the automatically detected Browser culture.
Culture:
<asp:Label ID="lblCulture" runat="server"></asp:Label>
<hr />
<asp:Label ID="Label1" Text="<%$Resources:Resource, Greetings%>" runat="server"
Font-Bold="true" />
<br />
<br />
<span><%=Resources.Resource.Introduction %></span>
Below are the syntaxes for assigning Resource file to an
ASP.Net Control and an
HTML element.
ASP.Net Control
<asp:Label ID="Label1" Text="<%$Resources:Resource, <Resource Name> %>" runat="server" Font-Bold="true" />
HTML SPAN
<span><%=Resources.Resource. <Resource Name> %></span>
Namespaces
You will need to import the following namespaces.
C#
using System.Threading;
using System.Globalization;
VB.Net
Imports System.Threading
Imports System.Globalization
Detecting Browser Language and Culture in ASP.Net
Inside the InitializeCulture event handler, the page is overridden and the User’s default language in browser is detected and is set as Current Culture.
Then inside the Page Load event of the page, the name of the Current Culture is set to the lblCulture Label.
C#
protected override void InitializeCulture()
{
string language = "en-us";
//Detect User's Language.
if (Request.UserLanguages != null)
{
//Set the Language.
language = Request.UserLanguages[0];
}
//Set the Culture.
Thread.CurrentThread.CurrentCulture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
}
protected void Page_Load(object sender, EventArgs e)
{
lblCulture.Text = CultureInfo.CurrentCulture.Name;
}
VB.Net
Protected Overrides Sub InitializeCulture()
Dim language As String = "en-us"
'Detect User's Language.
If Request.UserLanguages IsNot Nothing Then
'Set the Language.
language = Request.UserLanguages(0)
End If
'Set the Culture.
Thread.CurrentThread.CurrentCulture = New CultureInfo(language)
Thread.CurrentThread.CurrentUICulture = New CultureInfo(language)
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
lblCulture.Text = CultureInfo.CurrentCulture.Name
End Sub
Testing default Browser language detection in ASP.Net
In order to test whether the
ASP.Net website detects and loads the default
user’s browser language, you will need open Internet Options
window from the Tools menu of the Internet Explorer browser and then click on
Languages button and finally add and make
French language as
default.
Now run the website and it will display the contents in
French Language and also the
Label will display the
French Culture.
Demo
Downloads