Skip to content
This repository has been archived by the owner on Oct 18, 2018. It is now read-only.

Commit

Permalink
Modify Resources.tt to generate a property and a format method for ea…
Browse files Browse the repository at this point in the history
…ch resource
  • Loading branch information
pranavkm committed Feb 27, 2014
1 parent 5dccaab commit e859697
Showing 1 changed file with 50 additions and 31 deletions.
81 changes: 50 additions & 31 deletions build/Resources.tt
Expand Up @@ -83,35 +83,12 @@ namespace {0}
foreach (var resourceString in resourceStrings)
{
GenerationEnvironment.AppendLine();
GenerationEnvironment.AppendFormat(@" /// <summary>").AppendLine();
foreach (var line in resourceString.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
{
GenerationEnvironment.AppendFormat(" /// {0}", line.Replace("<", "&lt;").Replace(">", "&gt;"))
.AppendLine();
}

GenerationEnvironment.AppendFormat(
@" /// </summary>
internal static string {0}{1}
{{
", resourceString.Name, resourceString.Arguments.Count > 0 ? resourceString.Parameters : string.Empty);

if (resourceString.Arguments.Count == 0)
{
GenerationEnvironment.AppendFormat(
@" get {{ return GetString(""{0}""); }}", resourceString.Name);
}
else
{
GenerationEnvironment.AppendFormat(
@" return string.Format(CultureInfo.CurrentCulture, GetString(""{0}""{1}), {2});",
resourceString.Name,
resourceString.UsingNamedArgs ? ", " + resourceString.FormatArguments : null,
resourceString.ArgumentNames);
}

GenerationEnvironment.AppendLine().Append(
@" }").AppendLine();
RenderHeader(GenerationEnvironment, resourceString);
RenderProperty(GenerationEnvironment, resourceString);

GenerationEnvironment.AppendLine();
RenderHeader(GenerationEnvironment, resourceString);
RenderFormatMethod(GenerationEnvironment, resourceString);
}

GenerationEnvironment.Append(@"
Expand Down Expand Up @@ -145,6 +122,49 @@ namespace {0}
}
#>
<#+
private static void RenderHeader(StringBuilder builder, ResourceData resourceString)
{
builder.Append(" /// <summary>")
.AppendLine();
foreach (var line in resourceString.Value.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
{
builder.AppendFormat(" /// {0}", line.Replace("<", "&lt;").Replace(">", "&gt;"))
.AppendLine();
}
builder.Append(" /// </summary>")
.AppendLine();
}

private static void RenderProperty(StringBuilder builder, ResourceData resourceString)
{
builder.AppendFormat(" internal static string {0}", resourceString.Name)
.AppendLine()
.AppendLine(" {")
.AppendFormat(@" get {{ return GetString(""{0}""); }}", resourceString.Name)
.AppendLine()
.AppendLine(" }");
}

private static void RenderFormatMethod(StringBuilder builder, ResourceData resourceString)
{
builder.AppendFormat(" internal static string Format{0}({1})", resourceString.Name, resourceString.Parameters)
.AppendLine()
.AppendLine(" {");
if(resourceString.Arguments.Count > 0)
{
builder.AppendFormat(@" return string.Format(CultureInfo.CurrentCulture, GetString(""{0}""{1}), {2});",
resourceString.Name,
resourceString.UsingNamedArgs ? ", " + resourceString.FormatArguments : null,
resourceString.ArgumentNames);
}
else
{
builder.AppendFormat(@" return GetString(""{0}"");", resourceString.Name);
}
builder.AppendLine()
.AppendLine(" }");
}


private class ResourceData
{
Expand All @@ -166,13 +186,12 @@ private class ResourceData

public string Parameters
{
get { return "(" + string.Join(", ", Arguments.Select(a => "object " + GetArgName(a))) + ")"; }
get { return string.Join(", ", Arguments.Select(a => "object " + GetArgName(a))); }
}

public string GetArgName(string name)
{
return UsingNamedArgs ? name : 'p' + name;
}
}

#>

1 comment on commit e859697

@lajones
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe combine lines 127 & 128 and similarly 134 & 135. But not a big deal. :shipit:

Please sign in to comment.