Friday, October 14, 2011

Localizing ASP.NET Tips

There are two kind of localizations:

  • Local resources are specific to a page.
  • Global resources are shared throughout the site

If your site contains many folders, you might have an App_LocalResources subfolder in each folder of your site.

<asp:Button ID="ButtonFind" runat="server" Text="Find" CssClass="submitButton" meta:resourcekey="ButtonFindResource1" />

<asp:Localize ID="LabelTitle" runat="server" Text="Customer Lookup" meta:resourcekey="LabelTitleResource1"></asp:Localize>

The Text property of the Button control is to aid the developer at design time.

ASP.NET will use the key to find and match on any property that might be set inside the resource file as meta:resourcekey.<PropertyName> or ButtonFindResoure1.Text:

<data name="ButtonFindResource1.Text" xml:space="preserve">



<data name="ButtonFindResource1.Tooltip" xml:space="preserve">

     <value>Click this to start the search action</value>


Accessing a Local resource in C# could be done as follows:

Textbox1.Text = GetLocalResourceObject("Textbox1.Text").ToString();

Accessing a shared global resource in C# could be done in one of the two followings:

Textbox1.Text = Resources.ResourceFileName.Textbox1Text;

Textbox1.Text = GetGlobalResourceObject("ResourceFileName", "Textbox1Text") as string;

The methods GetGlobalResourceObject  and GetLocalResourceObject can come handy when the resources does not exist at compile time and might be provided as a separate dll at runtime.