0

我有一个包含我们网站控件的组件项目 - 现在必须可以本地化。为站点的其余部分设置本地化不是问题(我们将.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);
    }
  }
}

new以及带有/out ,等的几种变体System.Web.UI.TemplateControl...

我宁愿不必将其编译为附属程序集,然后将其拉回(这似乎是另一种选择),因为组件项目 (a) 具有许多相关控件,并且 (b)排序的已经依赖卫星了,对吧?我希望我可以将.resx文件放入,编译.DLL,然后项目的其余部分可以工作。

[编辑] 我觉得我越来越接近答案了,但我还没有完全到那里......

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

谢谢!

4

1 回答 1

0

哦,我快接近了 - 我打开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);
于 2012-08-30T18:28:24.157 回答