Skip Navigation LinksHome > View Post

Localizing Site Map data in ASP.NET 2.0

This article describes two ways of adding localization to your site map data when using the default XmlSiteMapProvider in ASP.NET 2.0. The first example uses implicit expressions and the second example uses explicit expressions.

For the purpose of this article I am going to use the following example Web.sitemap file (I have excluded the namespace for readability):

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="...">
    <siteMapNode url="Default.aspx" title="Home" description="Home page">
        <siteMapNode url="Search.aspx" title="Search" description="Search items" />
</siteMapNode>
</siteMap>

The first step to enable localization is to set the enableLocalization property of the siteMap node to true. <siteMap ... enableLocalization="true">.

The first method of adding localized data to your sitemap uses implicit expressions. Follow the simple steps described below:

  • Add a web.sitemap.resx (plus any web.sitemap.[CultureIdentifer].resx) to your App_GlobalResources folder, where CultureIdentifier is the identifier for any languages you want translated on your site. (E.g. "es" for Spanish).
  • Specify a resourceKey property on each of your <siteMapNode ... /> elements.

Below is an example of the new web.sitemap file and also an example web.sitemap.resx file.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="..." enableLocalization="true">
    <siteMapNode url="Default.aspx" title="Home" description="Home page" resourceKey="Home">
        <siteMapNode url="Search.aspx" title="Search" description="Search items" resourceKey="Search" />
</siteMapNode>
</siteMap>

web.sitemap.resx file

Notice the warning about the name of the resource not being a valid identifier. I wouldn't let this put you off, as it does actually work. Nice one!

In this example the XmlSiteMapProvider looks for the following resource in the web.sitemap.resx file: resourceKey + "." + Property of siteMap node. So for your home page's title above, it will look for the resource Home.Title.

Most likely you have the string "Home" somewhere else in your site, e.g. as the title and heading on your default.aspx page. What if you do not want to duplicate the translation and add the web.sitemap.resx file? This is where the second option of adding resources to your site map comes into play.

See the web.sitemap file below for an example of explicit expressions for localizing site map data:

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="..." enableLocalization="true">
    <siteMapNode url="Default.aspx" title="$resources: MyResourceFile, HOME, Home page" description="$resources: MyResourceFile, HOME_DESCRIPTION, My home page">
        <siteMapNode url="Search.aspx" title="$resources: MyResourceFile, SEARCH, Search item" description="$resources: MyResourceFile, SEARCH_DESCRIPTION, Search your favourite items" />
</siteMapNode>
</siteMap>

For the siteMapNode with url Default.aspx in the example above, the XmlSiteMapProvider would look for the title in a file called "MyResourceFile.resx" in the App_GlobalResources folder. It would then look for the resource key HOME, and if this can not be found the default text "Home page" is displayed.

The explicit expression is defined like this: "$resources: [MyResourceFile], [ResourceKey], [Default text]" where the values in angled brackets are replaced by your specific values.

Now all you need to do is translate your site map data, and for comedy value I would suggest using something like Babel fish from Alta Vista :D

Tags: ASP.NET

 
Bruusi Post By Bruusi
6:24 AM
15 Dec 2005

» Next Post: Performance of Method.Invoke vs a Delegate
« Previous Post: Web Service XML compression with .NET 2.0

Comments are closed for this post.

Posted by Laksh @ 24 Nov 2008 7:26 AM
how do u localize if you are using SQLSiteMapProvider where the SiteMap data is stored in SQL.

Posted by Bruusi @ 24 Nov 2008 7:34 AM
I have not done this and from documentation on MSDN it doesn't look like the SQLSiteMapProvider supports localization:

http://msdn.microsoft.com/en-us/library/aa479033.aspx

"SqlSiteMapProvider is a StaticSiteMapProvider-derivative that demonstrates the key ingredients that go into a custom site map provider. Unlike XmlSiteMapProvider, which reads site map data from an XML file, SqlSiteMapProvider reads site maps from a SQL Server database. It DOESN'T support localization, but it does support other significant features found in XmlSiteMapProvider, including security trimming and site maps of unlimited depth. SqlSiteMapProvider's source code appears below."

© 2005 - 2014 Josh Twist - All Rights Reserved.