How to use resx resource file in T4 template

I cannot figure out how to include a resource file (.resx) in a T4 template (.tt).

I have tried so far ... Import Namespace

<#@ import namespace="T4TemplateResources.resx" #>

Also includes a class.

+2
source share
4 answers

The Nico solution requires the creation of your solution.

There is another way, without having to compile your solution by reading the resx source file.

    var fileName = "CustomResource.resx";
    var filePath = Path.Combine(Path.GetDirectoryName(this.Host.ResolvePath("")), "WindowsFormsApplication1", fileName);
    var reader = new ResXResourceReader(filePath);
    var values = reader.Cast<DictionaryEntry>().ToDictionary(x => x.Key, y => y.Value);

    // this is how you would acces the resources
    var value = values["entry"];

You should be aware that this method does not check development time if the resource does not exist and you are not getting localized values ​​because you are just reading the file. Both often not necessary with T4 templates.

The following is a working snippet that creates an enumeration from a resource file.

, fileName filePath

<#@ 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>
        /// <#= summary #>
        /// </summary>
        <#= name #>,

        <# } #>

    }

}


<#
    }
#>
+3

T4 (.resx) JS JSON :

<#@ 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("'", "\\'")#>',
<# } #>
<# } #>
};

: T4 JavaScript .resx

+1

.resx T4, :

  • "Public".
  • , . t4 - , .
  • T4 ( Visual Studio): <#@ assembly name="$(TargetDir)\outputfile.ext" #>
  • ResourceFile T4 <#@ import namespace="MyNamespace" #>

Then you can access the resources as usual:

<# var theResource = Resource1.TheResource; #>
0
source

There is an easier way to do this without compiling your project if you are using VS2010 SP1 and above using

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

Copy the first line as it is, and in the second line use the namespace in which the resource file is located and usually access the resource lines, as in C #

Resources.ResourceManager.GetString("SomeKey");

or

var key = Resources.SomeKey;

I found out about this from Use a class inside a T4 template

Hope this helps someone

0
source

All Articles