fix resource class name generation -> 0.0.3

This commit is contained in:
2023-05-19 18:43:41 +00:00
parent 4c1f36e5f8
commit 2075b35f7b
9 changed files with 173 additions and 97 deletions

114
Readme.md
View File

@@ -3,8 +3,10 @@
## What is ResourceString.Net?
ResourceString.Net is a powerful .NET library that allows you to work with string resources in a type-safe manner.
It leverages the `resx` file in your project and utilizes a [c# source code generator](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview) to create a comprehensive API.
It leverages the `resx` file in your project and utilizes a [c# source code generator](https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview) to create a comprehensive API. No [`Designer.cs`](https://learn.microsoft.com/en-us/dotnet/framework/tools/resgen-exe-resource-file-generator?source=recommendations) or [`T4 Text Templates`](https://learn.microsoft.com/en-us/visualstudio/modeling/code-generation-and-t4-text-templates?) filer for resources are required any more.
With ResourceString.Net, you can handle resource strings as "multi-language strings" (see [The ResourceString-Classes](#the-resourcestring-classes)) instead of built-in strings.
This provides the ability to switch languages during runtime without the need to rerun string factory methods.
Additionally, ResourceString.Net ensures that formatted strings have methods with the correct number of expected parameters.
@@ -81,53 +83,85 @@ namespace MyTestConsoleApp
{
internal static class Resources
{
#region ResourceManager
#region ResourceManager
private static readonly Type _Type = typeof(Resources);
private static readonly Lazy<ResourceManager> _ResourceManager = new Lazy<ResourceManager>(
() => new ResourceManager("MyTestConsoleApp.Resources" ?? string.Empty, _Type.Assembly),
LazyThreadSafetyMode.PublicationOnly
);
private static readonly Type _Type = typeof(Resources);
public static ResourceManager ResourceManager => _ResourceManager.Value;
#endregion // ResourceManager
private static readonly Lazy<ResourceManager> _ResourceManager = new Lazy<ResourceManager>(
() => new ResourceManager(_Type.FullName ?? string.Empty, _Type.Assembly),
LazyThreadSafetyMode.PublicationOnly
);
#region Greetings
public static ResourceManager ResourceManager => _ResourceManager.Value;
internal static class Greetings
{
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("Greetings", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
#endregion // ResourceManager
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString name) => new FormattedResourceString(
Format,
name
);
internal static class Greetings
{
private static readonly Lazy<IResourceString> LazyFormat = new Lazy<IResourceString>(
() => new ResourceManagerString("Greetings", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
}
public static IResourceString Format => LazyFormat.Value;
public static IResourceString From(IResourceString name) => new FormattedResourceString(
Format,
name
);
}
#region World
#endregion // Greetings
private static readonly Lazy<IResourceString> LazyWorld = new Lazy<IResourceString>(
() => new ResourceManagerString("World", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
#region World
public static IResourceString World => LazyWorld.Value;
private static readonly Lazy<IResourceString> LazyWorld = new Lazy<IResourceString>(
() => new ResourceManagerString("World", ResourceManager, CultureInfo.CurrentCulture),
LazyThreadSafetyMode.PublicationOnly
);
#endregion // World
public static IResourceString World => LazyWorld.Value;
#endregion // World
}
}
```
### Add Multi-Languages
Run the following script to add a language specific resource file and add a culture change during runtime:
```sh
echo "<?xml version='1.0' encoding='utf-8'?>
<root>
<data name='Greetings'>
<value>Hallo {0}</value>
<comment>0 = name</comment>
</data>
<data name='World' type='System.String'>
<value>Welt</value>
</data>
</root>
" > Resources.de.resx
echo "
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo(\"de-DE\");
Console.WriteLine(message.Value);
" >> Program.cs
dotnet run
# Expected output: Hello World \n Hallo Welt
```
In the code from `Program.cs` you can see there is explicit rebuild of the format string required after the culture switch.
The `IResourceString` object themselves are rebuilding the culture specific string.
## The ResourceString-Classes
### IResourceString
@@ -212,13 +246,13 @@ You can redirect the output to a file if desired.
## Third party packages
| Package | Version |
| ------------------------------------- | ------- |
| Microsoft.CodeAnalysis.Analyzers | 3.3.4 |
| Microsoft.CodeAnalysis.CSharp | 4.3.0 |
| NETStandard.Library (Auto-referenced) | 2.0.3 |
| LanguageExt.Core | 4.4.3 |
| System.Resources.Extensions | 7.0.0 |
| Package | Version |
| --------------------------------------------------------------------------------------------------- | ------- |
| [Microsoft.CodeAnalysis.Analyzers](https://www.nuget.org/packages/Microsoft.CodeAnalysis.Analyzers) | 3.3.4 |
| [Microsoft.CodeAnalysis.CSharp](https://www.nuget.org/packages/Microsoft.CodeAnalysis.CSharp) | 4.3.0 |
| [NETStandard.Library](https://www.nuget.org/packages/NETStandard.Library) | 2.0.3 |
| [LanguageExt.Core](https://www.nuget.org/packages/LanguageExt.Core) | 4.4.3 |
| [System.Resources.Extensions](https://www.nuget.org/packages/System.Resources.Extensions) | 7.0.0 |
## Development Notes