diff --git a/Readme.md b/Readme.md index a004034..4bbe8c5 100644 --- a/Readme.md +++ b/Readme.md @@ -41,7 +41,7 @@ echo " Hello {0} 0 = name - + World @@ -143,7 +143,7 @@ echo " Hallo {0} 0 = name - + Welt @@ -162,6 +162,87 @@ dotnet run 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. + +## How it works + +The ResourceString.Net source code generator operates by reading all `AdditionalFiles` with the `.resx` file extension and generating a static class based on the specified `data` elements within the file. + +For example, given the following XML element: + +```xml + + Welt + +``` + +the source code generator transforms it into the following `C#` class members: + +```cs +#region World + +private static readonly Lazy LazyWorld = new Lazy( + () => new ResourceManagerString("World", ResourceManager, CultureInfo.CurrentCulture), + LazyThreadSafetyMode.PublicationOnly +); + +public static IResourceString World => LazyWorld.Value; + +#endregion // World +``` + +If an element contains a format string, such as: + +```xml + + Hello {0} and {1} + +``` + +the generator generates following code to support the formatted string: + +```cs +#region Greetings + +internal static class Greetings +{ + private static readonly Lazy LazyFormat = new Lazy( + () => new ResourceManagerString("Greetings", ResourceManager, CultureInfo.CurrentCulture), + LazyThreadSafetyMode.PublicationOnly + ); + + public static IResourceString Format => LazyFormat.Value; + + public static IResourceString From(IResourceString p1, IResourceString p2) => new FormattedResourceString( + Format, + p1, + p2 + ); +} + +#endregion // Greetings +``` + +In cases where the element with the format string includes a comment element like: + +```xml + + Hello {0} and {1} + 0 = name, 1 = otherName + +``` + +the source generator extracts the parameter names from the comment instead of using generic names: + +```cs +public static IResourceString From(IResourceString name, IResourceString otherName) => new FormattedResourceString( + Format, + name, + otherName +); +``` + +This allows for more descriptive parameter names in the generated code. + ## The ResourceString-Classes ### IResourceString