Search code examples
c#webformsdotnetnukeresx

Proper way of accessing resource file for a User Control added to DNN Module?


I'm working with the Christoc Module Template, trying to create a module with localized text. I've added a new User Control called TeamList.ascx to the project, and to the App_LocalResources folder I've added two new files:

TeamList.ascx.resx and TeamList.ascx.fr-CA.resx (for Canadian french).

I can include the user control in my view with no issues, but when the module loads on the page, all of the places containing localized text are blank.

I have tried registering the user control by including:

<moduleControl>
   <controlKey>Teams</controlKey>
   <controlSrc>
      DesktopModules/LTSC_DashboardModule/TeamList.ascx
   </controlSrc>
   <supportsPartialRendering>False</supportsPartialRendering>
   <controlTitle>Team Control</controlTitle>
   <controlType>View</controlType>
   <iconFile />
   <helpUrl />
   <viewOrder>0</viewOrder>
   <supportsPopUps>True</supportsPopUps>
</moduleControl>

...in the DNN manifest file, but it has no effect.

I'm not sure what I'm missing here. I think it's probably something simple.

Thanks in advance for any help.


Solution

  • Accessing the Resource File is done by using the localization methods in DNN. You'll need to post how you are referencing the resource files to ultimately figure out what you are doing wrong.

    Adding a resource file is simple, create a RESX file to match your ASCX file

    TeamList.ascx would get a resource file in App_LocalResources/ called TeamList.ascx.resx resulting in the path /app_localresources/teamlist.ascx.resx

    To access that path from code behind, you would simply call

    var stringValue = Localization.GetString("STRINGNAME.Text", LocalResourceFile)
    

    Where LocalResourceFile is inherited from PortalModuleBase (you need to have your ASCX file inherit the base class that my templates create, they inherit from PMB, or you can inherit from PMB directly.

    Accessing it from within the ASCX file can be done similarly

    <%=Localization.GetString("STRINGNAME.Text", LocalResourceFile)%>
    

    Of from within controls with

    <asp:label id="SOMEID" ResourceKey="STRINGNAME.Text" runat="server" />
    

    The only other potential catch is if you are loading your ASCX file into another ASCX file, if so, you need to pass the moduleconfiguration to the child ASCX file in codebehind

    I do this in the DNNSimpleArticle module with

    var mbl = (dnnsimplearticleModuleBase)LoadControl(controlToLoad);
    mbl.ModuleConfiguration = ModuleConfiguration;
    mbl.ID = System.IO.Path.GetFileNameWithoutExtension(controlToLoad);
    phViewControl.Controls.Add(mbl);
    

    Does that get you pointed in the right direction?