Tuesday, June 20, 2017

ways to change standard resource Manager - T4 Template for (standard) Resource Generator

1) use T4 Template instead of Resource Generator


1) add TextTemplate with same name as your resx file
2) copy and paste
3) edit the template to replace standard resourcemanager with your own

//------------------------------------------------------------------------------
//
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.34003
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//
//------------------------------------------------------------------------------
<#@ template hostspecific="true" language="C#" #>
<#@ output extension=".Designer.cs" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="System.IO" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="System.Xml.Linq" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Xml.Linq" #>

namespace WpfExp
{
    using System.Globalization;
    using System.Resources;


<#
string resxFileName = this.Host.TemplateFile.Replace(".tt", ".resx");
string className = Path.GetFileNameWithoutExtension(resxFileName);
XDocument doc = XDocument.Load(resxFileName);

#>

///





    ///   A strongly-typed resource class, for looking up localized strings, etc.
    ///
    // This class was auto-generated by the StronglyTypedResourceBuilder
    // class via a tool like ResGen or Visual Studio.
    // To add or remove a member, edit your .ResX file then rerun ResGen
    // with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class <#=className#>
    {

       private static global::System.Resources.ResourceManager resourceMan;
     
        private static global::System.Globalization.CultureInfo resourceCulture;
     
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public <#=className#>() {}
     
        ///





        ///   Returns the cached ResourceManager instance used by this class.
        ///
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("WpfExp.<#=className#>", typeof(<#=className#>).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
     
        ///





        ///   Overrides the current thread's CurrentUICulture property for all
        ///   resource lookups using this strongly typed resource class.
        ///
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }


<#
if(doc != null && doc.Root != null)
{
foreach(XElement x in doc.Root.Descendants("data"))
    {
        string name = x.Attribute("name").Value;
        WriteLine(string.Empty);
        WriteLine("        public static string " + name);
        WriteLine("        {");
        WriteLine("            get { return ResourceManager.GetString(\"" + name + "\", resourceCulture ?? CultureInfo.CurrentUICulture); }");
        WriteLine("        }");
    }
}
#>

}
}

see also: https://outlawtrail.wordpress.com/2014/03/17/custom-resources-with-t4/

2) change resource.Designer.cs in PreBuild Event:

ProjectProperties , Build Events,  pre build:

powershell.exe -command "(gc ..\..\Resource1.Designer.cs).Replace(\"new global::System.Resources.ResourceManager\", \"new MyResourceManager\") | set-content ..\..\Resource1.Designer.cs -Encoding UTF8"


No comments: