From <%@ Register TagPrefix="dnn" TagName="NAV" Src="~/Admin/Skins/Nav.ascx" %>
To <%@ Register TagPrefix="dnn" TagName="NAV" Src="~/DesktopModules/LocalizedNav/LocNav.ascx" %>
- Run the portal with the skin to make sure the LocNav.ascx is working properly.
- Then open the file LocNav.ascx.vb and start modifying it (for this post I am using LocNav.ascx.cs since I've converted it to C#).
- Create a private property to get the localized menu from cache, if it is not available it will get from database and stored it into cache again. Refer to http://www.west-wind.com/presentations/wwDbResourceProvider/ article to implement customized method/class to retreive all localized values from database.
Dictionary<string, Dictionary<string, string>> LocalResource
{
get
{
string strCacheKey = "menus";
Dictionary<string, Dictionary<string, string>> resources = DataCache.GetCache<Dictionary<string, Dictionary<string, string>>>(strCacheKey);
if (resources == null) //If resources not available in cache, retrieve from database
{
resources = DbResource.GetStrings("menus"); //This customized method/class to get all localized values from database
int cacheTimeout = 20 * Convert.ToInt32(DotNetNuke.Entities.Host.Host.PerformanceSetting);
DataCache.SetCache(strCacheKey, resources, TimeSpan.FromMinutes(cacheTimeout));
}
return resources;
}
}
- Next create a private method to retrieve specific localized value of menu from LocalResource property.
private string GetLocalResource(string resourceId)
{
Dictionary<string, string> resources;
LocalResource.TryGetValue(System.Threading.Thread.CurrentThread.CurrentCulture.Name, out resources);
string resourceValue = string.Empty;
if (resources != null)
{
resources.TryGetValue(resourceId, out resourceValue);
}
if (string.IsNullOrEmpty(resourceValue))
{
return string.Empty;
}
else
{
return resourceValue;
}
}
- Then create a recursive method to perform localization on portal menus. We need a recursive method because of the complex hierarchy of the menus. We cannot fixed or can never tell how many levels each node of menu has. The recursive method will drill down into each level and perform the translation as long as the node have subnode (another sub level of menus).
private void LocalizedNode(DNNNodeCollection nodes)
{
foreach (DNNNode node in nodes)
{
string tabResourceId = "tab_" + node.Text.ToLower().Replace(" ", string.Empty);
if (!string.IsNullOrEmpty(GetLocalResource(tabResourceId)))
{
node.Text = GetLocalResource(tabResourceId);
}
if (node.HasNodes)
{
LocalizedNode(node.DNNNodes);
}
}
}
- Next, locale the private void BuildNodes(DNNNode objNode) method. Inside this method we will call the recursive method to perform localization on portal menus.
private void BuildNodes(DNNNode objNode)
{
DNNNodeCollection objNodes = default(DNNNodeCollection);
objNodes = GetNavigationNodes(objNode); //this is where DNN will get and generate all menus as DNNNodeCollection
//Perform tabs/menus localization
LocalizedNode(objNodes); //this is where we will call the recursive method
this.Control.ClearNodes();
//since we always bind we need to clear the nodes for providers that maintain their state
this.Bind(objNodes); //this is where DNN will bind those menus to navigation control
}