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
- Delete the navigation from the onet.xml
- 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); }
Tags: multi language, SharePoint, SharePoint 2010
September 11, 2012 at 7:22 am |
Where did you placed the code GetNavigation() ?
September 11, 2012 at 8:29 am |
Hi. you can create following class http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spwebtemplate.provisionclass.aspx and link it with the template. or you can create a new FeatureReceiver class (http://msdn.microsoft.com/en-us/library/ee231604.aspx) for a custom feature that will be activated during creating of the template.
September 11, 2012 at 8:34 am
Hi,
actually i want to localize the Tab in my search center page. I have created the list instance for tab in onet.xml and for each tab i have specified the localized string in the onet.xml like this
$Resources:Microsoft.Office.Server.Search,DocumentsTabComment;
Here the issue is, its retreiving the string for english(default) language. but when i change the language to french it is not picking up the french string.
Any idea on this issue?
October 10, 2013 at 2:56 pm |
Hi Vojtan ,
Thanks for this work around. I have implemented your approach and was able to add the links in the Quick launch successfully.
However, changing the language from the Welcome control does not change the language of my links. Any idea what could have gone wrong ? Selecting the language while creating the site collection in the central admin does work though.
I am using the code as part of a feature which gets activated from the onet.xml whenever a site is created.