I have been doing some research into some of the Localization features available in .NET 2.0. For reference I used the excellent article Susan previously blogged:
http://blogs.vertigosoftware.com/swarren/articles/1343.aspx
In this article Susan places strings in .resx files for each culture the site will support along with a default .resx for the fallback language i.e.:
Strings.resx = Fall back language (English)
Strings.de.resx = German
Strings.fr.resx = French
In 1.1 in order to reference values within these resource files you have to:
- Create an instance of the ResourceManager object and specify the name if the resource class along with the string you wanted to return
In Susan’s example she creates an instance of this object in global.asax application start. She also wrote a helper class to return the strings.
- 2. Determine the current culture and add it to the current context.
In Susan’s example she determines the culture by looking at the users browser settings in application begin request and settling the first language found. As a fall back she sets the default language.
.NET 2.0 can remove these 2 manual steps.
Step 1 – Automatic ResourceManager
.resx files are now added to the “app_globalresources” folder. If .resx files exist in this folder .NET will automatically instantiate a ResourceManager. This ResourceManager is strongly typed with intellisence support. In html you can use an expression to bind to this resource i.e.
<asp:Label ID="Label1" runat="server" Text='<%$ Resources:Strings, App_Title %>'></asp:Label>
Within the Visual studio designer you can do this visually by clicking on the label and setting its expressions property. Set expression type to resources, class key to your resource (strings) and resource key to the name of your resource.
You can also set this in code behind with full intellisence i.e.
Label1.Text = Resources.Strings.App_Title;
Step 2- Automatic culture detection
In .NET 2.0 you can set the culture and uiculture of an app at the page or web.config level.
To set it at the page level add culture and uiculture elements to the page directive. i.e.
<%@ Page Language="C#" AutoEventWireup="true" Culture="auto:en-US" UICulture="auto:en-US" CodeFile="Default.aspx.cs" Inherits="_Default" %>
To set it at the web.config level add a globalization section:
<system.web>
<globalization culture="auto:en-US" uiculture="auto:en-US"/>
</system.web>
"Culture" is used to specify the culture in which things like dates and calendars should be displayed. "uiculture" is used to specify user interface elements such as which resources to use.
The auto before the colon will use the first item in the users browser preferences. The en-US after the colon is the language that will be used if the user hasn’t specified any preferences. This is the “Fall Back” language.
If you don’t want to rely on the users browser preference for culture selection you can override the InitializeCulture() method of the page. For example you could have a page which asks the user to select their preferred language and store it in their profile. You could then reference this profile to set the culture. i.e.
protected override void InitializeCulture()
{
// override virtual method InitializeCulture() to check if profile contains a user language setting
string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString();
if ( UserCulture != "")
{
// there is a user language setting in the profile: switch to it
Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
}
}
New asp:localize control
ASP.NET 2.0 comes with a new Localize control. This control is inherited from the Literal control. The main difference being the text property of this control is visible within the visual studio designer. The text property is not visible with the Literal Control.
To take advantage of the new resource expression binding syntax controls have to be marked as runat=”server”.
<asp:Localize runat=”server” text='<%$ Resources:Strings, App_Title %>'/>
Conclusion
Susan’s article is a great reference if you need to implement localization into your web applications. All of the necessary steps are relevant whether the app is 1.1 or 2.0. .NET 2.0 will take some of the manual steps out of this process.
.NET 2 has several other Localization features such as generation of local resources per page, user control etc and specific localization expressions. I need to explore these features in more detail before blogging about them.