Using Resources for Localization with ASP.NET 2.0
To localize a Web application we can use resource files. There a three way of using resources in ASP.Net 2.0 to localize a web application.
* Use ResourceManager to get a string programmatically
* Use Implicit Localization
* Use Explicit Localization
Many of you are probably already familiar with how to use the ResourceManager class to get a string from a resource file. So I will not spend some time writing about that. Instead I will focus on the two new localization features in Asp.Net 2.0, Implicit and explicit localization. If we should use the ResourceManager class, we have to programmatically get a string from a resource file with the GetString method and set the returned value to a control’s property or properties, such as the Text or ToolTip property of a TextBox control etc. But with the new implicit and explicit localization feature we don’t need to do it programmatically, the localization feature are doing this automatically for us.
Implicit Localization
To use implicit localization on a page, each page that should be localized must have its own resource file (.resx). The resource file contains each control on the page and the control’s properties and values that are marked with the “localizable” attribute:
[System.ComponentModel.Localizable(true)]
public string MyProperty
{
get { return this._value; }
set { this._value = value; }
}
Note: Most of the server control shipped with ASP.Net, such as TextBox, Label, Button etc has the LocalizableAttribute added to the properties that are text-based.”
There is a tool added to VS 2005 that will generate a resource for us. Before we generate a resource file, we need to add all the control’s that we are going to use on our page. To generate a file, choose Tools in the menu and select “Generate Resource file”.
Note: If you can’t find this option, you need to import the IDE settings for web application. You can do this by selecting the Tools menu, and Import/Export Settings, and choose Reset IDE settings and then select the “Web.vssettings” file located in the list box with installed settings files.
When we have generated a resource file, a LocalResource folder will be added to our web project root folder with will contain the resource file for our page. The resource file will contain each control added to the page and its properties with its values that are marked with the “Localizable” attribute:
Key Value
ButtonResource1.Text My Button’s Text
ButtonResource1.Tooltip My Button’s Tooltip
When the we have generated the resource file, it will also add a meta:resourcekey attribute to our control’s markup:
<asp:Button ID="Button1" Runat="server" Text="My Button" meta:resourcekey="ButtonResource1" />
At runtime, ASP.Net will examine the controls one the page. If the control is marked with the meta:resourcekey attribute, ASP.Net will look in the resource file for the page, if it could find the property of the marked control, ASP.Net will substitute the values in the resource file for those in the control. To create a resource file for other languages, we have to make a copy of the generated resource file and add the ISO culture code to the file, for example if the page we generate a resource file has the name “default” the resource file will be “default.resx”. If we want to have a resource file for Swedish, we make a copy the default resource file “default.resx” and add the culture code for Swedish (sv-SE) before the .resx file extension “Default.sv-SV.resx”. As you may know the runtime will load the right resource file base on the current UI culture when we use the ResourceManager, It works the same way for both implicit and explicit localization.
The implicit localization must have a resource file for each page in our web application that should use localization. The disadvantage with implicit localization is that we could not reuse the strings in the resource file for other pages. If we want to reuse localized data located in a resource files and want more control over which properties we want to set, we can instead use explicit localization.
Explicit Localization
With explicit localization we will have more control over which properties that we want to set. To do this we are using an expression that will point to a resource file. The expression should be added to the control’s property that we want to set. For example:
<asp:Button ID="Button1" Runat="server" Text="<%$ Resources: ClassName, ResoureKey %>">
This can be done either manually or through the property window of the control. In the property windows you select the dotted button right to the “Expressions” options, select the property you want to add an expression to and select Resources as the Expression type. In the “Expression Properties” window, you write the ClassName of the resource file as an value to the ClassName option, and in the ResourceKey option you write the name of the resource key you have added to you resource file.
The ClassName is the name of the resource file without any culture or .resx extension. It’s the base part of the resource file. For example if we have those files:
Strings.resx
Strings.sv-SE.resx
The LocalizedText should be ”Strings”.
We have to create our own resource file for each language and it would be placed into the Resources folder in our web project root folder. If the folder doesn’t exits, we create it. To create a resource file, we right click on our Resources folder, select “Add new Item” and select the “Assembly Resource file” template.
When a page runs the expression will be evaluated and the specified resource file will be loaded based on the current UI culture, and the resource key’s value specified in the expression will be set to the property of the control. The different between implicit and explicit localization is that explicit localization can have one or several resource files, but the implicit must have a resource file for each page.