What is Resource File?
A resource file is
a XML file that contains the strings that we want to
a. Translate into different languages.
b. Can be updated dynamically so that
user themselves can modify values in resource files once the application is deployed on the server without re-compiling the entire application itself.
The resource file contains key / value pairs.
Each pair is an individual resource.
Key names are not case sensitive.
Types of Resources:
There are two types of resources
- Local Resources
- Global Resources
Local Resources:
Local resource is specific to a single Web page and used for providing versions of a Web page in different languages.
Local resources must be stored in App_LocalResources sub folder.
Local resources must be named in format <
WebPageName> [
.language / language and culture]
.resx.
Ex: Default
.aspx.resx- Base resource file. This is the default, or
fallback, resource file.
Default.aspx.de.resx- A resource file for German etc.
Generating Default Resource File using VS2005:
In Visual Studio designer, click the designer surface or a control.
Select Tools --> Generate Local Resource
An XML - based local resource file for the Web page in the App_LocalResources folder with Text and ToolTip values for all existing controls on the page, as well as the page title will be generated.
Generating Resource Files for Other Cultures:
In Solution Explorer
Right click the 'Default
.aspx.resx' file and click the Copy
Right click the App_LocalResources folder and click the Paste
Right click the 'Copy of Default
.aspx.resx' and click the Rename
Type the new name for the resource file that includes the new language and culture code before the extension.
Ex: 'Default.aspx.fr.resx' to create a French language version of 'Default.aspx' page.
Double click the resource file to open it in Visual Studio. Update the values of each resource for the new culture, and save the file
Testing Resource Files for Other Culture
ASP.NET automatically determines the user's preferred culture based on information provided by the Web Browser. To test other culture we need to update the preferred language in Web Browser as
In Internet Explorer, select Tools --> Internet Options
Under General tab, select Languages
In Language Preference dialog box click on Add button, Select the language from Add Language dialog box and click on Ok button
In Language Preference dialog box, under Language list make newly added language on top using Move Up , and then click on Ok
Now Webpage will be displayed using selected language resource.
Global Resources:
Global resource can be read from any page or code that is in the application.
Global resource must be stored in App_GlobalResources at the root of the application.
Generating Global Resource File using VS2005:
In Solution Explorer, right-click the root of your Web Site or Project in Web Application, click add
ASP.NET folder and the click App_GlobalResources folder
Right click the App_GlobalResources folder, then click on add new item.
Under visual studio installed template, click on Resource file. Give any name with an extension .resx [e.g.MyResource.resx].
Copy and paste the resource file to create the resource file for creating different cultures. For each culture, add the culture identifier immediately before the .resx extension [e.g.- MyResource.fr.resx- for French culture]
Working with Resources in Web Pages:
Resource files are used in
ASP.NET
To fill the property values of controls on the page.
To update the resource values dynamically i.e. after publishing the web site, to change the Page Titles, Error Messages, and Labels Text etc.
To use resource to set control's property values, we can use
Implicit localization
If local resource files are created in Application, then implicit localization can be used to fill the property values for the control.
To use implicit localization, following naming convention must be used for resources in local resource file
key.property
Where key: Any name for the resource
Property: Property of the control that we are localizing
Ex: If we are creating resource for control label named lblErrorMsg, you need to create following key/value pairs in local resource file
lblErrorMsg.Text = "Error Message"
lblErrorMsg.ForeColor= "Red"
In aspx page, we need to use a special meta attribute in the markup for the control to specify implicit localization as-
<asp:Label ID="lblErrorMsg" runat="server" meta:resourcekey=" lblErrorMsg" Text="Label"></asp:Label>
No need to explicitly specify which properties are localized.
The resourcekey value matches a key in resource file.
At run time,
ASP.NET matches resources to control properties using the resourcekey, if property value is defined in resource file,
ASP.NET substitute the resource value for the property.
Explicit localization
We use resource expression for each property of a control
For ex: A label control that is configured to set a text and fore color property from global resource look like-
<asp:Label ID="lblErrorMsg" runat="server" Text="<%$ Resources:ResourceFileName, lblErrorsgText %>"></asp:Label>
The resource expression takes the following form
<%$ Resources:Class, ResourceID %>
Where Class: Identifies the resource file to be used
ResourceID: Identifier of the resource to be read from resource file
Localizing Static Text
ASP.NET Localize control is used to localize the static text on the page.
The Localize control renders no markup, it just function to act as a place holder for static text
At run time
ASP.NET treat Localize control as a Literal control
<asp:Localize ID="WelcomeMessage" runat="server" meta:resourcekey="literalResource" Text="WelcomeMessage" />
Retrieve Resource Values Programmatically
Call the GetLocalResourceObject() or GetGlobalResourceObject() method to read specific resource from local or global resource file, respectively.
Ex: string localresourcestring = string.Empty;
string globalresourcestring = string.Empty;
// Get the local resource string.
try
{
localresourcestring = (String)GetLocalResourceObject ("LocalResourceString1");
}
catch
{
localresourcestring = "Could not find local resource.";
}
// Get the global resource string.
try
{
globalresourcestring = (String)GetGlobalResourceObject("MyResource", "GlobalResourceString1");
}
catch
{
globalresourcestring = "Could not find global resource.";
}
Editing Resource Files after publishing in
ASP.NET 2.0
The beauty of the resource file in
ASP.NET 2.0 is modifying the resource file after the application is deployed on a server without re-compiling the entire application itself.
But the ability to do so depends on what project model we have followed for Web application in VS 2005
Project Model- Website Project Model (Default in VS2005)
Resource file only under the App_LocalResources will get published as a raw .resx files on the server as these are not compiled. These resources files can be edited on the server as they are compiled during runtime.
Files under the App_GlobalResources folders are compiled into individual resource specific dlls and published on the server, so we cannot edit resource under this folder
Project Model- Web Application Project(WAP)
Files both under the App_LocalResources or App_GlobalResources folders will get published as raw .resx files which are editable.
So with this project model, we are able to edit the resource files after publishing the web application.