Skip to content

Commit

Permalink
Merge pull request #13 from christianhelle/dotted-string-keys
Browse files Browse the repository at this point in the history
Add support for dotted string keys
  • Loading branch information
christianhelle authored May 27, 2019
2 parents 650d301 + 438ac2e commit b9e7083
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ A Visual Studio Custom Tool for generating a strongly typed helper class for acc
- Auto-updating of generated code file when changes are made to the .ResW Resource file
- XML documentation style comments like "Localized resource similar to '[the value]'"
- Supports Visual Studio 2015, 2017, and 2019
- Supports dotted keys - Replaces **.** with **_** (e.g. `Something.Awesome` = `Something_Awesome`)

**Custom Tools**

Expand Down Expand Up @@ -36,6 +37,7 @@ A Visual Studio Custom Tool for generating a strongly typed helper class for acc
test1 = App1.LocalizedResources.Resources.Test1;
test2 = App1.LocalizedResources.Resources.Test2;
test3 = App1.LocalizedResources.Resources.Test3;
test4 = App1.LocalizedResources.Resources.Test_With_Dotted_Keys;
}


Expand All @@ -47,6 +49,7 @@ A Visual Studio Custom Tool for generating a strongly typed helper class for acc
test1 = AppVb.LocalizedStrings.Resources.Test1
test2 = AppVb.LocalizedStrings.Resources.Test2
test3 = AppVb.LocalizedStrings.Resources.Test3
test4 = AppVb.LocalizedStrings.Resources.Test_With_Dotted_Keys;
End Sub


Expand Down Expand Up @@ -136,6 +139,17 @@ A Visual Studio Custom Tool for generating a strongly typed helper class for acc
}
}
/// <summary>
/// Localized resource similar to "test"
/// </summary>
public static string Test_With_Dotted_Keys
{
get
{
return Resource.GetString("Test/With/Dotted/Keys");
}
}
public static void Initialize()
{
string executingAssemblyName;
Expand Down Expand Up @@ -238,6 +252,15 @@ A Visual Studio Custom Tool for generating a strongly typed helper class for acc
End Get
End Property

'''<summary>
'''Localized resource similar to "test"
'''</summary>
Public Shared ReadOnly Property Test_With_Dotted_Keys() As String
Get
Return Resource.GetString("Test/With/Dotted/Keys")
End Get
End Property

Public Shared Sub Initialize()
Dim executingAssemblyName As String
executingAssemblyName = Windows.UI.Xaml.Application.Current.GetType().AssemblyQualifiedName
Expand Down
21 changes: 18 additions & 3 deletions src/VSPackage.Tests/CSharpCodeGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,31 @@ public void GeneratedCodeContainsPropertiesDefinedInResources()
{
var resourceItems = target.ResourceParser.Parse();

foreach (var item in resourceItems.Where(item => !item.Name.Contains(".")))
Assert.IsTrue(actual.Contains("public static string " + item.Name));
foreach (var item in resourceItems)
{
var value = $"public static string {item.Name.Replace(".", "_")}";
Assert.IsTrue(actual.Contains(value));
}
}

[TestMethod]
public void GeneratedCodeReplacesDottedKeysWithForwardSlash()
{
var resourceItems = target.ResourceParser.Parse();

foreach (var item in resourceItems)
{
var value = $"GetString(\"{item.Name.Replace(".", "/")}\")";
Assert.IsTrue(actual.Contains(value));
}
}

[TestMethod]
public void GeneratedCodePropertiesContainsCommentsSimilarToValuesDefinedInResources()
{
var resourceItems = target.ResourceParser.Parse();

foreach (var item in resourceItems.Where(item => !item.Name.Contains(".")))
foreach (var item in resourceItems)
Assert.IsTrue(actual.Contains("Localized resource similar to \"" + item.Value + "\""));
}

Expand Down
22 changes: 18 additions & 4 deletions src/VSPackage.Tests/VisualBasicCodeGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.IO;
using System.Linq;
using ChristianHelle.DeveloperTools.CodeGenerators.Resw.VSPackage.CustomTool;
using Microsoft.VisualBasic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -35,16 +34,31 @@ public void GeneratedCodeContainsPropertiesDefinedInResources()
{
var resourceItems = target.ResourceParser.Parse();

foreach (var item in resourceItems.Where(item => !item.Name.Contains(".")))
Assert.IsTrue(actual.Contains("Public Shared ReadOnly Property " + item.Name + "() As String"));
foreach (var item in resourceItems)
{
var value = $"Public Shared ReadOnly Property {item.Name.Replace(".", "_")}() As String";
Assert.IsTrue(actual.Contains(value));
}
}

[TestMethod]
public void GeneratedCodeReplacesDottedKeysWithForwardSlash()
{
var resourceItems = target.ResourceParser.Parse();

foreach (var item in resourceItems)
{
var value = $"GetString(\"{item.Name.Replace(".", "/")}\")";
Assert.IsTrue(actual.Contains(value));
}
}

[TestMethod]
public void GeneratedCodePropertiesContainsCommentsSimilarToValuesDefinedInResources()
{
var resourceItems = target.ResourceParser.Parse();

foreach (var item in resourceItems.Where(item => !item.Name.Contains(".")))
foreach (var item in resourceItems)
Assert.IsTrue(actual.Contains("Localized resource similar to \"" + item.Value + "\""));
}

Expand Down
6 changes: 3 additions & 3 deletions src/VSPackage/CustomTool/CodeDomCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ public override string GenerateCode()
var resources = ResourceParser.Parse();
foreach (var item in resources)
{
if (string.IsNullOrEmpty(item.Name) || item.Name.Contains("."))
if (string.IsNullOrEmpty(item.Name))
continue;

var property = new CodeMemberProperty
{
Name = item.Name.Trim(),
Name = item.Name.Replace(".", "_").Trim(),
Attributes = MemberAttributes.Public | MemberAttributes.Static,
HasGet = true,
Type = new CodeTypeReference(typeof (string))
Expand All @@ -185,7 +185,7 @@ public override string GenerateCode()
new CodeMethodInvokeExpression(
new CodeFieldReferenceExpression(null, "Resource"),
"GetString",
new CodePrimitiveExpression(item.Name))));
new CodePrimitiveExpression(item.Name.Replace(".","/")))));

targetClass.Members.Add(property);
}
Expand Down

0 comments on commit b9e7083

Please sign in to comment.