Title: dotnetnuke localresourcefile does not return resource file name for dynamically loaded control
Tags: c#;module;localization;dotnetnuke
Question: 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


Here is another answer: 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";

Here is another answer: 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.
var controlToLoad = "Controls/ArticleList.ascx";
if (ArticleId &gt; 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);
Comment for this answer: thanks Chris. Neat. Any pro's with this over the BaseUserControl solution?
Comment for this answer: You don't need to override the files if you aren't changing the names, so no need to rename the LocalResourceFile path

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:06/11/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><module><localization><dotnetnuke>', 'Title': 'dotnetnuke localresourcefile does not return resource file name for dynamically loaded control', 'ViewCount': 928, 'id': 30777310, 'user_AboutMe': '', 'user_AccountId': 3449968.0, 'user_DisplayName': 'Norbert Norbertson', 'user_Reputation': 1996.0, 'user_Views': 216.0, 'user_id': 2889838.0}}