add how to chapter
This commit is contained in:
85
Readme.md
85
Readme.md
@@ -41,7 +41,7 @@ echo "<?xml version='1.0' encoding='utf-8'?>
|
||||
<value>Hello {0}</value>
|
||||
<comment>0 = name</comment>
|
||||
</data>
|
||||
<data name='World' type='System.String'>
|
||||
<data name='World'>
|
||||
<value>World</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -143,7 +143,7 @@ echo "<?xml version='1.0' encoding='utf-8'?>
|
||||
<value>Hallo {0}</value>
|
||||
<comment>0 = name</comment>
|
||||
</data>
|
||||
<data name='World' type='System.String'>
|
||||
<data name='World'>
|
||||
<value>Welt</value>
|
||||
</data>
|
||||
</root>
|
||||
@@ -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
|
||||
<data name='World'>
|
||||
<value>Welt</value>
|
||||
</data>
|
||||
```
|
||||
|
||||
the source code generator transforms it into the following `C#` class members:
|
||||
|
||||
```cs
|
||||
#region World
|
||||
|
||||
private static readonly Lazy<IResourceString> LazyWorld = new Lazy<IResourceString>(
|
||||
() => 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
|
||||
<data name='Greetings'>
|
||||
<value>Hello {0} and {1}</value>
|
||||
</data>
|
||||
```
|
||||
|
||||
the generator generates following code to support the formatted string:
|
||||
|
||||
```cs
|
||||
#region Greetings
|
||||
|
||||
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 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
|
||||
<data name='Greetings'>
|
||||
<value>Hello {0} and {1}</value>
|
||||
<comment>0 = name, 1 = otherName </comment>
|
||||
</data>
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user