navigation
 Monday, August 27, 2007

If you need to share resources across multiple pages you can use global resources as opposed to the local resources shown in the previous blog. Global resources are implemented using explicit binding, i.e. server tags like this one:

<%$ Resources:MyGlobalResources, OrderButtonText %>

The syntax is <% Resources:Class, ResourceID %> where Class is the name of the resource file, but without the culture and "resx" extensions.  ResourceID is the name of the entry in the resource file.

By the way, you can use explicit syntax for local resources, but must use explicit syntax for global resources.

The basic steps for global resources to work are:

  • Making sure you have the correct UICulture page directive defined.
  • Adding an App_GlobalResources folder to your project.
  • Add resources files to the App_GlobalResources folder.
  • Add name/value pairs to the resource files.
  • Sprinkle the explicit syntax in the ASP.NET markup where you need localized text.
  • Change your browser language settings.

Try this step-by-step in an example with a RadioButtonList of drinks and an Order button. 

  • Create a web application. 
  • On the default page drop a RadioButtonList and a Button.
  • In the default page add UICulture="auto" to your page directive.  That is the minimum to get translated text showing up when you change your browser settings.  Typically you will also add Culture="auto" to globalize things like date/time and currency formats.
  • Right-click your project and select from the context menu Add | Add ASP.NET Folder | App_GlobalResources. This will contain all your resource files to be used throughout the project.
  • Right-click App_GlobalResources and from the context menu select Add | New Item | Resources File. In this example I'm naming the resource file "MyGlobalResources.resx".
  • In the Solution Explorer double-click your resource file and enter the following Name/Value pairs.image

 

 

 

 

 

 

 

  • Copy the resource file to a new name "MyGlobalResources.fr-FR.resx".  This will contain the French translation (or the nearest Google Language Tools approximation). Modify the Value column entries as shown in the screenshot below.image

 

 

 

 

 

 

  • Enter the following explicit syntax in the ASP.NET HTML markup to provide localized text to your RadioButtonList and Button (go ahead, copy the whole thing, no one's watching).

<div> <asp:RadioButtonList ID="rblDrinks" runat="server" Width="262px"> <asp:ListItem Selected="True" Text="<%$ Resources:MyGlobalResources, CoffeeText %>"></asp:ListItem> <asp:ListItem Text="<%$ Resources:MyGlobalResources, OrangeJuiceText %>"></asp:ListItem> <asp:ListItem Text="<%$ Resources:MyGlobalResources, MangoJuiceText %>"></asp:ListItem> <asp:ListItem Text="<%$ Resources:MyGlobalResources, BottledWaterText %>"></asp:ListItem> </asp:RadioButtonList> <asp:Button ID="btnOrder" runat="server" Text="<%$ Resources:MyGlobalResources, OrderButtonText %>" Width="113px" /> </div>

  • Run the application.  Assuming Internet Explorer as your browser, go to Tools | Options | Languages.  Select the "fr-FR" culture code, then refresh the page.image

 

 

 

 

 

Global resources let you organize your resource files however you like and let you re-use the material anywhere in the project. Global and local resources are both codeless and relatively painless methods you can use to localize your site.

Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, i, strike, u) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview