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.
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.
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.