dotnetnuke localresourcefile does not return resource file name for dynamically loaded control

981 views Asked by At

I am using DNN 7.04.

I am trying to get a string from a resource file and have tried these:

var path1 = Localization.GetString("MyAdsPath.Text", LocalResourceFile);
var path2 = Localization.GetString("MyAdsPath.Text", LocalResourceFile + "/AdditionalInfo.ascx.resx");

path1 returns null and path2 returns the correct string from the resource file.

In both cases LocalResourcfile returns:

/desktopmodules/qEmployerCreateAd/App_LocalResources/

The problem is I don't want to hard code the resource file name as it will change when the language changes.

I think the issue is to do with the control being dynamically loaded. Do I have to check the culture and then hard code the resource file name? Or is there a better solution?

thanks Norb

2

There are 2 answers

0
Norbert Norbertson On

I found a solution that works:

http://weblogs.asp.net/anasghanem/fixing-dotnetnuke-localization-for-child-and-dynamically-loaded-usercontrols

In case that link dies here is the solution:

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";
}
}
2
Chris Hammond On

My solution to this can be found in DNNSimpleArticle

But basically when loading the dynamic control, you just need to pass along the module configuration from the parent to it.

try
{
    var controlToLoad = "Controls/ArticleList.ascx";
    if (ArticleId > 0)
        controlToLoad = "Controls/ArticleView.ascx";

    var mbl = (dnnsimplearticleModuleBase)LoadControl(controlToLoad);
    mbl.ModuleConfiguration = ModuleConfiguration;
    mbl.ID = System.IO.Path.GetFileNameWithoutExtension(controlToLoad);
    phViewControl.Controls.Add(mbl);
}
catch (Exception exc) //Module failed to load
{
    Exceptions.ProcessModuleLoadException(this, exc);
}