如何在T4模板中使用resx资源文件

我无法弄清楚如何在(.tt)T4模板中包含资源文件(.resx)。

我到目前为止尝试了…导入名称空间

 

还包括class级

Nico的解决方案需要您的解决方案来构建。

还有另一种方法,无需通过读取原始resx文件来编译解决方案。

  var fileName = "CustomResource.resx"; var filePath = Path.Combine(Path.GetDirectoryName(this.Host.ResolvePath("")), "WindowsFormsApplication1", fileName); var reader = new ResXResourceReader(filePath); var values = reader.Cast().ToDictionary(x => x.Key, y => y.Value); // this is how you would acces the resources var value = values["entry"]; 

您应该知道,如果资源不存在并且您没有获取本地化值,则此方法缺少设计时检查,因为您只是在读取文件。 对于T4模板,两者通常都不是强制性的

这是一个工作剪辑,它从资源文件创建一个枚举。

只需确保使用为fileNamefilePath设置正确的值

 <#@ template debug="false" hostspecific="true" language="C#" #> <#@ output extension=".cs" #> <#@ assembly name="System.Windows.Forms" #> <#@ import namespace="System.Resources" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.ComponentModel.Design" #> <# var nameSpace = "WindowsFormsApplication1"; var enumName = "CustomEnum"; var fileName = "CustomResource.resx"; var filePath = Path.Combine(Path.GetDirectoryName(this.Host.ResolvePath("")), "WindowsFormsApplication10", fileName); using (var reader = new ResXResourceReader(filePath)) { reader.UseResXDataNodes = true; #> namespace <#=nameSpace#> { public enum <#=enumName#> { Undefined, <# foreach(DictionaryEntry entry in reader) { var name = entry.Key; var node = (ResXDataNode)entry.Value; var value = node.GetValue((ITypeResolutionService) null); var comment = node.Comment; var summary = value; if (!String.IsNullOrEmpty(comment)) summary += " - " + comment; #> ///  /// <#= summary #> ///  <#= name #>, <# } #> } } <# } #> 

示例T4模板代码,用于从Resources(.resx)读取并创建具有资源的JSON结果的JS文件:

 <#@ template debug="true" hostspecific="true" language="C#" #> <#@ assembly name="System.Core" #> <#@ assembly name="$(TargetPath)" #> <#@ assembly name="System.Windows.Forms" #> <#@ import namespace="System.Linq" #> <#@ import namespace="System.Resources" #> <#@ import namespace="System.Collections" #> <#@ import namespace="System.IO" #> <#@ import namespace="System.Text" #> <#@ import namespace="System.Collections.Generic" #> <#@ output extension=".js" #> <# var path = Path.GetDirectoryName(Host.TemplateFile); var resourceNames = new string[1] { "Resources" }; #> var $.Resources = { <# foreach ( var name in resourceNames ) { var localeFile = Host.ResolvePath(path + "\\" + name + ".resx"); ResXResourceSet jpResxSet = new ResXResourceSet(localeFile); #> <# foreach (DictionaryEntry item in jpResxSet) { #> '<#=item.Key.ToString()#>' : '<#= ("" + item.Value).Replace("\r\n", string.Empty).Replace("'", "\\'")#>', <# } #> <# } #> }; 

感谢Jochen van Wylick: 使用T4基于.resx文件本地化JavaScript资源

如果要从T4模板中访问.resx-File的资源,您可以这样做:

  1. 在资源编辑器中,将资源的访问修饰符设置为“公共”。
  2. 确保您的项目成功构建。 t4模板只能访问输出程序集 – 因此请检查它是否是最新的。
  3. 引用T4模板中的输出程序集(您可以在此处使用Visual Studio宏): <#@ assembly name="$(TargetDir)\outputfile.ext" #>
  4. 在T4模板中<#@ import namespace="MyNamespace" #> ResourceFile的命名空间<#@ import namespace="MyNamespace" #>

然后您可以通常访问资源:

 <# var theResource = Resource1.TheResource; #> 

如果您正在使用VS2010 SP1及更高版本,则可以通过使用来更轻松地执行此操作而无需编译项目

 <#@ assembly name="$(TargetPath)" #> <#@ import namespace="Your.Namespace.Properties" #> 

按原样复制第一行,在第二行中使用资源文件所在的命名空间并正常访问资源字符串,就像在c#中一样

 Resources.ResourceManager.GetString("SomeKey"); 

要么

 var key = Resources.SomeKey; 

我从T4模板中的Use类中学到了这一点

希望它对某人有帮助