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

4 Comments

  • This article is too simple and too good. Explained everything in simple way.

    well written. so easy to understand!

  • Hello Anas,
    please how can i contact you?
    i have to implement a multilingual(English-Arabic) site and i need some help about that?
    this is my email:
    tttottt (at) gmail (dot) com
    thanks in advance

  • hi great post,

    my control is returning the name of the file without the extensions (ascx.resx) and when I am trying to test for French it does not work and only get the english resx files ... my french file is ascx.fr-FR.resx ... do you know why its not reading this file?

    i tested in IE by changing my languange in tools->options

    thanks,

  • I wish all tutorials were as clearly dealt with as this one is. Very good job, and it helped me figure out what the core team claimed was 'not a dotnetnuke issue, but a resource file issue'.

    Not!

Comments have been disabled for this content.