Created
June 24, 2014 07:02
-
-
Save meziantou/d4a06965b04c8f2535f2 to your computer and use it in GitHub Desktop.
SR.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<#@ template language="C#" hostspecific="true" debug="true" #> | |
<#@ assembly name="System.Core" #> | |
<#@ assembly name="System.Windows.Forms" #> | |
<#@ assembly name="System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" #> | |
<#@ assembly name="System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" #> | |
<#@ import namespace="System.Collections" #> | |
<#@ import namespace="System.Globalization" #> | |
<#@ import namespace="System.Linq" #> | |
<#@ import namespace="System.IO" #> | |
<#@ import namespace="System.Reflection" #> | |
<#@ import namespace="System.Resources" #> | |
<#@ import namespace="System.Collections.Generic" #> | |
<#@ import namespace="System.Text.RegularExpressions" #> | |
<#@ import namespace="System.Xml.Linq" #> | |
<# | |
string directory = System.IO.Path.GetDirectoryName(this.Host.TemplateFile); | |
string fileName = System.IO.Path.GetFileNameWithoutExtension(this.Host.TemplateFile); | |
string visibility = "public"; | |
string @namespace = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("NamespaceHint").ToString(); | |
string className = fileName; | |
var files = Directory.GetFiles(directory, fileName + "*.resx").OrderBy(_ => _.Length); | |
System.Environment.CurrentDirectory = directory; | |
List<ResXDataNode> nodes = new List<ResXDataNode>(); | |
foreach (var file in files) | |
{ | |
using (ResXResourceReader reader = new ResXResourceReader(file)) | |
{ | |
reader.UseResXDataNodes = true; | |
foreach (DictionaryEntry d in reader) | |
{ | |
ResXDataNode dataNode = (ResXDataNode)d.Value; | |
if(nodes.All(n => n.Name != dataNode.Name)) | |
nodes.Add(dataNode); | |
} | |
} | |
} | |
nodes.Sort((node, dataNode) => node.Name.CompareTo(dataNode.Name)); | |
AssemblyName[] assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies(); | |
#> | |
//------------------------------------------------------------------------------ | |
// <auto-generated> | |
// 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. | |
// </auto-generated> | |
//------------------------------------------------------------------------------ | |
namespace <#= @namespace #> | |
{ | |
using System; | |
/// <summary> | |
/// A strongly-typed resource class, for looking up localized strings, etc. | |
/// </summary> | |
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("<#= GetType().ToString() #>", "1.0.0.0")] | |
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] | |
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] | |
<#= visibility #> partial 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 #>() | |
{ | |
} | |
/// <summary> | |
/// Returns the cached ResourceManager instance used by this class. | |
/// </summary> | |
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] | |
public static global::System.Resources.ResourceManager ResourceManager | |
{ | |
get | |
{ | |
if (object.ReferenceEquals(resourceMan, null)) | |
{ | |
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("<#= @namespace #>.<#= className #>", typeof(<#= className #>).Assembly); | |
resourceMan = temp; | |
} | |
return resourceMan; | |
} | |
} | |
/// <summary> | |
/// Overrides the current thread's CurrentUICulture property for all | |
/// resource lookups using this strongly typed resource class. | |
/// </summary> | |
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] | |
public static global::System.Globalization.CultureInfo Culture | |
{ | |
get | |
{ | |
return resourceCulture; | |
} | |
set | |
{ | |
resourceCulture = value; | |
} | |
} | |
public static object GetObject(System.Globalization.CultureInfo culture, string name, string defaultValue) | |
{ | |
object obj = ResourceManager.GetObject(name, culture); | |
if ((obj == null)) | |
{ | |
return defaultValue; | |
} | |
return obj; | |
} | |
public static object GetObject(string name, string defaultValue) | |
{ | |
return GetObject(null, name, defaultValue); | |
} | |
public static string GetString(System.Globalization.CultureInfo culture, string name, params object[] args) | |
{ | |
string str = ResourceManager.GetString(name, culture); | |
if ((str == null)) | |
{ | |
return name; | |
} | |
if ((args != null)) | |
{ | |
return string.Format(culture, str, args); | |
} | |
else | |
{ | |
return str; | |
} | |
} | |
public static string GetString(string name, params object[] args) | |
{ | |
return GetString(null, name, args); | |
} | |
public static string GetString(string name, string defaultValue) | |
{ | |
return GetStringWithDefault(null, name, defaultValue, null); | |
} | |
public static string GetString(string name) | |
{ | |
return GetStringWithDefault(null, name, name, null); | |
} | |
public static string GetStringWithDefault(System.Globalization.CultureInfo culture, string name, string defaultValue, params object[] args) | |
{ | |
string str = ResourceManager.GetString(name, culture); | |
if ((str == null)) | |
{ | |
if (defaultValue == null || args == null) | |
{ | |
return defaultValue; | |
} | |
else | |
{ | |
return string.Format(culture, defaultValue, args); | |
} | |
} | |
if ((args != null)) | |
{ | |
return string.Format(culture, str, args); | |
} | |
else | |
{ | |
return str; | |
} | |
} | |
public static string GetStringWithDefault(string name, string defaultValue, params object[] args) | |
{ | |
return <#= className #>.GetStringWithDefault(null, name, defaultValue, args); | |
} | |
public static string GetStringWithDefault(string name, string defaultValue) | |
{ | |
return <#= className #>.GetStringWithDefault(null, name, defaultValue, null); | |
} | |
<# | |
foreach(var node in nodes) | |
{ | |
string nodeValue = node.GetValue(assemblies).ToString(); | |
#> | |
/// <#= new XElement("summary", "Looks up a localized string similar to " + nodeValue + ".").ToString().Replace(System.Environment.NewLine, System.Environment.NewLine + " /// ") #> | |
public static string <#= node.Name #> | |
{ | |
get | |
{ | |
return GetString("<#= node.Name #>"); | |
} | |
} | |
<# | |
// Select all tokens between braces that constitute valid identifiers | |
var tokens = Regex.Matches(nodeValue, @"{[0-9]+}").Cast<Match>().Select(m => m.Value).Distinct().ToList(); | |
if(tokens.Any()) | |
{ | |
var inParams = tokens.Aggregate("", (list, value) => list + (", object " + GetArgumentName(value))); | |
if (inParams.Length > 0) | |
{ | |
inParams = inParams.Substring(2); | |
} | |
var outParams = tokens.Aggregate("", (list, value) => list + (", " + GetArgumentName(value))); | |
#> | |
/// <#= new XElement("summary", "Looks up a localized string similar to " + nodeValue + ".").ToString().Replace(System.Environment.NewLine, System.Environment.NewLine + " /// ") #> | |
public static string Format<#= node.Name#>(System.Globalization.CultureInfo provider, <#= inParams#>) | |
{ | |
return GetString(provider, "<#= node.Name #>"<#= outParams #>); | |
} | |
/// <#= new XElement("summary", "Looks up a localized string similar to " + nodeValue + ".").ToString().Replace(System.Environment.NewLine, System.Environment.NewLine + " /// ") #> | |
public static string Format<#= node.Name#>(<#= inParams#>) | |
{ | |
return GetString("<#= node.Name #>"<#= outParams #>); | |
} | |
<# | |
} | |
#> | |
<# | |
} | |
#> | |
} | |
<#= visibility #> partial class <#= className #>Names | |
{ | |
<# | |
foreach (var node in nodes) | |
{ | |
#> | |
public const string <#= node.Name #> = "<#= node.Name #>"; | |
<# | |
} | |
#> | |
} | |
} | |
<#+ | |
string GetArgumentName(string name) | |
{ | |
name = name.Replace("{", "").Replace("}", "").Split(':')[0]; | |
int result; | |
if (int.TryParse(name, out result)) | |
{ | |
return "arg" + result.ToString(CultureInfo.InvariantCulture); | |
} | |
return name; | |
} | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment