繁体   English   中英

在(待)编译控件中访问本地化资源

[英]accessing localized resources within a (to be) compiled control

我有一个包含用于我们网站的控件的组件项目-现在必须可以本地化。 为网站的其余部分设置本地化不是问题(我们将.resx文件拖放到App_GlobalResources目录中;所有内容都是笨拙的),但是我很难找到有关如何在本地访问资源的信息。组件项目。

  • App_Local[/Global]Resources文件夹不想工作-我无法像从主项目中那样“添加ASP.NET文件夹”。
  • 当我自己生成它并将.resx文件放入其中时,我不知道如何像在网站上那样访问它们。

一些缩写的控制代码,完全ThisWidget.cs在组件项目的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 ,然后该项目的其余部分可以正常工作。

[编辑]我觉得我已经接近答案了,但是我还没到那...

有任何想法吗? 我想念什么?

谢谢!

哦,我接近了-我弹出打开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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM