Fixing DotNetNuke localization for child and dynamically loaded UserControls
The DotNetNuke localization engine depends heavily on the "LocalResourceFile" property which is contained in "PortalModuleBase" class which is must be the base class for the UserCotrols that needs to be used as a controls for the Module.
The "LocalresourceFile" property uses the following code to get the correct Localization file for the control ( the code based on DNN 4.08.04)
Me.TemplateSourceDirectory & "/" & Services.Localization.Localization.LocalResourceDirectory & "/" & Me.ID
Note how the property value depends on the UserCotrol ID , this will cause the problem when the UserCotrol used as a child UserCotrol in another UserCotrol , or when its dynamically loaded to a nother UserCotrol (using the LoadControl method) . in both cases the "LocalResourceFile" property will returns incorrect resource file which prevent the DotNetNuke from localizing the control .
Note that when dynamically loading the UserCotrol using LoadControl method , the UserCotrol Id will be null , and when we place the UserCotrol in another UserCotrol , the UserCotrol id will be the Instance Id , for example if the UserCotrol is "Products.ascx" , then its id will be Products1 or the id that is assigned to it by the developer.
To overcome all of the above issues , the solution is to modify the LocalResourceFile for the UserCotrol , this can be applied by creating a new "LocalResourceFile" property that shadows the Property of the PortalModuleBase control (since the property is not overridable ), and statically return the correct LocalResourceFile , the problem in this solution is that you need to apply it to every UserCotrol .
The other solution( and the best I think) is to create a base UserCotrol class that inherits from PortalModuleBase and fix the LocalResourceFile like below :
public partial class BaseUserControl : DotNetNuke.Entities.Modules.PortalModuleBase
{
// basicly this will fix the localization issue
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
string FileName = System.IO.Path.GetFileNameWithoutExtension(this.AppRelativeVirtualPath);
if (this.ID != null)
//this will fix it when its placed as a ChildUserControl
this.LocalResourceFile = this.LocalResourceFile.Replace(this.ID, FileName);
else
// this will fix it when its dynamically loaded using LoadControl method
this.LocalResourceFile = this.LocalResourceFile + FileName + ".ascx.resx";
}
}
Now change your UserControls to inherit from BaseUserControl Instead of directly inheriting PortalModuleBase .
I can place that code in the PortalModuleBase directly , but I don't like the idea of modifying the Core DNN code , It will be nice if the DNN core team added that fix ).
I used that solution in my projects , Please Let me know if there is any issues in it.
Thanks
Anas Ghanem