Auto-culture handling can be enabled for each page in
ASP.NET 2.0 by including the Culture="auto"
and UICulture="auto" attributes in the Page
directive of each page. UICulture can be considered as the physical UI, fonts, colors
etc. Culture affects formatting, for example date formats,numeric formats. Once
enabled, the ASP.NET runtime will automatically map Accept-Language headers to
CultureInfo objects and attach them to the current thread. As a developer we
need to concentrate on creating the resource file for different cultures that
need to be supported in the web Application.ASP.NET will then automatically
load the appropriate resources for each culture.
The Resource files can be created by right clicking on the
Project->Add New Item->Resource in the Solution Explorer. The extension
names for the Resource file is .resx. While creation we need to follow the
naming conventions. For example, if the aspx page name is “form1.aspx” then the
English resource file for the page should be “form1.aspx.en.resx” where en is
the keyword for English culture. Similarly, for French the resource file name
should be “form1.aspx.fr.resx”.
Note: There should be one default
resource file for each page. In case ASP.NET does not find the appropriate
resource for the current culture then the default resource file will be
automatically loaded. The naming convention for the default resource file
should be in “pagename.aspx.resx” format. For the above example the default
resource file name should be “form1.aspx.resx”.
Figure 1
In ASP.NET 2.0 the local resource files is placed inside the
App_LocalResources folder.
Figure 2
The resource files can be edited by
double clicking on the resource file name in the solution explorer (Please see
Resource Editor).Every resource file contains three fields Name, Value and
Comment. Here I will try to explain it with an example so that it will be
easier for the user to understand.
Suppose in your web page say “form1.aspx” you have a label
control id=label1 and the user wants to make its text property to be supported
for english and french language (“Welcome” for english and “Accueil” for
french)
In the form1.aspx.en.resx file give a name say “test” and
value as "Welcome"
Name Value Comment
test Welcome
Similarly in the form1.aspx.fr.resx file
Name Value Comment
test Accueil
Here you must have noticed that “test” is common for both
the resource files. This is because in the ASP.NET page the Name field will be
called for loading the value to the control. The resource file can be called
both at design and at runtime.
In design time the resource file is called by adding the
following piece of code in the text property of the control.
Listing 1
<%$ Resources:<ResourceName>%>
For the above example, the HTML tag for the Label control
will look like
Listing 2
<asp:Label ID="label1" Runat=Server Text="<%$ Resources:test%>"> </asp:Label>
At runtime the resource file is called by using API's
function. GetLocalResourceObject is the function for this purpose.
Listing 3
Me.label1.Text =Convert.ToString(GetLocalResourceObject("test"))
For this we have to use the following name spaces
Listing 4
Imports System.Threading
Imports System.Resources
Imports System.Globalization
So make sure to include them in your page.
Depending upon the culture set in the application ASP.NET
will look for the resource name in the resource files. Here if the culture is French,
ASP.NET will look for test resource name in form1.aspx.fr.resx file and will
load its value to the appropriate property of the control.
Now to test this, change the default culture on your
browser.
On IE 6 this can be done by going to
Tools -> Internet Options -> Languages
Click on Add to add the French language to the language list
and then move the French language to the top of the list by selecting the
language and clicking the MoveUp button. Now the default culture of your
browser is set to French.
Figure 3
Figure 4
Reload the Page; you will see that the Web Server has served
the French resource in the label control.