diff --git a/src/ThisAssembly.Strings/CSharp.sbntxt b/src/ThisAssembly.Strings/CSharp.sbntxt index 88875b8a..10921ee3 100644 --- a/src/ThisAssembly.Strings/CSharp.sbntxt +++ b/src/ThisAssembly.Strings/CSharp.sbntxt @@ -18,27 +18,27 @@ /// {{ end }} {{ func render }} - public static partial class {{ $0.Name }} + public static partial class {{ $0.Id }} { {{~ for value in $0.Values ~}} {{~ if!(value.HasFormat) ~}} {{- summary value ~}} - public static string {{ value.Name }} => Strings.GetResourceManager("{{ $1 }}").GetString("{{ $0.Prefix + value.Name }}"); + public static string {{ value.Id }} => Strings.GetResourceManager("{{ $1 }}").GetString("{{ value.Name }}"); {{~ else ~}} {{~ if value.IsIndexedFormat ~}} {{- summary value ~}} - public static string {{ value.Name }}( + public static string {{ value.Id }}( {{- for arg in value.Format -}} object arg{{~ arg ~}}{{ if !for.last }}, {{ end }} {{- end -}}) => string.Format( CultureInfo.CurrentCulture, - Strings.GetResourceManager("{{ $1 }}").GetString("{{ $0.Prefix + value.Name }}"), + Strings.GetResourceManager("{{ $1 }}").GetString("{{ value.Name }}"), {{ for arg in value.Format -}} arg{{- arg -}}{{- if !for.last -}}, {{ end }}{{- end -}}); {{~ else if value.IsNamedFormat ~}} {{- summary value ~}} - public static string {{ value.Name }}( + public static string {{ value.Id }}( {{- for arg in value.Format -}} object {{ arg ~}}{{ if !for.last }}, {{ end }} {{- end -}}) @@ -46,7 +46,7 @@ CultureInfo.CurrentCulture, Strings .GetResourceManager("{{ $1 }}") - .GetString("{{ $0.Prefix + value.Name }}") + .GetString("{{ value.Name }}") {{- for arg in value.Format }} .Replace("{%{{}%}{{ arg }}{%{}}%}", "{%{{}%}{{ for.index }}{%{}}%}"){{- end -}}, {{ for arg in value.Format -}} diff --git a/src/ThisAssembly.Strings/Model.cs b/src/ThisAssembly.Strings/Model.cs index 78f7ff89..02134316 100644 --- a/src/ThisAssembly.Strings/Model.cs +++ b/src/ThisAssembly.Strings/Model.cs @@ -15,6 +15,7 @@ record Model(ResourceArea RootArea, string ResourceName) static class ResourceFile { static readonly Regex FormatExpression = new Regex("{(?[^{}]+)}", RegexOptions.Compiled); + internal static readonly Regex NameReplaceExpression = new Regex(@"\||:|;|\>|\<", RegexOptions.Compiled); public static ResourceArea Load(string fileName, string rootArea) { @@ -33,17 +34,18 @@ public static ResourceArea Load(IEnumerable data, string rootArea) { // Splits: ([resouce area]_)*[resouce name] var nameAttribute = element.Attribute("name").Value; + var id = NameReplaceExpression.Replace(nameAttribute, "_"); var valueElement = element.Element("value").Value; var comment = element.Element("comment")?.Value?.Replace("<", "<").Replace(">", ">"); - var areaParts = nameAttribute.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries); + var areaParts = id.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries); if (areaParts.Length <= 1) { - root.Values.Add(GetValue(nameAttribute, valueElement) with { Comment = comment }); + root.Values.Add(GetValue(id, nameAttribute, valueElement) with { Comment = comment }); } else { var area = GetArea(root, areaParts.Take(areaParts.Length - 1)); - var value = GetValue(areaParts.Skip(areaParts.Length - 1).First(), valueElement) with { Comment = comment }; + var value = GetValue(areaParts.Skip(areaParts.Length - 1).First(), nameAttribute, valueElement) with { Comment = comment }; area.Values.Add(value); } @@ -65,13 +67,13 @@ static ResourceArea GetArea(ResourceArea area, IEnumerable areaPath) var currentArea = area; foreach (var areaName in areaPath) { - var existing = currentArea.NestedAreas.FirstOrDefault(a => a.Name == areaName); + var existing = currentArea.NestedAreas.FirstOrDefault(a => a.Id == areaName); if (existing == null) { if (currentArea.Values.Any(v => v.Name == areaName)) throw new ArgumentException(string.Format( "Area name '{0}' is already in use as a value name under area '{1}'.", - areaName, currentArea.Name)); + areaName, currentArea.Id)); existing = new ResourceArea(areaName, currentArea.Prefix + areaName + "_"); currentArea.NestedAreas.Add(existing); @@ -83,9 +85,9 @@ static ResourceArea GetArea(ResourceArea area, IEnumerable areaPath) return currentArea; } - static ResourceValue GetValue(string resourceName, string resourceValue) + static ResourceValue GetValue(string resourceId, string resourceName, string resourceValue) { - var value = new ResourceValue(resourceName, resourceValue); + var value = new ResourceValue(resourceId, resourceName, resourceValue); // Parse parameter names if (FormatExpression.IsMatch(resourceValue)) @@ -101,15 +103,15 @@ static ResourceValue GetValue(string resourceName, string resourceValue) } } -[DebuggerDisplay("Name = {Name}, NestedAreas = {NestedAreas.Count}, Values = {Values.Count}")] -record ResourceArea(string Name, string Prefix) +[DebuggerDisplay("Id = {Id}, NestedAreas = {NestedAreas.Count}, Values = {Values.Count}")] +record ResourceArea(string Id, string Prefix) { public List NestedAreas { get; init; } = new List(); public List Values { get; init; } = new List(); } -[DebuggerDisplay("{Name} = {Value}")] -record ResourceValue(string Name, string? Raw) +[DebuggerDisplay("{Id} = {Value}")] +record ResourceValue(string Id, string Name, string? Raw) { public string? Value => Raw?.Replace(Environment.NewLine, "")?.Replace("<", "<")?.Replace(">", ">"); public string? Comment { get; init; } diff --git a/src/ThisAssembly.Strings/ThisAssembly.Strings.csproj b/src/ThisAssembly.Strings/ThisAssembly.Strings.csproj index 29ae575c..298bcc80 100644 --- a/src/ThisAssembly.Strings/ThisAssembly.Strings.csproj +++ b/src/ThisAssembly.Strings/ThisAssembly.Strings.csproj @@ -43,5 +43,5 @@ such as "Hello {name}". - + diff --git a/src/ThisAssembly.Tests/Resources.resx b/src/ThisAssembly.Tests/Resources.resx index d755309d..5335b067 100644 --- a/src/ThisAssembly.Tests/Resources.resx +++ b/src/ThisAssembly.Tests/Resources.resx @@ -117,7 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + Value diff --git a/src/ThisAssembly.Tests/Tests.cs b/src/ThisAssembly.Tests/Tests.cs index 7d30e57f..2efb6cf3 100644 --- a/src/ThisAssembly.Tests/Tests.cs +++ b/src/ThisAssembly.Tests/Tests.cs @@ -5,17 +5,21 @@ namespace ThisAssemblyTests { public class Tests { + [Fact] + public void CanReadResourceFile() + => Assert.NotNull(ResourceFile.Load("Resources.resx", "Strings")); + [Fact] public void CanUseConstants() => Assert.Equal("Baz", ThisAssembly.Constants.Foo.Bar); [Fact] public void CanUseFileConstants() - => Assert.Equal(Path.Combine("Content", "Docs", "License.md"), ThisAssembly.Constants.Content.Docs.License); + => Assert.Equal(ThisAssembly.Constants.Content.Docs.License, Path.Combine("Content", "Docs", "License.md")); [Fact] public void CanUseFileConstantLinkedFile() - => Assert.Equal(Path.Combine("Included", "Readme.txt"), ThisAssembly.Constants.Included.Readme); + => Assert.Equal(ThisAssembly.Constants.Included.Readme, Path.Combine("Included", "Readme.txt")); [Fact] public void CanUseMetadata() diff --git a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj index 22297cf2..babd5226 100644 --- a/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj +++ b/src/ThisAssembly.Tests/ThisAssembly.Tests.csproj @@ -54,6 +54,10 @@ + + + +