Posts Tagged ‘multi language’

Multilanguage in Navigation using Webtemplate does not work

May 15, 2012

I created a webtemplate (http://msdn.microsoft.com/en-us/library/ms434313.aspx) in SharePoint and added following localized Navigation Bar to the top the navigation in the onet.xml:

<NavBar Name="$Resources:osrvcore,SspAdministrationTopNavBarTitle;" ID="1002">
<NavBarLink Name="$Resources:osrvcore,HelpText;" Url="default.aspx" />
 </NavBar>

then I created a web site based on the template and turned the multilanguage on.

The problem is that the navigation bar stays always in one language and is never localised to current language of the website. If I use the same xml file in the sitetemplate everything works correctly.

The problem is recognized as designed by Microsoft and there is only workaround available.

Solution

  1. Delete the navigation from the onet.xml
  2. Recreate the Navigation with code.

Creating the navigation programmatically

  • Create the xml file called navigation.xml with the navigation and let it be deployed to the layouts folder. The file should have following structure:
</div>
<?xml version="1.0" encoding="utf-8" ?>
<NavBars>
<NavBar Template="templatename">
<NavBarLink Url="$Resources:cmscore,List_Pages_UrlName;/page1.aspx" Name="$Resources:osrvcore,HelpText"/>
 <NavBarLink Url="$Resources:cmscore,List_Pages_UrlName;/page2.aspx" Name="$Resources:osrvcore,HelpText"/>
</NavBar>
</NavBars>
  • Load the xml file:
public IList<NavigationNode> GetNavigation(string templateName)
{
{
var fileName = SPUtility.GetGenericSetupPath("TEMPLATE\\LAYOUTS\\navigation.xml");
var navigation = new List<NavigationNode>();
var data = new XmlDocument();
data.Load(fileName);
var xpath = string.Format("/NavBars/NavBar[@Template='{0}']/NavBarLink", templateName);
var nodes = data.SelectNodes(xpath);

foreach (XmlNode node in nodes)
{
navigation.Add(new NavigationNode
{
Name = node.Attributes["Name"].Value,
Url = node.Attributes["Url"].Value
});
}

return navigation;
}
}
  • Add the navigation nodes to the top navigation. There is also handling if the url contains resources that have to be replaced
foreach (var navigationNode in GetNavigation(templatename))
{
var parsedUrl =  ParseNavigationUrl(navigationNode.Url, web.Language);
var spNavigationNode = new SPNavigationNode(navigationNode.Name, parsedUrl, true);
web.Navigation.TopNavigationBar.AddAsLast(spNavigationNode);
}
private static string ParseNavigationUrl(string url, uint language)
{
var resourceExpression = GetResourceExpressionFromString(url);
if (string.IsNullOrEmpty(resourceExpression))
return url;
var localizedString = SPUtility.GetLocalizedString(resourceExpression, string.Empty, language);           return url.Replace(resourceExpression, localizedString);
}
private static string GetResourceExpressionFromString(string text)
{
if (string.IsNullOrEmpty(text))
return string.Empty;
var dollarIndex = text.IndexOf("$");
var semicolonIndex = text.IndexOf(";");
if ((dollarIndex == -1) || (semicolonIndex == -1))
return string.Empty;
return text.Substring(dollarIndex, semicolonIndex - dollarIndex + 1);
}
Advertisements