Localizing a sitemap

ASP.NET 2.0 has a very nice mechanism to localize resources:



  • Explicitly using expressions such as
    <asp:Label ID=”someLabel” Text='<%$ Resources: Common, HelloText %>’ … />

  • Implictly for all properties marked with the LocalizableAttribute
    <asp:Label ID=”someLabel” meta:ResourceKey=”someLabel” … />

  • Programmatically using calls to Page.GetLocalResourceObject(…) or Page.GetGlobalResourceObject.

This applies to all controls. The Menu and SiteMapPath controls however can be linked to a SiteMapDataSource and have no way to directly localize the node/items that are in the sitemap. The sitemap itself can be localized anyway. Here’s how:


First of all, mark the document element of the XML file web.sitemap with the attribute enableLocalization=”true”. Next add a resourceKey attribute to all siteMapNode elements that you want to localize.



<siteMap xmlns=http://schemas.microsoft.com/AspNet/SiteMap-File-1.0 enableLocalization=true>


  <siteMapNode title=Root description=My Links>


    <siteMapNode url=~/Default.aspx title=Home resourceKey=Home description=Go to homepage />


    <siteMapNode url=~/Admin/EditPosting.aspx title=Add Entry resourceKey=AddEntry description=Adds a new posting />


    …


  </siteMapNode>


</siteMap>


This is the implicit version of the localization. The Title and Description properties of the nodes can now be added to a resx file by using the ResourceKey.Property syntax. For example, for the Home node (see above) this would be Home.Title and Home.Description.


Now here comes the funny part: the resource file should be called Web.sitemap.resx. Wait, that’s not funny yet. It must be placed in the App_GlobalResources folder of the web application. “Hahahaha. Wait, I don’t get it?!” Well, the funny thing is that officially global resources do not allow a period (.) in the resource key (You normally replace the periods with underscores or so). See what happens in VS2005 when you do.



Just ignore those nasty red dots. Fire up your site once again and see the magic happen.


Update: The syntax to use explicit resources is like below.



<siteMapNode url=~/Admin/EditPosting.aspx title=$Resources: SiteMapResourceFile, EditPosting =“” />


Your resource file should be located in App_GlobalResources as well and must be called ClassName.culture.resx, which means SiteMapResourceFile.nl-NL.resx in the example above. Strangely enough the design is not consistent in for local resources and sitemaps.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s