访问(待)编译控件内的本地化资源



我有一个组件项目,为我们的网站提供控件,现在必须能够本地化。为站点的其余部分设置本地化不是问题(我们将.resx文件放入App_GlobalResources目录;一切都很好),但我很难找到有关如何在组件项目中本地访问资源的信息。

  • App_Local[/Global]Resources文件夹不需要工作——我不能像在主项目中那样"添加ASP.NET文件夹"
  • 当我自己生成它并将.resx文件放入其中时,我不知道如何像在网站上那样访问它们

一些缩写的控制代码,完全包含在组件项目的ThisWidget.cs中:

namespace site.shop {
  [ToolboxData("<{0}:ThisWidget runat=server></{0}:ThisWidget>")]
  public class ThisWidget : WebControl {
    protected override void CreateChildControls() {
      if (ChildControlsCreated) { return; }
      this.Controls.Clear();
      Label head = new Label();
      head.CssClass = "header";
      // fails: ThisWidget does not exist in namespace
      head.Text = System.Resources.ThisWidget.Label_head;
      // fails: can't find GetLocalResourceObject
      head.Text = new System.Web.UI.TemplateControl.GetLocalResourceObject("Label_head");
      // fails: can't find linked or embedded resources
      System.Resources.ResourceManager rm = new ResourceManager();
      head.Text = Convert.ToString(rm.GetObject("Label_head"));
      this.Controls.Add(head);
    }
  }
}

以及带有/不带有newSystem.Web.UI.TemplateControl等的几种变体…

我不想把它编译成一个附属程序集,然后再把它拉回来(这似乎是另一种选择),因为组件项目(a)有很多相关的控件,而(b)已经是一种附属依赖了,对吧?我希望我可以把.resx文件放进去,编译.DLL,然后项目的其余部分就可以工作了。

[edit]我觉得我离答案越来越近了,但我只是还没有完全找到。。。

有什么想法吗?我错过了什么?

谢谢!

哦,我快接近了——我打开了ThisWidget.Designer.cs,找到了正确的参数,用调用ResourceManager()

ResourceManager rm = new ResourceManager("Components.App_LocalResources.ThisWidget", typeof(ThisWidget).Assembly);
head.Text = Convert.ToString(rm.GetObject("Label_head"));

看起来很有魅力。我还意识到,由于它忽略了App_LocalResources的特殊性,我可以把它放在任何地方,所以我创建了一个/Resources/Resx/目录,并把每个组件控件的.resx文件放在其中。为了更新调用,这个:

ResourceManager rm = new ResourceManager("Components.Resources.Resx.ThisWidget", typeof(ThisWidget).Assembly);

最新更新